Linux Shell Script Command Summary

Shell Script Command Summary

Numeric Comparisons File Comparisons String Comparisons Expression Comparisons
-eq int1 == int2
-ge int1 >= int2
-gt int1  >  int2
-le int1 <= int2
-lt int1  <  int2
-ne int1 !=  int2
-d is Exist & is Directory
-f is Exist & is File
-s is Exist & is non-Zero
-r is Readable by process
-w is Writable by process
-x is Executable by process
str1 = = str2 is Identical
str1 ! = str2 is Not Identical
str is Not Null
-n str is Non-Zero Length
-z str is Zero Length
! expr is Not true
expr1  -a expr2 is Both true
expr1 && expr2 is Both true
expr1  -o expr2 is Either true
expr1   ||  expr2 is Either true

test If...then If..then...else If..then...elif...else Case select While loop Until loop For loop Function
test expression
Or
[ expression ]
if [ expression ]
then
commands
fi
Or
if [ expression ]; then
commands
fi
if [ expression ] then
commands
else
commands
fi
if [ expression ]; then
commands
elif [ expression2 ]; then
commands
else
commands
fi
case string1 in
str1)
commands;;
str2)
commands;;
*)
commands;;
esac
while [ expression ]
do
commands
done
until [ expression ]
do
commands
done
for var1 in list
do
commands
done
fname(){
commands
}
Call by:
fname2 (arg1,arg2...argN){
commands
}


Variables Quote Marks
$# : contains No_of_argments in command line
$0 : program_name itself as in command line
$1 : argument 1
$2 : argument 2
$* Stores all the arguments that were entered on the command line ($1 $2 ...).
"$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...).
$? Stores the exit value of the last command that was executed.

Create a variable: varname=value, Access a variable: echo $varname

"Double quotes" : ignore whitespace and count all as one argument being passed or string to use. Special characters inside are still noticed/obeyed.

'Single quotes' : ignore all special characters in whatever string is being passed.

`Back single quote` : perform a different function; use results of a command in another command. For example, if you wanted to set the value of the variable contents equal to the list of files in the current directory, you would type the following command: contents=`ls`, the results of the ls program are put in the variable contents.

Shell Script Examples

Sample1 (Clone disk) Sample2 (Cisco up/download) Sample3 (NFS backup)

Labels: cheatsheet, linux.