Documente Academic
Documente Profesional
Documente Cultură
1
TOPICS
1
File Handling.....................................................................................................................25
Array..................................................................................................................................26
1
Benefits of Shell Programming
Process
Variables
Variables are how programming and scripting languages represent data. A variable is
nothing more than a label, a name
Assigned to a location or set of locations in computer memory holding an item of data.
Variables appear in arithmetic operations and manipulation of quantities, and in string
parsing.
# variable1=23
# echo variable1
variable1
1
# echo $variable1
23
Environment variables
env
assign environment variables
echo $PATH
export PATH="$PATH:/usr"
echo $PATH
$0, $1, $2, etc. - Positional parameters, passed from command line to script, passed to a
function, or set to a variable
$# - Number of command-line arguments [4] or positional parameters (see Example 33-
2)
$* - All of the positional parameters, seen as a single word "$*" must be quoted.
$@ - Same as $*, but each parameter is a quoted string, that is, the parameters are passed
on intact, without interpretation
or expansion. This means, among other things, that each parameter in the argument list is
seen as a separate word.
#
##
###
#!/bin/bash
echo The name of this script is $0
echo 1st parameter Passed is $1
echo 2nd parameter Passed is $2
echo 3rd parameter Passed is $3
echo 4th parameter Passed is $4
echo 5th parameter Passed is $5
echo 6th parameter Passed is $6
echo 7th parameter Passed is $7
echo 8th parameter Passed is $8
echo 9th parameter Passed is $9
1
echo 10th parameter Passed is $10
echo All the command-line parameters are: $*
echo No of Argument passed is $#
echo No of Argument passed is $@
# bash a.sh 11 12 13 14 15 16 17 18 19 20
#
##
###
grep '[Ff]irst' *.txt
echo $(ls -l)
echo "$(ls -l)"
#
##
###
echo a
echo "a"
echo \"a\"
Exit codes
Exit codes are a number between 0 and 255, which is returned by any Unix command
when it returns control to its parent
process.
Success is traditionally represented with exit 0
failure is normally indicated with a non-zero exit-code, This value can indicate different
reasons for failure.
For example, GNU grep returns 0 on success, 1 if no matches were found, and 2 for other
errors (syntax errors, nonexistent
input files, etc).
#
##
###
ls -l
echo $?
ls -
1
echo $?
#
##
###
#\!/bin/bash
echo test
exit 0
# bash a.sh
echo $?
#
##
###
echo "#\!/bin/bash"
echo "test"
echo "exit 2"
# bash a.sh
echo $?
Escape characters
\n - means newline
\r - means return
\t - means tab
\v - means vertical tab
\b - means backspace
\a - means alert (beep or flash)
\0xx - translates to the octal ASCII equivalent of 0nn, where nn is a string of digits
#
##
###
echo "HI"
echo "\nHI"
echo -e "\nHI"
kill
#
#
##
1
#!/bin/bash
kill $$
echo This line will not echo.
exit 0
# bash a.sh
CUT
AWK
SED
SEQ
seq 5
seq -s : 5
Redirection
>
>>
1>
2>
> a.sh
cat /dev/null > a.sh
1
cat a > a.sh
ls 1> a
lss 1> a
lss 2> a
Debug Mode
-e - file exists
-f - file is a regular file (not a directory or device file)
-s - file is not zero size
-d - file is a directory
-b - file is a block device
-c - file is a character device
-p - file is a pipe
-h - file is a symbolic link
-L - file is a symbolic link
-S - file is a socket
-t - file (descriptor) is associated with a terminal device
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given
script is a terminal.
-r - file has read permission (for the user running the test)
-w - file has write permission (for the user running the test)
-x - file has executed permission (for the user running the test)
-g - set-group-id (sgid) flag set on file or directory
If a directory has the sgid flag set, then a file created within that directory belongs to the
group that owns the directory,
1
not necessarily to the group of the user who created the file. This may be useful for a
directory shared by a workgroup.
-u - set-user-id (suid) flag set on file
-k - sticky bit set
-O - you are owner of file
-G - group-id of file same as yours
-N - file modified since it was last read
f1 -nt f2 - file f1 is newer than f2
f1 -ot f2 - file f1 is older than f2
f1 -ef f2 - files f1 and f2 are hard links to the same file
! - "not" -- reverses the sense of the tests above (returns true if condition absent).
#
##
###
#!/bin/bash
echo Enter the Absolute File name you want to check;
read file
echo File you entered is
if test -e
then
echo File Exists;
else
echo File not Exists;
fi
bash a.sh
Enter file name
#
##
###
#!/bin/bash
echo Enter the Absolute Directory name you want to check;
read Directory
echo Directory you entered is $Directory
if test -d $Directory
then
echo Directory Exists;
else
echo Directory not Exists;
fi
# bash a.sh
Enter Directory name
1
#
##
###
#!/bin/bash
echo Enter the Absolute File name you want to check;
read File
echo File you entered is $File
if test -k $File
then
echo Sticky Bit Set To File;
else
echo Sticky Bit Not Set To File;
fi
# chmod o+t a
# bash a.sh
Comparison Operators
String Arithmetic
== -eq
> -gt
< -lt
>= -ge
<= -le
!= -ne
String Comparison
#
##
###
#!/bin/bash
echo Enter String1
read str1
echo enter String2
read str2
if test "$str1" == "$str2"
then
echo Both Strings are Same;
1
else
echo Both Strings are Differ;
fi
# bash a.sh
You can Put big string here for compare.
compound comparison
-o - logical or ||
exp1 -o exp2 returns true if either exp1 or exp2 is true
#
##
###
#!/bin/bash
echo Enter val1
read val1
echo enter val2
read val2
# This condition will pass for val1=100 AND val2=101 OR val1=200 AND val2=201
if test $val1 -eq 100 -a $val2 -eq 101 -o $val1 -eq 200 -a $val2 -eq 201
then
echo PASS
else
echo FAIL
fi
# bash a.sh
100-101 200-201 then 202 204
Arithmetic operators
+ - plus
- - minus
* - multiplication
/ - division
** - exponentiation
% - modulo, or mod (returns the remainder of an integer division operation)
+= - plus-equal (increment variable by a constant)
-= - minus-equal (decrement variable by a constant)
*= - times-equal (multiply variable by a constant)
1
let "var *= 4" results in var being multiplied by 4.
/= - slash-equal (divide variable by a constant)
%= - mod-equal (remainder of dividing variable by a constant)
EXPR
#
##
###
#\!/bin/bash
Total_Mem=3042
Used_Mem=3002
Free_Mem=40
echo Total_Mem is $Total_Mem;
echo Used_Mem is $Used_Mem;
echo Free_Mem is $Free_Mem;
Per_Free_Mem=`expr $Free_Mem \* 100`
Per_Free_Mem=`expr $Per_Free_Mem / $Total_Mem`
Per_Used_Mem=`expr 100 - $Per_Free_Mem`
echo Per_Free_Mem is $Per_Free_Mem;
echo Per_Used_Mem is $Per_Used_Mem;
# bash a.sh
This will give used and free memory on server in percentage
Modulo example
expr 5 % 3
expr 10 % 3
expr 12 % 3
Example of let
#
##
###
#!/bin/bash
a=2147483646
1
echo a = $a
let a+=1
echo a = $a
let a+=1
echo a = $a
# bash a.sh
Operators
Bitwise operators
! - NOT
&& - AND
|| - OR
Miscellaneous operators
, - Comma operator
#!/bin/bash
let "t1 = ((a = 5 + 3, b = 7 - 1, 15 - 4))"
echo a = $a
echo b = $b
1
echo t1 = $t1
# Here t1 is set to the result of the last operation.
let "t2 = ((a = 9, 15 / 3))"
echo t2 = $t2 a = $a
# bash a.sh
Here by default t1 will take value of last expression.
Shift Operator
#
##
###
#!/bin/bash
echo $@
shift
echo $@
shift
echo $@
shift
echo $@
shift
echo $@
# bash a.sh 1 2 3 4 5
The break and continue loop control commands [1] correspond exactly to their
counterparts in other programming languages. The
break command terminates the loop (breaks out of it), while continue causes a jump to
the next iteration of the loop,
skipping all the remaining commands in that particular loop cycle
The continue command, similar to break, optionally takes a parameter. A plain continue
cuts short the current iteration
within its loop and begins the next. A continue N terminates all remaining iterations at its
loop level and continues with
the next iteration at the loop, N levels above.
1
##
###
#\!/bin/bash
LIMIT=19
echo Printing Numbers 1 through 20 but not 3 and 11
a=0
while [ $a -le $LIMIT ]
do
a=$(($a+1))
if [ $a -eq 3 ] || [ $a -eq 11 ]
then
continue
fi
echo -n $a
done
echo Printing Numbers 1 through 20, but something happens after 2.
a=0
while [ $a -le $LIMIT ]
do
a=$(($a+1))
if [ $a -gt 2 ]
then
break
fi
echo -n $a
done
exit 0
# bash a.sh
IF Statement
#
##
###
echo "This is file a" > a
echo "This is file b" > b
#!/bin/bash
if cmp a b &> /dev/null
then echo Files a and b are identical
else echo Files a and b differ
fi
1
# bash a.sh
#
##
###
echo "This is file a" > a
echo "This is file a" > b
#!/bin/bash
if cmp a b &> /dev/null
then echo Files a and b are identical
else echo Files a and b differ
fi
bash a.sh
if else
#
##
###
#!/bin/bash
a=5
if test $a -eq 5
then
echo Value of a is 5;
else
echo Value of a is not 5;
fi
# bash a.sh
#
##
###
#!/bin/bash
a=6
if test $a -eq 5
then
echo Value of a is 5;
else
echo Value of a is not 5;
fi
1
# bash a.sh
#
##
###
#!/bin/bash
a=5
#a=6
#a=7
#a=8
if test $a -eq 5
then
echo Entered in 1st If Loop;
echo Value of a is 5;
elif test $a -eq 6
then
echo Entered in 2nd If Loop;
echo Value of a is 6;
elif [ $a -eq 7 ]
then
echo Entered in 3rd If Loop;
echo Value of a is 7;
else
echo Entered in else loop;
echo Value of a is not 5 or 6 or 7;
fi
# bash a.sh
if-then-if-then
##
#!/bin/bash
a=3
if [ $a -gt 0 ]
then
if [ $a -lt 5 ]
then
echo The value of "a" lies somewhere between 0 and 5.
1
fi
fi
bash a.sh
For Loop
#
##
###
#!/bin/bash
for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto
do
echo Planet name is $planet # Each planet on a separate line.
done
for planet in "Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"
# All planets on same line., Entire 'list' enclosed in quotes creates a single variable,
do
echo planet name is $planet
done
echo Whoops! Pluto is no longer a planet...
# bash a.sh
#
##
###
#!/bin/bash
for (( I = 1; I <= 9; i++ ))
do
echo $i
done
# bash a.sh
1
##
###
#\!/bin/bash
generate_list ()
{
echo one two three
}
for word in $(generate_list) # Let word grab output of function.
do
echo $word
done
# bash a.sh
While Loop
while [ condition ]
do
command(s)...
done
#
##
###
#!/bin/bash
var0=0
LIMIT=10
while [ $var0 -lt $LIMIT ]
do
echo $var0
var0=`expr $var0 + 1`
done
# bash a.sh
#
##
###
#!/bin/bash
t=0
1
condition ()
{
((t++))
if [ $t -lt 5 ]
then
return 0 # true
else
return 1 # false
fi
}
while condition
do
echo Still going: t = $t
done
bash a.sh
While loop will be executed when function returns true value.
Untill Loop
until [ condition-is-true ]
do
command(s)...
done
until [ condition-is-true ] ; do
#!/bin/bash
END_CONDITION=end
until [ "$var1" = "$END_CONDITION" ]
do
echo Input variable #1
echo "($END_CONDITION to exit)"
read var1
echo "variable #1 = $var1"
done
LIMIT=10
var=0
until (( var > LIMIT ))
do
echo "$var"
(( var++ ))
done
# bash a.sh
1
Case Statement
case "$variable" in
"$condition1" )
command...
;;
"$condition2" )
command...
;;
esac
#
##
###
#!/bin/bash
clear
echo This Will Help You Tracking The Traffic On Your server;
echo Select What You Want To Check;
echo -e 1. Check Physical Connection To The Host
echo -e 2. Ping The Host
echo -e 3. Protocol-Wise Traffic
echo -e 4. SSH Connection Info
echo -e 5. Display Open Ports
echo -e 6. Display Logs
echo -e 20. Exit
echo -n Choice -
read ans
case $ans in
1)
echo You have selected 1st Option
;;
2)
echo You have selected 2nd Option
;;
3)
echo You have selected 3rd Option
;;
1
4)
echo You have selected 4th Option
;;
5)
echo You have selected 5th Option
;;
6)
echo You have selected 6th Option
;;
20)
echo Exiting......
exit
;;
*)
echo Enter Valid Input;
eval First_Function
;;
esac
# bash a.sh
Select statement
#!/bin/bash
PS3='Choose your favorite vegetable: '
echo
select vegetable in beans carrots potatoes onions rutabagas
do
echo
echo Your favorite veggie is $vegetable.
echo
break
done
# bash a.sh
Funcion
1
function in shell script.
#
##
###
#!/bin/bash
Fruits()
{
echo Apple
echo Banana
echo Grapes
echo Mango
}
Vegetables()
{
echo beans
echo potato
echo lady finger
echo peas
}
read ans
case $ans in
1)
eval Fruits
;;
2)
eval Vegetables
;;
*)
echo Enter Valid Input;
echo Exiting.....
exit 1
;;
esac
1
# bash a.sh
sleep command
#
##
###
#\!/bin/bash
i=0
while test $i -lt 10
do
sleep 1
i=`expr $i + 1`
echo value of i is $i
done
# bash a.sh
File Handling
#
##
###
#!/bin/bash
for i in `cat a`
do
1
echo $i
sleep 1
done
# bash a.sh
#
##
###
#!/bin/bash
echo Enter filename
read file
i=`wc -l $file | awk '{print $1}'`
while [ $i -gt 0 ]
do
line=`cat $file | tail -$i | head -n 1`
echo $line
sleep 1
i=`expr $i - 1`
done
# bash a.sh
Array
#\!/bin/bash
names=( "John Smith" "Jane Doe" "sachin" "Rohan" "Sanjay" "Sandeep")
i=0
while test $i -lt 6
do
echo ${names[$i]}
i=`expr $i + 1`
done
# bash a.sh