Shell scripting tutorials
Doing an Arithmetic :
i=$(( i * 5 ))
Single Quotes:
Ignoring special Characters : echo ‘*’ [ anything inside single quotes is ignored ]
Double Quotes:
Same as single quotes but not restrictive.
The BackSlash:
Equivalent to single quotes around a single character. Backslash quotes the single character
$(…) Construct :
$(command) is same as command
and is better option.
eg. echo Date is :$(date)
\ : use backslash \ to continue lines
**$# variable : **
$# gets set to the number of arguments that were typed:
$ variable :*
$* variable references all the arguments passed to the program.
${n} :
Arguments greater than 9 cannot be obtained by $10, so you need to get it by ${10}.
Shift Command :
Shift command allows to effectively left shift positional parameters. When you execute shift , whatever was assigned to $2 will be assigned to $1, $3 is assigned to $2 and the value of $1 will be lost.
eg. Usage :
echo $# $*<br></br>
shift<br></br>
echo $# $*<br></br>
shift```
**Exit Status:**
Exit status of a command is obtained by $? variable . 0 implies success and 1 implies failed.
**Test Command:**
Test command is used to test one or more conditions in an if command. Test evaluates expression and if result is true it returns an exit status of 0 and non zero if result is false.
syntax : test expression.
**String Operators:**
string1 = string2 : string 1 is identical to string 2
string1 != string2 : string 1 is not identical to string2
string : string is not null
-n string : string is not null
-z string : string is null```
Integer Operators:
x -eq y : x is equal to y<br></br>
x -ge y : x is greater than or equal to y<br></br>
x -gt y : x is greater than y<br></br>
x -le y : x is less than or equal to y<br></br>
x -lt y : x is less than y<br></br>
x -ne y : x is not equal to y```
**File Operators : **
-d file : file is a directory
-e file : file exists
-f file : file is an ordinary file
-r file : file is readable by the process
-s file : file has a nonzero length
-w file : file is writable by the process
-x file : file is executable
-L file : file is a symbolic link```
Logical Operators :
Logical negation Operator : ! : can be placed in front of any other test expression.
Logical And Operator : -a
Parantheses: used to alter the order of evaluation.
Logical OR operator : -o
**IF Else : **
if command<br></br>
then<br></br>
command<br></br>
else<br></br>
command<br></br>
fi```
ELIF:
if command
then
command
command
elif command
command
command
fi```
Case Command:
case value in<br></br>
pattern1)<br></br>
commands..<br></br>
;;<br></br>
pattern2)<br></br>
commands..<br></br>
;;<br></br>
pattern3 | pattern4 )<br></br>
commands..<br></br>
;;<br></br>
esac```
**Exit Command :** Exit enables to immediately terminate execution of program:
Syntax: exit n
Where n is the exit status that is to be returned.
**
Tips and Tricks : **
**
Checking if argument is passed : **
if [ "$#" -ne 1 ]
then
echo "Usage: usage message"
fi```
..
**
Using -x for debugging:**
sh -x script.sh
**The Null Command : **
:
if expression
then
:
else
…
fi
**
The && and || Constructs:**
command1 && command2 : Execute command2 only if command1 is successful.
eg.
if [ -z "$var" ] && var = "value"
command1 || command2 : Command 2 is executed even if command1 fails.
eg.
if test || test2<br></br>
then<br></br>
ok<br></br>
fi```
if test is successful ok is executed otherwise test2 is executed.
**Check if a variable is set :**
if [[ -z "$1" ]]