Month: September 2011
If given condition is true then command1 is executed otherwise command2 is executed.
if condition then condition is zero (true - 0) execute all commands up to else statement else if condition is not true then execute all commands up to fi fi
example of nested if else
echo "1. Unix (Sun Os)" echo "2. Linux (Red Hat)" echo -n "Select your os choice [1 or 2]? " read osch if [ $osch -eq 1 ] ; then echo "You Pick up Unix (Sun Os)" else #### nested if i.e. if within if ###### if [ $osch -eq 2 ] ; then echo "You Pick up Linux (Red Hat)" else echo "What you don't like Unix/Linux OS." fi fi
if condition which is used for decision making in shell script, If given condition is true then command1 is executed.
command1 if condition is true or if exit status of condition is 0 (zero)
A pipe is a way to connect the output of one program to the input of another program without any temporary file.
command1 | command2
eg: who | wc -l
Output of who command is given as input to wc command So that it will number of user who logon to system
Command line arguements
$ myshell foo bar
Shell Script name i.e. myshell
First command line argument passed to myshell i.e. foo
Second command line argument passed to myshell i.e. bar
In shell if we wish to refer this command line argument we refer above as follows
myshell it is $0
foo it is $1
bar it is $2
Here $# (built in shell variable ) will be 2 (Since foo and bar only two Arguments), Please note at a time such 9 arguments can be used from $1..$9, You can also refer all of them by using $* (which expand to `$1,$2…$9`). Note that $1..$9 i.e command line arguments to shell script is know as “positional parameters“.
1. * – Matches any string or group of characters.
2. ? – Matches any single character.
3. […] – Matches any one of the enclosed characters
[..-..] A pair of characters separated by a minus sign denotes a range.
The read statement
Use to get input (data from user) from keyboard and store (data) to variable.
read variable1, variable2,…variableN
echo “Enter your name please”
echo “Welcome $fname”
By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.
(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.
This value is know as Exit Status.
exit status can be found out by issuing $ echo $?
if the output is 0, the previously executed command is successful if it’s a non zero value means the last command run is unsuccessful.
Quotes in scripts
Mainly three types of quotes are used.
1. ” – Double quotes: Anything enclose in double quotes removed meaning of that characters (except and $).
2. ‘ – Single quotes: Enclosed in single quotes remains unchanged.
3. ` – Back quotes: To execute command
used to perform arithmetic operations in shell scripts.
Synatax: $ expr op1 operator op2
$ expr 1 + 3
$ expr 2 – 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 * 3
$ echo `expr 6 + 3`
in the last one we used back quote (` the one below the tilde symbol in standard keyboards).
if we use the back quote the last eg’s output may differ.