TechView with Kamlesh khaliya

Resources for OCP, Oracle Interviews(kamleshkhaliya@gmail.com)

Friday 24 August 2012

Interview Questions and Answers Unix & Shell

Q. How will you kill all the child process of a parent process?
For example a.sh is parent shell and it execute many other shell files then first need to kill all the child and then main shell. How can we do this in unix shell scripting?

Q. What is autosys? Why do we need to use autosys?
Ans - Autosys is a job scheduling software like Control - M and cron, with the help of autosys we can define the run time,day,date,week and which script or program needs to be run.
In real business, you need to fire jobs not just based on scheduled time, but also based on events (like an arrival of a file), and, based on the success or failure of other jobs. Autosys is a total job scheduling solution that allows you to define these events, dependencies, time schedules, alerts, etc, making it a complete data center automation tool.
The main advantage of using autosys w.r.t crontab is that it is has a Java front end too, so a person do not need to be a Unix champ to create or change a job in autosys.

Q. How to schedule a job in autosys?
Ans - There are the two methods you can use to create job definitions:
¦ Using the AutoSys Graphical User Interface (GUI).
¦ Using the AutoSys Job Information Language (JIL) through a command-line interface.

Q. Have you ever used any other scheduling tool, other than Cron Tab?
Ans – Yes I have used Autosys and Control M.

Q. How can you identify the process currently running?
Ans - ps -eaf | grep RU -- will list out the Processes currently running in the System

Q. Explain about sourcing commands?
Sourcing commands help you to execute the scripts within the scripts. For example sh command makes your program to run as a separate shell. .command makes your program to run within the shell. This is an important command for beginners and for special purposes.

Q. How do you read arguments in a shell program - $1, $2 ?
A. Shell script accepts parameters in following format...
$1 : first
$2 : second....so on upto
$9 : 9th param
whereas $0 : gives script/function name
If your script has more than 9 params then accept in following way...
${12} : 12th param
${18} : 18th param

Q. What are the different kinds of loops available in shell script?
A. Broadly categorised in 3
for
while
until
Q. What does $# stand for?
A. $# returns the number of parameters that are passed to a shell script
$? returns the exit code of the last executed command (0 : Successful, 1 or other: Failed)

Q. How do u open a read only file in Unix?
A. "vi -R filename"
In a shell script you can open a file in read only mode by using O_RDONLY
Syntax - open(<filename> O_RDONLY)
more <file>
cat <file>
less <file>

Q. What is the difference between a shell variable that is exported and the one that is not exported?
A. exported variable is visible to the child processes while the normal variables are not.
Export is to set the vaibales globally

Q. If you have a string "one two three", Which shell command would you use to extract the strings?
A. echo "one two three" | awk '{print $1 $2 $3}'
echo $string | cut -d -f1
echo $string | cut -d -f2
echo $string | cut -d -f3

Q. How do you schedule a command to run at 4:00 every morning?
A. Schedule the Job using crontab..From the command prompt perform a man on crontab and then type in crontab -e to create a new job
Crontab format
* * * * * <command>
<minute> <hour> <day> <month> <day_of_the_week> command
<0-59> <0-23> <1-31> <1-12> <0-7> command

Q. How will you list only the empty lines in a file (using grep)?
A. We can do this efficiently through awk script

awk '{
if (NF 0)
{
print "Here comes the empty line"
print $0
}
}' filename

NF number of fields in the current line
$0 current line

Q. How would you get the character positions 10-20 from a text file?
A. cat filename.txt | cut -c 10-20
TAB is default field separator for in a file for cut command.
cut -d"," -f2 cdr.txt
-d is for delimeter.
-f is for extracting the fields.
cut [OPTION]... [FILE]...
DESCRIPTION
Print selected parts of lines from each FILE to standard output.
Mandatory arguments to long options are mandatory for short options too.
-b, --bytes=LIST
output only these bytes
-c, --characters=LIST
output only these characters
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-f, --fields=LIST
output only these fields; also print any line that contains no delimiter character, unless the -s option is specified.

Q. How would you print just the 25th line in a file (smallest possible script please)?
A. head -25 cdr.txt | tail -1
sed -n '25p' cdr.txt
using awk we can do the same also.

Q. How would you replace the n character in a file with some xyz?
A. sed 's/n/xyz/g' filename > new_filename

Q. What is chmod in unix?
A. This is for file permissions on unix- first is for self, second for group, and third for others.
And this rwx for each with 421.
Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Tripler for o: r-x => 4 + 0 + 1 = 5
Which makes : 755

UNIX chmod command
To change permissions on a UNIX file or directory, use the chmod command. chmod category+permissions filename category can be omitted, in which case the permissions specified are granted to all three categories (user, group and other). If + is replaced by a -, then those permissions are taken away from the categories.
As an example, consider this statement:
chmod o+r data
This grants other read permission to the file data. The command
chmod +x data
grants everyone (user, group and other) execute permission, and the command
chmod g+rwx data
gives category group read, write and execute permission.
Permissions change cumulatively, in that the commands
chmod +r file
chmod +w file
chmod +x file
are equivalent to
chmod +rwx file

Q. What is the difference between a 'thread' and a 'process'?
A. The key difference is that processes are fully isolated
from each other; threads share (heap) memory with other
threads running in the same application.
OR
A process is an instance of an running application.
And a Thread is the execution stream of the process.

A process can have multiple threads.
OR
Changes to the main thread (cancellation, priority
change, etc.) may affect the behavior of the other threads
of the process; changes to the parent process does not
affect child processes. If we consider running a word
processing program to be a process, then the auto-save and
spell check features that occur in the background are
different threads of that process which are all operating
on the same data set (your document).
OR
A process runs in it’s own memory area, where the thread always run in the parent process memory.
This is just like parent child.

Q. Write a shell script to identify the given string is palindrome or not?
A. var hai
var1 `echo $var|rev`
if [ $var ! $var1 ]
then
echo $var is not palindrome.
else
echo $var is palyndrome
fi

Q. How Connect to a Database in Shell Programming?Please tell me Step by Step?
A. Hi
In order to connect to a database using a shell script it is always necessary to set the environment first and then connect to the database and execute whatever the statements u need to and come out of the script gracefully.

ex:
#!/bin/sh
export ORACLE_SID $1
export ORACLE_HOME more /etc/oratab|grep -v '#'|grep -i $1|cut -d":" -f2
export LD_LIBRARY_PATH $ORACLE_HOME/lib:/usr/lib
export PATH $ORACLE_HOME/bin:$HOME/bin:$PATH

