Sunteți pe pagina 1din 26

Introduction

How familiar you are with Unix/Linux?


How familiar you are with programming?
How familiar you are with shell scripting?
After session How you want to implement shell scripting knowledge in your
project?

1
TOPICS

Benefits of Shell Programming............................................................................................4


Process.................................................................................................................................4
Variables..............................................................................................................................4
Variable substitution / assignment.......................................................................................4
Environment variables.........................................................................................................5
Command line argument/Run time variables......................................................................5
Exit codes.............................................................................................................................6
Escape characters.................................................................................................................7
kill........................................................................................................................................7
CUT......................................................................................................................................8
AWK....................................................................................................................................8
SED......................................................................................................................................8
SEQ......................................................................................................................................8
Redirection...........................................................................................................................8
Clearing File contents..........................................................................................................8
Overwrite File Contents.......................................................................................................8
Append File Contents..........................................................................................................9
Standard error and standard out...........................................................................................9
Debug Mode.........................................................................................................................9
Comparison Operators.......................................................................................................11
String Comparison.............................................................................................................11
compound comparison.......................................................................................................12
Arithmetic operators..........................................................................................................12
EXPR.................................................................................................................................13
Modulo example................................................................................................................13
Example of let....................................................................................................................13
Operators............................................................................................................................14
Bitwise operators...............................................................................................................14
Logical (boolean) operators...............................................................................................14
Miscellaneous operators.....................................................................................................14
Shift Operator.....................................................................................................................15
Continue and Break statement...........................................................................................15
IF Statement.......................................................................................................................16
if else..................................................................................................................................17
if elif (Nested If)................................................................................................................18
if-then-if-then.....................................................................................................................18
For Loop.............................................................................................................................19
While Loop........................................................................................................................20
Untill Loop.........................................................................................................................21
Case Statement...................................................................................................................22
Select statement.................................................................................................................23
Funcion..............................................................................................................................23
sleep command ..................................................................................................................25
Convert shell script into executables or binary format......................................................25

1
File Handling.....................................................................................................................25
Array..................................................................................................................................26

1
Benefits of Shell Programming

1. Help in System Administration task Like monitoring,


2. Analyzing Logs
3. Monitoring Process / Load / Memory of server/ Databases / Partitions
4. Creating Reports in csv format
5. Log Rotation
6. Automating daily tasks by scheduling scripts in the cron
7. Write startup script for applications

A shell script is a quick method of prototyping a complex application. Getting even a


limited subset of the Functionality to work in a script is often a useful first stage in
project development. This way, the structure of the Application can be tested and played
with, and the major pitfalls found before proceeding to the final coding in C, C++,
Java, Perl, or Python.

Linux startup procedures are nothing but the shell scripts.


Main are /etc/inittab and /etc/rc.sysinit

Process

Process defined as:


"A process is program (command given by user) to perform specific Job. In Linux when
you start process, it
Gives a number to process (called PID or process-id); PID starts from 0 to 65535."

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.

Variable substitution / assignment

# variable1=23
# echo variable1
variable1

1
# echo $variable1
23

When value is not assigned value will be null.


echo "$uninitialized"
let "uninitialized += 5"
echo "$uninitialized"

Environment variables

env
assign environment variables
echo $PATH
export PATH="$PATH:/usr"
echo $PATH

assign command output to variable


a=`ls -l`
echo $a

Command line argument/Run time variables

$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

Search string in file. (Quotes)


#
##
###
echo "file1 - This is the first line of file1.txt." > file1.txt
echo "file2 - This is the First line of file1.txt." > file2.txt
echo "file3 - This is the Third line of file1.txt." > file3.txt

#
##
###
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

self trmination script

#
#
##

1
#!/bin/bash
kill $$
echo This line will not echo.
exit 0

# bash a.sh

CUT

cat /etc/passwd | cut -f1 -d":"


cat /etc/passwd | cut -f1-2 -d":"
cat /etc/passwd | cut -f3 -d":"

AWK

cat /etc/passwd | awk -F ":" '{print $1}'

SED

cat /etc/inittab | grep ctr


sed -i 's/ca::ctrlaltdel:/#ca::ctrlaltdel:/g' /etc/inittab
cat /etc/inittab | grep ctr
sed -i 's/#ca::ctrlaltdel:/ca::ctrlaltdel:/g' /etc/inittab
cat /etc/inittab | grep ctr

SEQ

seq 5
seq -s : 5

Redirection

>
>>
1>
2>

Clearing File contents

> a.sh
cat /dev/null > a.sh

Overwrite File Contents

1
cat a > a.sh

Append File Contents

cat a >> a.sh

Standard error and standard out

ls 1> a
lss 1> a
lss 2> a

stdin 0 as Standard input Keyboard


stdout 1 as Standard output Screen
stderr 2 as Standard error Screen

Debug Mode

Executing in debug more / explore more


# bash -x a.sh

For string value should be in Quotes.

File test operators

-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

-a - logical and &&


exp1 -a exp2 returns true if both exp1 and exp2 are true.

-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)

Arithmetic operators often occur in an expr or let expression.

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

<< - bitwise left shift (multiplies by 2 for each shift position)


<<= - left-shift-equal
let "var <<= 2" results in var left-shifted 2 bits (multiplied by 4)
>> - bitwise right shift (divides by 2 for each shift position)
>>= - right-shift-equal (inverse of <<=)
& - bitwise AND
&= - bitwise AND-equal
| - bitwise OR
|= - bitwise OR-equal
~ - bitwise NOT
^ - bitwise XOR
^= - bitwise XOR-equal

Logical (boolean) operators

! - NOT
&& - AND
|| - OR

Miscellaneous operators

, - Comma operator

Example of 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.

Check installed bash version


echo $BASH_VERSION

Shift Operator

#
##
###

#!/bin/bash
echo $@
shift
echo $@
shift
echo $@
shift
echo $@
shift
echo $@

# bash a.sh 1 2 3 4 5

Continue and Break statement

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

if elif (Nested If)

#
##
###

#!/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

Change value of a and check output for each loop.


Here we have used two types test and [ ].

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

for arg in [list]


do
command(s)...
done

#
##
###

#!/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

For loop for a function


-----------------------------

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

While loop with function


--------------------------------

#
##
###

#!/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

We can create menus using select statement as well.

#!/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
}

echo Select Your Choice


echo 1. Fruits
echo 2. Vegetables

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

Stops execution of script for given time interval.

#
##
###

#\!/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

Convert shell script into executables or binary format

Use shc software


Install shc
# shc -f script.sh
You will get these two files.
C File - script.sh.x.c
Binary File - script.sh.x.

File Handling

echo "this is line 1" > a


echo "this is line 2" >> a
echo "this is line 3" >> a
echo "this is line 4" >> a
echo "this is line 5" >> a

#
##
###

#!/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

S-ar putea să vă placă și