Sei sulla pagina 1di 4

Linux Shell Scripting

1. How to write a shell script


can put those commands in a file and thus, get a program (shell script) written in
shell as a programming language.
The following steps are required to write shell script:
1. Use any editor vi to write shell script.
2. After writing shell script, set execute permission for your script file as follows:
>chmod u+x yourscriptname
3. Execute your script as:
>./yourscriptname
>sh yourscriptname

1.1. My first shell script


Now, open an editor (for example by using vi), give the name first.sh to your file
and write in this file the following commands:
# My first shell script
clear
echo "Hello World"
After saving the above script, you can run it as follows:
sh first
Just notice what happens.
Tip: For shell script file try to give file extension such as sh or .scr, which can be
easily identified by you as shell script.
Script Command(s) Meaning
>pico first.sh Start pico editor
# My first shell script # followed by any text is considered as
comment. Comment gives more information
about script.
Syntax:
# commenttext
clear clear the screen
echo "Hello World" To print message or value of variables on
screen, we use echo command.
General form of echo command is as follows.
Syntax:
echo "Message"
Exercise:
1) Write the following shell script, save it, execute it and notice the output.
> vi user_info sh
# Script to print user information who currently login , current
# date & time
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc l
echo "Calendar"
cal

1.2 Perform arithmetic operations


Syntax:
expr op1 mathoperator op2
Examples:
expr 1 + 3
expr 2 1
expr 10 / 2
expr 20 % 3
expr 10 \* 3
echo `expr 6 + 3

2.1. Operators for the conditions:


(a) Mathematics
Mathematical
Operator
Meaning
Normal
Arithmetical/
Mathematical
Statements
Shell Script
eq is equal to i == 6 if [ $i eq 6 ]
ne
is not equal
to
i != 6 if [ $i ne 6 ]
lt is less than i < 6 if [ $i lt 6 ]
le
is less than
or equal to
i <= 6 if [ $i le 6 ]
gt
is greater
than
i > 6 if [ $i gt 6 ]
ge
is greater
than or
equal to
i >= 6 if [ $i ge 6 ]
(b) String Comparisons
Operator Meaning
string1 = string2 string1 is equal to string2
string1 != string2 string1 is NOT equal to string2
string1 string1 is NOT NULL or not defined
n string1 string1 is NOT NULL and does exist
z string1 string1 is NULL and does exist
(c) Logical Operators
Logical operators are used to combine two or more condition at a time

Operator Meaning
! expression Logical NOT
expression1 a expression2 Logical AND
expression1 o expression2 Logical OR

2.2. if condition
if condition which is used for decision making in shell script.
Syntax:
if [ condition ]
then
command1
...
fi

Syntax:

if [ condition ]
then
command1
...
else
command1
...
fi

Syntax:

if [ condition ]
then
command
...
elif [ condition1 ]
then
command1
...
elif [ condition2 ]
then
command2
...
else
elsecommand
...
fi

2.3. for Loop


Syntax:

for variable_name in list


do
command1
...
done

2.4. while loop


Syntax:

while [ condition ]
do
command1

....
done

3 Use of Spaces in linux


1 . After writing echo single space is used and then inverted commas( ) are applied.
2. After using every operator we have to use spaces both sides .Like while using
if,while,for etc. we have to use space before starting its paranthesis and after it too.
3. But when we assign a value to a variable,spaces are not used.
4. When we rename a file,copy or move it then also we have to use spaces.

Potrebbero piacerti anche