sqlplus -s sys/<passwd> as sysdba <EOF
select name from v$database;
archive log list;
exit
EOF

The above script echoes the name of the database you are connected along with the archive log mode information.

Q. What is this line in the shell script do ?#!/bin/ksh?
A. This line is called as "Hash Bang" Statement.
It gives the environment or the shell in which your script would be run

Q. What is the basic difference u find between a shell script and perl.I mean the advantages of one over the other?
A. 1) PERL scripts can be used on both UNIX and windows systems unless some OS specific commands are used. But the same case is not with Shell scripting.
2) PERL scripts are used for web based applications but shell scripts can not be used for the same purpose.
3) PERL modules gives PERL extra edge over Shell scripts. PERL modules are extensive and can be used for n number of purposes.
4) Data base drivers are available for perl also.

Q. How to take input values from the user?
A. 1. using read command
2. reading input from a file

Q. How to compress files by using shell scripting?
A.
Command Syntax File Produced
gzip gzip .gz
zip zip .zip
compress compress .Z
pack pack .z
Gzip, zip, compress, pack are used for compression of files.
Tar is also used to compress.

Q. how to delete a word from a file using shell scripting???
A. sed -e 's/word//g' filename

Q. How do you search the string for vowel's occurrence and number of occurrences of each vowel.
A. grep -io [aeiou] filename | wc –w

Q. You have current directory containing set of directories which contain files.
One file can reside in many directories.
Write script which returns number of unique file names in
all the subdirectories of a current dir.
A. ls -R|sort|uniq

Q. What are the different types of shells available in UNIX?
A. bourne (sh)
c shell (csh)
korn (ksh)
bourne again shell (bash)
TC shell (tcsh)

Q. What are the different security mechanisms available in UNIX?
A. Unix is having 3 ways of Security Mechanism:
1. By granting or revoking File permissions. Owner or Admin can change permissions to be given to group or others by using chmod command in Unix.
2. Login is restricted using login credentials ( User name and Password ).
3. Password is kept in encrypted format in the file /etc/passwd

Q. What are the type of files in unix?
A. File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link
s socket
D door (Solaris)

Q. what is find command?
A. find is used for searching dir for files.
Examples –
find /oracle/oracle9i/Import -perm 755 -name "*.htm*" -mtime -1 -size 100k

Q. What is a named pipe?
A named pipe is a special file that is used to transfer data
between unrelated processes. One (or more) processes write to it, while
another process reads from it. Named pipes are visible in the file
system and may be viewed with
`ls' like any other file. (Named
pipes are also called
fifos; this term stands for `First In, First
Out'.)
To create a named pipe, use the Unix command mknod(1) or on some systems, mkfifo(1). These may not be in your normal path.
# system return val is backwards, so && not ||
#
$ENV{PATH} .= ":/etc:/usr/etc";
if ( system('mknod', $path, 'p')
&& system('mkfifo', $path) )
{
die "mk{nod,fifo} $path failed;
}
A fifo is convenient when you want to connect a process to an unrelated one. When you open a fifo, the program will block until there's something on the other end.
For example, let's say you'd like to have your .signature file be a named pipe that has a Perl program on the other end. Now every time any program (like a mailer, news reader, finger program, etc.) tries to read from that file, the reading program will block and your program will supply the new signature. We'll use the pipe-checking file test -p to find out whether anyone (or anything) has accidentally removed our fifo.
chdir; # go home
$FIFO = '.signature';
$ENV{PATH} .= ":/etc:/usr/games";
while (1) {
unless (-p $FIFO) {
unlink $FIFO;
system('mknod', $FIFO, 'p')
&& die "can't mknod $FIFO: $!";
}
# next line blocks until there's a reader
open (FIFO, "> $FIFO") || die "can't write $FIFO: $!";
print FIFO "John Smith (smith\@host.org)\n", `fortune -s`;
close FIFO;
sleep 2; # to avoid dup signals
}

Q. What is relative path and absolute path?
Absolute path : Exact path from root directory.
Relative path : Relative to the current path.

Q. Explain kill() and its possible return values?
There are four possible results from this call:
‘kill()’ returns 0. This implies that a process exists with the given PID, and the system would allow you to send signals to it. It is system-dependent whether the process could be a zombie.
‘kill()’ returns -1, ‘errno == ESRCH’ either no process exists with the given PID, or security enhancements are causing the system to deny its existence. (On some systems, the process could be a zombie.)
‘kill()’ returns -1, ‘errno == EPERM’ the system would not allow you to kill the specified process. This means that either the process exists (again, it could be a zombie) or draconian security enhancements are present (e.g. your process is not allowed to send signals to *anybody*).
‘kill()’ returns -1, with some other value of ‘errno’ you are in trouble! The most-used technique is to assume that success or failure with ‘EPERM’ implies that the process exists, and any other error implies that it doesn't.
An alternative exists, if you are writing specifically for a system (or all those systems) that provide a ‘/proc’ filesystem: checking for the existence of ‘/proc/PID’ may work.

Q. What is a pipe and give an example?
A pipe is two or more commands separated by pipe char '|'. That tells the shell to arrange for the output of the preceding command to be passed as input to the following command.
Example : ls -l | pr
The output for a command ls is the standard input of pr.
When a sequence of commands are combined using pipe, then it is called pipeline.

Q. How to terminate a process which is running and the specialty on command kill 0?
To find out weather a process is running or not :
We can use ps command with gives the current running process.
And after that we can apply grep command to filter out the process name.
Like to find out sshd process is running or not : $ ps -ewwo pid,args | grep [s]sh
Output :
5341 /usr/sbin/sshd
5864 /usr/bin/ssh-agent x-session-manager
6289 ssh oldbox
7126 ssh admin@core.r1.vsnl.router

Where
  • ps : Command name
  • -ewwo pid,args : -e option force to select all running processes. -o option is used to specify user-defined format. In our case we are forcing to display only program pid and its arguments. Finally -w option specifies wide output. Use this option twice for unlimited width.
  • grep [s]sh : We are just filtering out sshd string
Once we got the process id we can kill the process with kill command:
Syntax is : kill -N PID
Where,
  • N is a signal number
  • PID is the Process Identification Number. If you do not know the PID, it can be learned through the ps command.

Some of the more commonly used signals:

signal #
Usage
1
HUP (hang up) (Default)
2
INT (interrupt)
3
QUIT (quit)
6
ABRT (abort)
9
KILL (non-catchable, non-ignorable kill)
14
ALRM (alarm clock)
15
TERM (software termination signal)

Kill 0 - kills all processes in your system except the login shell.

Q. What is redirection?
Directing the flow of data to the file or from the file for input or output.
Example : ls > wc

Q. What are shell variables?
Shell variables are special variables, a name-value pair created and maintained by the shell.
Example: PATH, HOME, MAIL and TERM

Q. How to switch to a super user status to gain privileges?
Use ‘su’ command. The system asks for password and when valid entry is made the user gains super user (admin) privileges.

Q. How does the kernel differentiate device files and ordinary files?
Kernel checks 'type' field in the file's inode structure.

Q. How many prompts are available in a UNIX system?
Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).

Q. Name the data structure used to maintain file identification?
‘inode’, each file has a separate inode and a unique inode number.

Q. Is it possible to count number char, line in a file; if so, How?
Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.

Q. Is ‘du’ a command? If so, what is its use?
Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity and free space of the disk.

Q. What is the use of the command "ls -x chapter[1-5]"?
ls stands for list; so it displays the list of the files that starts with 'chapter' with suffix '1' to '5', chapter1, chapter2, and so on.

Q. Is it possible to restrict incoming message?
Yes, using the ‘mesg’ command.

Q. Is it possible to create new a file system in UNIX?
Yes, ‘mkfs’ is used to create a new file system.

Q. What will the following command do?
$ echo *
It is similar to 'ls' command and displays all the files in the current
directory.

Q. Write a command to display a file’s contents in various formats?
$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.

Q. Which command is used to delete all files in the current directory and all its sub-directories?
rm -r *

Q. Write a command to kill the last background job?
Kill $!

Q. What is the difference between cat and more command?
Cat displays file contents. If the file is large the contents scroll off the screen before we view it. So command 'more' is like a pager which displays the contents page by page.


Q. What is the use of ‘grep’ command?
‘grep’ is a pattern search command. It searches for the pattern, specified in the command line with appropriate option, in a file(s).
Syntax : grep
Example : grep 99mx mcafile

Q. What difference between cmp and diff commands?
cmp - Compares two files byte by byte and displays the first mismatch
diff - tells the changes to be made to make the files identical

Q. Explain the steps that a shell follows while processing a command?
After the command line is terminated by the key, the shel goes ahead with processing the command line in one or more passes. The sequence is well defined and assumes the following order.
Parsing: The shell first breaks up the command line into words, using spaces and the delimiters, unless quoted. All consecutive occurrences of a space or tab are replaced here with a single space.
Variable evaluation: All words preceded by a $ are avaluated as variables, unless quoted or escaped.
Command substitution: Any command surrounded by backquotes is executed by the shell which then replaces the standard output of the command into the command line.
Wild-card interpretation: The shell finally scans the command line for wild-cards (the characters *, ?, [, ]). Any word containing a wild-card is replaced by a sorted list of filenames that match the pattern. The list of these filenames then forms the arguments to the command.
PATH evaluation: It finally looks for the PATH variable to determine the sequence of directories it has to search in order to hunt for the command.

Q. What is the difference between > and >> redirection operators ?
is the output redirection operator when used it overwrites while >> operator appends into the file.

Q. Which of the following commands is not a filter?(a)man , (b) cat , (c) pg , (d) head?
Ans: man
A filter is a program which can receive a flow of data from std input, process (or filter) it and send the result to the std output.


Q. Construct pipes to execute the following jobs?
1. Output of who should be displayed on the screen with value of total number of users who have logged in displayed at the bottom of the list.
2. Output of ls should be displayed on the screen and from this output the lines containing the word ‘poem’ should be counted and the count should be stored in a file.
3. Contents of file1 and file2 should be displayed on the screen and this output should be appended in a file .
From output of ls the lines containing ‘poem’ should be displayed on the screen along with the count.
4. Name of cities should be accepted from the keyboard . This list should be combined with the list present in a file. This combined list should be sorted and the sorted list should be stored in a file ‘newcity’.
5. All files present in a directory dir1 should be deleted any error while deleting should be stored in a file ‘errorlog’.

Q. How to find free space in unix/linux?
df -h

Q. What is the difference between soft link and hard link in unix operating system ?
Hard Links :
1. All Links have same inode number.
2.ls -l command shows all the links with the link column(Second) shows No. of links.
3. Links have actual file contents
4.Removing any link just reduces the link count but doesn't affect other links.

Soft Links(Symbolic Links) :
1.Links have different inode numbers.
2. ls -l command shows all links with second column value 1 and the link points to original file.
3. Link has the path for original file and not the contents.
4.Removing soft link doesn't affect anything but removing original file the link becomes dangling link which points to nonexistant file.

Q. What are the differences between Shared and Dynamic libraries?
Shared libraries are loaded into memory before compilation during parsing whereas dynamic libraries are loaded during compilation time itself.
Q. How would you create shared and dynamic libraries?
Well shared libraries have 2 types

1) Static
2) Dynamic.
u can create library by
ar cr -o sharedobj.a file1.o file2.o
while file1 and file2 are headfiles (obj)
now put this sharedobj.a into /usr/lib directory

Q. What type of scheduling is used in Unix?
Multi Level Feedback Queue Scheduling with each queue in round robin

Q. What is stty used for?
stty is used to set your terminal options. Following are some of the typical examples1. stty echo -iuclc The above command will switch off character display on your screen and ignore any case differences (ignore upper case and lower case). This can be used in situation where password is read and should not get printed on screen2. stty -erase '^H' This will set the erase character for your terminal3. stty -intr '^c' This will set the execution interrupt as control+c key 4. stty -eof '^d' This will set the end of file character as control+d key

Q. How to read and write of a open file?
Read from file:
cat file_name

Or on terminal pagewise read as :
more file_name.
Write to file :
echo "`cmd`" > file_name.

Where cmd= any executable UNIX command.
> = redirect the o/p.
>> = append to file_name.

Q. What are the directory commands available in UNIX programming?
mkdir to create directory
cd to change directory
rmdir to remove directory (must be empty)
pwd to show present working directory
ls to list contents of a directory

Q. What is the significance of the “tee” command?
It reads the standard input and sends it to the standard output while redirecting a copy of what it has read to
the file specified by the user.

Q. Write a command to display a file’s contents in various formats?
$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.
Q. How does the kernel differentiate device files and ordinary files?
Kernel checks 'type' field in the file's inode structure.

Q. In Unix, what is a symbolic link, and how do I create one?

A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. This difference gives symbolic links certain qualities that hard links do not have, such as the ability to link to directories, or to files on remote computers networked through NFS. Also, when you delete a target file, symbolic links to that file become unusable, whereas hard links preserve the contents of the file.
To create a symbolic link in Unix, at the Unix prompt, enter:
ln -s source_file myfile
Replace source_file with the name of the existing file for which you want to create the symbolic link (this file can be any existing file or directory across the file systems). Replace myfile with the name of the symbolic link. The ln command then creates the symbolic link. After you've made the symbolic link, you can perform an operation on or execute myfile, just as you could with the source_file. You can use normal file management commands (e.g., cp, rm) on the symbolic link.
Note: If you delete the source file or move it to a different location, your symbolic file will not function properly. You should either delete or move it. If you try to use it for other purposes (e.g., if you try to edit or execute it), the system will send a "file nonexistent" message.

Q. Where is Socket used?

A Unix Socket is used in a client server application frameworks. A server is a process which does some function on request from a client. Most of the application level protocols like FTP, SMTP and POP3 make use of Sockets to establish connection between client and server and then for exchanging data.

Socket Types:

There are four types of sockets available to the users. The first two are most commenly used and last two are rarely used.
  • Stream Sockets: Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A,B,C", they will arrive in the same order - "A,B,C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do no have any boundaries.
  • Datagram Sockets: Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets - you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).
  • Raw Sockets: provides users access to the underlying communication protocols which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more esoteric facilities of an existing protocol.
  • Sequenced Packet Sockets: They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming packets.
Q. What is door in unix?
Ans. - Doors are an inter-process communication facility for Unix computer systems. They provide a form of procedure call. Doors were developed by Sun Microsystems as a core part of the Spring operating system, then added to Solaris
Crontab – Quick Reference

Setting up cron jobs in Unix and Solaris
cron is a unix, solaris utility that b which tasks run automatically in the background at regular intervals. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity

1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny. If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab.

2. Crontab Commands
export EDITOR=vi ;to specify a editor to open crontab file.
crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

3. Crontab file
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30 18 * * * rm /home/someuser/tmp/*
Changing the parameter values as below will cause this command to run at different time schedule below :

min
hour
day/month
month
day/week
Execution time
30
0
1
1,6,12
*
00:30 Hrs on 1st of Jan, June & Dec.

0
20
*
10
1-5
–8.00 PM every weekday (Mon-Fri) only in Oct.

0
0
1,10,15
*
*
– midnight on 1st ,10th & 15th of month

5,10
0
10
*
1
– At 12.05,12.10 every Monday & on 10th of every month
:
Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.
5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1
7. Generate log file
To collect the cron execution execution log in a file :
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Basic UNIX commands

Files

Ls command
ls command is most widely used command and it displays the contents of directory.
options
ls will list all the files in your home directory, this command has many options.
ls -l will list all the file names, permissions, group, etc in long format.
ls -a will list all the files including hidden files that start with . .
ls -lt will list all files names based on the time of creation, newer files bring first.
ls -Fxwill list files and directory names will be followed by slash.
ls -Rwill lists all the files and files in the all the directories, recursively.
ls -R | more will list all the files and files in all the directories, one page at a time.
  • more filename --- shows the first part of a file, that can fit on one screen. Just hit the space bar to see more or q to quit.
  • emacs filename --- is an editor that lets you create and edit a file.
  • mv filename1 filename2 --- moves a file
  • cp filename1 filename2 --- copies a file
  • rm filename --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything.
  • diff filename1 filename2 --- compares files, and shows where they differ
  • wc filename --- tells you how many lines, words, and characters there are in a file -l for lines, -c for char, -w for words.
  • chmod options filename --- lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them. For example, chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others again.
  • File Compression
    • gzip filename --- compresses files. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended to the original filename.
    • gunzip filename --- uncompresses files compressed by gzip.
    • gzcat filename --- lets you look at a gzipped file without actually having to gunzip it (same as gunzip -c). You can even print it directly, using gzcat filename | lpr
  • printing
    • lpr filename --- print. Use the -P option to specify the printer name if you want to use a printer other than your default printer. For example, if you want to print double-sided, use 'lpr -Pvalkyr-d', or if you're at CSLI, you may want to use 'lpr -Pcord115-d'. See 'help printers' for more information about printers and their locations.
    • lpq --- check out the printer queue, e.g. to get the number needed for removal, or to see how many other files will be printed before yours will come out
    • lprm jobnumber --- remove something from the printer queue. You can find the job number by using lpq. Theoretically you also have to specify a printer name, but this isn't necessary as long as you use your default printer in the department.
    • genscript --- converts plain text files into postscript for printing, and gives you some options for formatting. Consider making an alias like alias ecop 'genscript -2 -r \!* | lpr -h -Pvalkyr' to print two pages on one piece of paper.
    • dvips filename --- print .dvi files (i.e. files produced by LaTeX). You can use dviselect to print only selected pages. See the LaTeX page for more information about how to save paper when printing drafts.

Directories

Directories, like folders on a Macintosh, are used to group files together in a hierarchical structure.
  • mkdir dirname --- make a new directory
  • cd dirname --- change directory. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position.
  • pwd --- tells you where you currently are.

About other people

  • w --- tells you who's logged in, and what they're doing. Especially useful: the 'idle' part. This allows you to see whether they're actually sitting there typing away at their keyboards right at the moment.
$w
11:47:16 up 28 days, 8:22, 2 users, load average: 1.01, 1.03, 1.26
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
whdcaxkc pts/0 10.57.42.146 Thu17 20:11m 0.35s 0.01s login -- whdcaxkc
whdcaxkc pts/1 10.57.12.174 10:38 0.00s 0.13s 0.01s login -- whdcaxkc
  • who --- tells you who's logged on, and where they're coming from (IP add).
$who
whdcaxkc pts/0 Aug 5 17:05 (10.57.42.146)
whdcaxkc pts/1 Aug 9 10:38 (10.57.12.174)
  • finger username --- gives you lots of information about that user, e.g. when they last read their mail and whether they're logged in. Often people put other practical information, such as phone numbers and addresses, in a file called .plan. This information is also displayed by 'finger'.
$finger whdcaxkc
Login: whdcaxkc Name: Kannan Chandran
Directory: /home/whdcaxkc Shell: /bin/bash
On since Thu Aug 5 17:05 (IST) on pts/0 from 10.57.42.146
20 hours 7 minutes idle
On since Mon Aug 9 10:38 (IST) on pts/1 from 10.57.12.174
No mail.
No Plan.
  • last -1 username --- tells you when the user last logged on and off and from where. Without any options, last will give you a list of everyone's logins.
  • talk username --- lets you have a (typed) conversation with another user
  • write username --- lets you exchange one-line messages with another user
  • elm --- lets you send e-mail messages to people around the world (and, of course, read them). It's not the only mailer you can use, but the one we recommend. It is like mailx on sun.

About your (electronic) self

  • whoami --- returns your username. Sounds useless, but isn't. You may need to find out who it is who forgot to log out somewhere, and make sure *you* have logged out.
  • finger & .plan files
    of course you can finger yourself, too. That can be useful e.g. as a quick check whether you got new mail. Try to create a useful .plan file soon. Look at other people's .plan files for ideas. The file needs to be readable for everyone in order to be visible through 'finger'. Do 'chmod a+r .plan' if necessary. You should realize that this information is accessible from anywhere in the world, not just to other people on turing.
  • passwd --- lets you change your password, which you should do regularly (at least once a year).
  • ps -u yourusername --- lists your processes. Contains lots of information about them, including the process ID, which you need if you have to kill a process.
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
whdcaxkc 4254 0.0 0.0 3480 744 pts/1 R+ 11:56 0:00 ps -u
whdcaxkc 9393 0.0 0.0 6004 1480 pts/0 S+ Aug06 0:00 bash
whdcaxkc 21998 0.0 0.0 5000 1508 pts/0 Ss Aug05 0:00 -bash
whdcaxkc 27731 0.0 0.0 5628 1488 pts/1 Ss 10:38 0:00 -bash
whdcaxkc 31295 0.0 0.0 4012 856 pts/1 T 11:08 0:00 man find
  • kill PID --- kills (ends) the processes with the ID you gave. This works only for your own processes, of course. Get the ID by using ps. If the process doesn't 'die' properly, we can use the option -9. But we should attempt without that option first, because it doesn't give the process a chance to finish possibly important business before dying.
  • quota -v --- show what your disk quota is (i.e. how much space you have to store files), how much you're actually using, and in case you've exceeded your quota (which you'll be given an automatic warning about by the system) how much time you have left to sort them out (by deleting or gzipping some, or moving them to your own computer).
  • du filename --- shows the disk usage of the files and directories in filename (without argument the current directory is used). du -s gives only a total.
  • last yourusername --- lists your last logins. Can be a useful memory aid for when you were where, how long you've been working for.

Connecting to the outside world

  • nn --- allows you to read news. It will first let you read the news local to turing, and then the remote news. If you want to read only the local or remote news, you can use nnl or nnr, respectively.
  • rlogin hostname --- lets you connect to a remote host
  • telnet hostname --- also lets you connect to a remote host. Use rlogin whenever possible.
  • ftp hostname --- lets you download files from a remote host which is set up as an ftp-server. The most important commands within ftp are get for getting files from the remote machine, and put for putting them there (mget and mput let you specify more than one file at once). Be sure not to confuse the two, especially when your physical location doesn't correspond to the direction of the ftp connection you're making. ftp just overwrites files with the same filename. If you're transferring anything other than ASCII text, use binary mode.
  • lynx --- lets you browse the web from an ordinary terminal.

Miscellaneous tools

  • date --- shows the current date and time.
  • cal --- shows a calendar of the current month. Use e.g., 'cal 10 1995' to get that for October 95, or 'cal 1995' to get the whole year.


Nohup command.
nohup command if added in front of any command will continue running the command or process even if you shut down your terminal or close your session to machine. For example, if I want to run a job that takes lot of time and must be run from terminal and is called update_entries_tonight .


Ln command.
Instead of copying you can also make links to existing files using ln command.
If you want to create a link to a file called coolfile in /usr/local/bin directory then you can enter this command.
ln mycoolfile /usr/local/bin/coolfile
Some examples:
ln -s fileone filetwo will create a symbolic link and can exist across machines.
ln -n option will not overwrite existing files.
ln -f will force the link to occur.

Diff command.
diff command will compare the two files and print out the differences between.

Cmp command.
cmp command compares the two files. For exmaple I have two different files fileone and filetwo.
cmp fileone filetwo will give me
fileone filetwo differ: char 80, line 4
if I run cmp command on similar files nothing is returned.
-s command can be used to return exit codes. i.e. return 0 if files are identical, 1 if files are different, 2 if files are inaccessible.
This following command prints a message 'no changes' if files are same
cmp -s fileone file1 && echo 'no changes' .

Find command.
Find command is a extremely useful command. you can search for any file anywhere using this command provided that file and directory you are searching has read write attributes set to you ,your, group or all. Find descends directory tree beginning at each pathname and finds the files that meet the specified conditions. Here are some examples.

Some Examples:
find $HOME -print will lists all files in your home directory.
find /work -name chapter1 -print will list all files named chapter1 in /work directory.
find / -type d -name 'man*' -print will list all manpage directories.
find / -size 0 -ok rm {} \; will remove all empty files on system.
conditions of find
-atime +n |-n| n will find files that were last accessed more than n or less than -n days or n days.
-ctime +n or -n will find that were changed +n -n or n days ago.
-depth descend the directory structure, working on actual files first and then directories. You can use it with cpio command.
-exec commad {} \; run the Unix command on each file matched by find. Very useful condition.
-print print or list to standard output (screen).
-name pattern find the pattern.
-perm nnnfind files whole permission flags match octal number nnn.
-size n find files that contain n blocks.
-type c Find file whole type is c. C could be b or block, c Character special file, d directory, p fifo or named pipe, l symbolic link, or f plain file.

Sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents
zzz
aaa
1234
yuer
wer
qww
wwe
Then running
sort testfile
will give us output of
1234
aaa
qww
wer
wwe
yuer
zzz
Options:
-b ignores leading spaces and tabs.
-c checks whether files are already sorted.
-d ignores punctuation.
-i ignores non-printing characters.
-n sorts in arithmetic order.
-ofile put output in a file.
+m[-m] skips n fields before sorting, and sort upto field position m.
-r reverse the order of sort.
-u identical lines in input file apear only one time in output.

Uniq command.
uniq command removes duplicate adjacent lines from sorted file while sending one copy of each second file.
Examples

sort names | uniq -d will show which lines appear more than once in names file.
Options:
-c print each line once, counting instances of each.
-d print duplicate lines once, but no unique lines.
-u print only unique lines.

Sed command.
sed command launches a stream line editor which you can use at command line.
you can enter your sed commands in a file and then using -f option edit your text file. It works as
sed [options] files
options:
-e 'instruction' Apply the editing instruction to the files.
-f script Apply the set of instructions from the editing script.
-n suppress default output.

Df command.
df command displays information about mounted filesystems. It reports the number of free disk blocks. Typically a Disk block is 512 bytes (or 1/2 Kilobyte).
syntax is
df options name
Options
-b will print only the number of free blocks.
-e will print only the number of free files.
-f will report free blocks but not free inodes.
-F type will report on an umounted file system specified by type.
-k will print allocation in kilobytes.
-l will report only on local file systems.
-n will print only the file system name type, with no arguments it lists type of all filesystems

Du command.
du command displays disk usage.

Touch – is used to change a file's access and modification timestamps. It is also used to create a new empty file.
-a, change the access time only
-c, if the file does not exist, do not create it and do not report this condition
-m, change the modification time only
-r file, use the access and modification times of file
-t time, use the time specified (in the format below) to update the access and modification times
The simplest use case for touch is thus:
# touch myfile.txt

Alias - It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. An alias will last for the life of the shell session. Regularly used aliases can be set from the shell's configuration so that they will be available upon the start of the corresponding shell session.
Syntax
alias [name=['command']]
An example of the Bash shell syntax is:
alias copy="cp"
To view defined aliases the following commands can be used:
alias # Used without arguments; displays a list of all current aliases
alias -p # Analogous to the above; not available in 4DOS/4NT and PowerShell
alias myAlias # Displays the command for a defined alias
In Unix shells, if an alias exists for a command, it is possible to override the alias by surrounding the command with quotes or prefixing it with a backslash. For example, consider the following alias definition:
alias ls='ls -la'
To override this alias and execute the ls command as it was originally defined, the following syntax can be used:
'ls' or \ls
In Unix shells and 4DOS/4NT, aliases can be removed by executing the unalias command:
unalias copy # Removes the copy alias
unalias -a # The -a switch will remove all aliases; not available in 4DOS/4NT
unalias * # 4DOS/4NT equivalent of `unalias -a` - wildcards are supported

Tee - tee is normally used to split the output of a program so that it can be seen on the display and also be saved in a file. The command can also be used to capture intermediate output before the data is altered by another command or program. The tee command reads standard input, then writes its content to standard output and simultaneously copies it into the specified file(s) or variables.

The grep command allows you to search one file or multiple files for lines that contain a pattern. Exit status is 0 if matches were found, 1 if no matches were found, and 2 if errors occurred.
The syntax for the grep command is:
grep [options] pattern [files]
options:
-b
Display the block number at the beginning of each line.
-c
Display the number of matched lines.
-h
Display the matched lines, but do not display the filenames.
-i
Ignore case sensitivity.
-l
Display the filenames, but do not display the matched lines.
-n
Display the matched lines and their line numbers.
-s
Silent mode.
-v
Display all lines that do NOT match.
-w
Match whole word.
Examples:
grep -c tech file1

Some one Liners of Unix
  1. How do you find out what’s your shell? - echo $SHELL
  2. What’s the command to find out today’s date? - date
  3. What’s the command to find out users on the system? - who
  4. How do you find out the current directory you’re in? - pwd
  5. How do you remove a file? - rm
  6. How do you find out your own username? - whoami
  7. How do you count words, lines and characters in a file? – wc –l or –w or -c
  8. How do you search for a string inside a given file? - grep string filename
  9. How do you search for a string inside a directory? - grep string *
  10. How do you search for a string in a directory with the subdirectories recursed? - grep -r string *
  11. What are PIDs? - They are process IDs given to processes. A PID can vary from 0 to 65535.
  12. How do you list currently running process? - ps
  13. How do you stop a process? - kill pid
  14. How do you find out about all running processes? - ps –ae : a for all user process, e for system process
  15. How do you stop all the processes, except the shell window? - kill 0
  16. How do you fire a process in the background? - ./process-name &
  17. How do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is your script name.
  18. What’s the conditional statement in shell scripting? - if {condition} then … fi
  19. How do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge
  20. How do you test for file properties in shell scripts? - -s filename tells you if the file is not empty, -f filename tells you whether the argument is a file, and not a directory, -d filename tests if the argument is a directory, and not a file, -w filename tests for writeability, -r filename tests for readability, -x filename tests for executability
  21. How do you do Boolean logic operators in shell scripting? - ! tests for logical not, -a tests for logical and, and -o tests for logical or.
  22. How do you find out the number of arguments passed to the shell script? - $#
  23. What’s a way to do multilevel if-else’s in shell scripting? - if {condition} then {statement} elif {condition} {statement} fi
  24. How do you write a for loop in shell? - for {variable name} in {list} do {statement} done
  25. How do you write a while loop in shell? - while {condition} do {statement} done
  26. How does a case statement look in shell scripts? - case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac
  27. How do you read keyboard input in shell scripts? - read {variable-name}
  28. How do you define a function in a shell script? - function-name() { #some code here return }
  29. How does getopts command work? - The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts n:x option, and the variable $option contains the value of the entered option.
Q. State the advantages of Shell scripting?
There are many advantages of shell scripting some of them are, one can develop their own operating system with relevant features best suited to their organization than to rely on costly operating systems. Software applications can be designed according to their platform.
Q. What are the disadvantages of shell scripting?
There are many disadvantages of shell scripting they are
* Design flaws can destroy the entire process and could prove a costly error.
* Typing errors during the creation can delete the entire data as well as partition data.
* Initially process is slow but can be improved.
* Portbility between different operating system is a prime concern as it is very difficult to port scripts etc.
Q. Explain about the slow execution speed of shells?
Major disadvantage of using shell scripting is slow execution of the scripts. This is because for every command a new process needs to be started. This slow down can be resolved by using pipeline and filter commands.
Q. Give some situations where typing error can destroy a program?
There are many situations where typing errors can prove to be a real costly effort. For example a single extra space can convert the functionality of the program from deleting the sub directories to files deletion. cp, cn, cd all resemble the same but their actual functioning is different. Misdirected > can delete your data.
Q. Explain about Stdin, Stdout and Stderr?
These are known as standard input, output and error. These are categorized as 0, 1 and 2. Each of these functions has a particular role and should accordingly functions for efficient output. Any mismatch among these three could result in a major failure of the shell.
Q. Explain about sourcing commands?
Sourcing commands help you to execute the scripts within the scripts. For example sh command makes your program to run as a separate shell. .command makes your program to run within the shell.
Q. Explain about Login shell?
Login shell creates an environment and default parameters. It consists of two files they are profile files and shell rc files. These files initialize the login and non login files. Environment variables are created by Login shell.
Q. Explain about non-login shell files?
The non login shell files are initialized at the start and they are made to run to set up variables. Parameters and path can be set etc are some important functions. These files can be changed and also your own environment can be set. These functions are present in the root. It runs the profile each time you start the process.
Q. Explore about Environment variables?
Environment variables are set at the login time and every shell that starts from this shell gets a copy of the variable. When we export the variable it changes from an shell variable to an environment variable and these variables are initiated at the start of the shell.
Q. What type of variables are in shell?
There are two types of variable:
(1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.
(2) User defined variables (UDV) - Created and maintained by user.

System Variable
Meaning
BASH=/bin/bash
Our shell name
BASH_VERSION=1.14.7(1)
Our shell version name
COLUMNS=80
No. of columns for our screen
HOME=/home/vivek
Our home directory
LINES=25
No. of columns for our screen
LOGNAME=students
students Our logging name
OSTYPE=Linux
Our Os type
PATH=/usr/bin:/sbin:/bin:/usr/sbin
Our path settings
PS1=[\u@\h \W]\$
Our prompt settings
PWD=/home/students/Common
Our current working directory
SHELL=/bin/bash
Our shell name
USERNAME=vivek
User name who is currently login to this PC

Syntax:
variable name=value
Example:
$ n=10
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character.
(2) Variables are case-sensitive, just like filename in Linux.
(4) You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) For e.g.
$ vech=
$ vech=""

To print or access UDV use following syntax
Syntax:
$variablename
Define variable vech as follows:
$ vech=Bus
To print contains of variable 'vech' type
$ echo $vech
Q. How to perform arithmetic in shell?
Use to perform arithmetic operations.
Syntax:
expr op1 math-operator op2

Examples:
$ echo `expr 6 + 3`
For the last statement not the following points
(1) First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ') sign. Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above of TAB key.
(2) Second, expr is also end with ` i.e. back quote.
(3) Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum
(4) Here if you use double quote or single quote, it will NOT work
Q. How to read values from user?
Syntax:
read variable1, variable2,...variableN
Example:
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"

Q. How can we write more than one command in one line?
Syntax:
command1;command2
Examples:
$ date;who
Will print today's date followed by users who are currently login. Note that You can't use

Q. test command or [expr]?

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]

Example:
#!/bin/sh
if test $1 -gt 0
then
echo "$1 number is positive"
fi

Shell also test for file and directory types
Test
Meaning
-s file
Non empty file
-f file
Is File exist or normal file and not a directory
-d dir
Is Directory exist and not a file
-w file
Is writeable file
-r file
Is read-only file
-x file
Is file is executable

Q. if else syntax ;
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
elif

else
if condition is not true then
execute all commands up to fi
fi

Example :
$ vi isnump_n
#!/bin/sh
#
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi

if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi

Q. for loop in shell?
Syntax:
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done

Example:
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

Even you can use following syntax:
Syntax:
for (( expr1; expr2; expr3 ))
do
.....
...
repeat all statements between do and
done until expr2 is TRUE
Done

Example:
for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done

Q. While loop in shell?
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
Done
Example:
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done

Q. How to debug shell script?
For this purpose you can use -v and -x option with sh or bash command to debug the shell script. General syntax is as follows:
Syntax:
sh option { shell-script-name }
OR
bash option { shell-script-name }
Option can be
-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments.

Q. What is test command in unix shell?
The unix test command can test for various conditions, and then returns 0 for true and 1 for false. Usually, test is used in if...then or while loops.

Let start with a simple unix test or if examples for comparing two number equal or not
if [ 9 -eq 9 ]
then
echo equal
else
echo no equal
fi

We can do the same thing by using test command as
test 9 -eq 9
if [ $? -eq 0 ]
then
echo equal
else
echo no equal
fi

Q. What is /dev/null in unix?

This is special Linux file which is used to send any unwanted output from program/command.
Syntax:
command > /dev/null
Example:
$ ls > /dev/null
Output of above command is not shown on screen its send to this special file. The /dev directory contains other device files. The files in this directory mostly represent peripheral devices such disks like floppy disk, sound card, line printers etc
Q. What is conditional execution i.e. && and || in shell?

The control operators are && (read as AND) and || (read as OR). The syntax for AND list is as follows
Syntax:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero.
The syntax for OR list as follows
Syntax:
command1 || command2
command2 is executed if and only if command1 returns a non-zero exit status.

You can use both as follows
Syntax:
command1 && comamnd2 if exist status is zero || command3 if exit status is non-zero
if command1 is executed successfully then shell will run command2 and if command1 is not successful then command3 is executed.
Example:
$ rm myf && echo "File is removed successfully" || echo "File is not removed"
If file (myf) is removed successful (exist status is zero) then "echo File is removed successfully" statement is executed, otherwise "echo File is not removed" statement is executed (since exist status is non-zero)
Q. how to write functions in shell?

Syntax:
function-name ( )
{
command1
command2
.....
...
commandN
return
}
Where function-name is name of you function, that executes series of commands. A return statement will terminate the function. Example:
Type SayHello() at $ prompt as follows
$ SayHello()
{
echo "Hello $LOGNAME, Have nice computing"
return
}

Arithmetic Operations in Shell Scripting

For arithmetic operations such as addition, subtraction, multiplication, and division. We uses a function called expr; e.g., "expr a + b" means 'add a and b'.
e.g.:
sum=`expr 12 + 20`
Similar syntax can be used for subtraction, division, and multiplication.
There is another way to handle arithmetic operations; enclose the variables and the equation inside a square-bracket expression starting with a "$" sign. The syntax is
$[expression operation statement]
e.g.:
echo $[12 + 10]

Conditional Statements

The syntax is show below:
if [ conditional statement ]
then
... Any commands/statements ...
fi

The script cited below will prompt for a username, and if the user name is "blessen", will display a message showing that I have successfully logged in. Otherwise it will display the message "wrong username".
#!/bin/sh
echo "Enter your username:"
read username
if [ "$username" = "blessen" ]
then
echo 'Success!!! You are now logged in.'
else
echo 'Sorry, wrong username.'
Fi

Remember to always enclose the variable being tested in double quotes; not doing so will cause your script to fail due to incorrect syntax when the variable is empty. Also, the square brackets (which are an alias for the 'test' command) must have a space following the opening bracket and preceding the closing one.

Variable Comparison

If the values of variables to be compared are numerical, then you have to use these options:
-eq Equal to
-ne Not Equal to
-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater then or equal to

If they are strings, then you have to use these options:
= Equal to
!= Not Equal to
< First string sorts before second
> First string sorts after second

Loops

The "for" Loop

The most commonly used loop is the "for" loop. In shell scripting, there are two types: one that is similar to C's "for" loop, and an iterator (list processing) loop.
Syntax for the first type of "for" loop (again, this type is only available in modern shells):
for ((initialization; condition; increment/decrement))
do
...statements...
done

Example:
#!/bin/sh
for (( i=1; $i <= 10; i++ ))
do
echo $i
done

This will produce a list of numbers from 1 to 10. The syntax for the second, more widely-available, type of "for" loop is:
for <variable> in <list>
do
...statements...
done

This script will read the contents of '/etc/group' and display each line, one at a time:
#!/bin/sh
count=0
for i in `cat /etc/group`
do
count=`expr "$count" + 1`
echo "Line $count is being displayed"
echo $i
done
echo "End of file"

Another example of the "for" loop uses "seq" to generate a sequence:
#!/bin/sh
for i in `seq 1 5`
do
echo $i
done

While Loop

The "while" loop is another useful loop used in all programming languages; it will continue to execute until the condition specified becomes false.
while [ condition ]
do
...statement...
done

The following script assigns the value "1" to the variable num and adds one to the value of num each time it goes around the loop, as long as the value of num is less than 5.

#!/bin/sh
num=1
while [$num -lt 5]; do num=$[$num + 1]; echo $num; done

Select and Case Statement

Similar to the "switch/case" construct in C programming, the combination of "select" and "case" provides shell programmers with the same features. The "select" statement is not part of the "case" statement, but I've put the two of them together to illustrate how both can be used in programming.
Syntax of select:
select <variable> in <list>
do
...statements...
Done

Syntax of case:
case $<variable> in
<option1>) statements ;;
<option2>) statements ;;
*) echo "Sorry, wrong option" ;;
esac

The example below will explain the usage of select and case together, and display options involving a machine's services needing to be restarted. When the user selects a particular option, the script starts the corresponding service.

#!/bin/bash
echo "***********************"
select opt in apache named sendmail
do
case $opt in
apache) /etc/rc.d/init.d/httpd restart;;
named) /etc/rc.d/init.d/named restart;;
sendmail) /etc/rc.d/init.d/sendmail restart;;
*) echo "Nothing will be restarted"
esac
echo "***********************"

# If this break is not here, then we won't get a shell prompt.
break

done

[ Rather than using an explicit 'break' statement - which is not useful if you want to execute more than one of the presented options - it is much better to include 'Quit' as the last option in the select list, along with a matching case statement. -- Ben ]

Functions

In the modern world where all programmers use the OOP model for programming, even we shell programmers aren't far behind. We too can break our code into small chunks called functions, and call them by name in the main program. This approach helps in debugging, code re-usability, etc.
Syntax for "function" is:
<name of function> ()
{ # start of function
statements
} # end of function
Functions are invoked by citing their names in the main program, optionally followed by arguments. For example:
#!/bin/sh
sumcalc ()
{
sum=$[$1 + $2]
}

echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2

sumcalc $num1 $num2

echo "Output from function sumcalc: $sum"

Debugging Shell Scripts

Now and then, we need to debug our programs, we use the '-x' and '-v' options of the shell. The '-v' option produces verbose output. The '-x' option will expand each simple command, "for" command, "case" command, "select" command, or arithmetic "for" command, displaying the expanded value of PS4, followed by the command and its expanded arguments or associated word list. Try them in that order - they can be very helpful when you can't figure out the location of a problem in your script.
Shell Variable:

1. The PATH variable

One of the more important variables is the path variable. The path variable controls where the shell searches for commands, when you type them to at the prompt.

2. HOME
This is the home directory of your account. It is also used as the default when typing cd without any arguments.
3. PS1
The primary prompt string, normally set to `$ '. This is what the computer prints whenever it is ready to process another command.
4. PS2
The secondary prompt string, normally set to `> '. The shell prints this prompt whenever you have type an incomplete line, e.g., if you are missing a closing quote. For example:
$ echo 'hello
> world'
hello
world
5. SHELL
This variables give the path of the shell you are currently executing, e.g., if you are executing the standard Bourne shell, the SHELL variables is usually set to /bin/sh.
6. IFS
This is set to the internal field separators, normally set to space, tab, and blank. We will talk more about this later.

One-liners

Here are some one-liners that might come in handy some time. These one-liners serve both as educational examples that solves "real problems". If you have suggestions for more one-liners, please mail me.

Renaming several files at the same time

If you have a number of files named foo.C, bar.C.gz, etc. and want to rename them to foo.cc, bar.cc.gz, etc. This line will do the trick.
\ls *.C* | sed 's/\(.*\).C\(.*\)/mv & \1.cc\2/' | sh
The backslash before the ls command is to prevent is from being expanded, in the case that is is an alias and you are using shell that has aliases (such as Bash). We want to prevent the shell from doing this expansion since ls might come out as ls -F (which would behave strange) or ls -l which is really bad.
An alternative is to install the rename script, which is written in Perl.

Remove processes matching some regular expression

If you have a number of processes that you want to kill, one of the following one-liners might be useful:
kill `ps xww | grep "sleep" | cut -c1-5` 2>/dev/null
ps xww | grep "sleep" | cut -c1-5 | xargs kill 2>/dev/null
This will kill any processes that has the word "sleep" in the calling command. If your kill does not handle multiple pids' you can either use the one-liner
ps xww | grep "sleep" | cut -c1-5 | xargs -i kill {} 2>/dev/null
or use a for-loop:
for x in `ps xww | grep "sleep" | cut -c1-5`
do
kill $x 2>/dev/null
done
But then it is not a one-liner any more.
Note! Be very careful about what you use as expression to grep. You might get more processes than you wanted killed.

20 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home