7/31/2019 Shell Scripting 2
1/53
LINUX
7/31/2019 Shell Scripting 2
2/53
Variables
process our data/information
kept in computers RAM memory
two types of variables
System variables
Created and maintained by Linux itself and is defined in
CAPITAL LETTERS.
User defined variables (UDV)
Created and maintained by user and is defined in
LOWER LETTERS
7/31/2019 Shell Scripting 2
3/53
System Variables
7/31/2019 Shell Scripting 2
4/53
System Variables
$ echo $USERNAME
$ echo $HOME
Caution: Do not modify System variable this can
some time create problems.
7/31/2019 Shell Scripting 2
5/53
System variables
PS1 variable
Prompt string 1 variable
Contains the shell prompt, $ symbol
To change the shell prompt
$PS1=Hello>
Now your prompt becomes
Hello>
7/31/2019 Shell Scripting 2
6/53
PS2 variable
Specifies the value for the secondary prompt
Secondary prompt
Displays when an incomplete command is entered
on the command line
Default value is > symbol
Example
$PS2=^
7/31/2019 Shell Scripting 2
7/53
Example - PS2 variable
$ echo This is incomplete
^ close the quotes
^
$
7/31/2019 Shell Scripting 2
8/53
LOGNAME variable
Users login name
echo ${LOGNAME}
Or
echo $LOGNAME
7/31/2019 Shell Scripting 2
9/53
SHLVL variable Contains the shell level currently working in
$echo $SHLVL
1 this is the login shell
$sh
$echo $SHLVL
2 working in the new shell
$exit
$echo $SHLVL
1 login shell
7/31/2019 Shell Scripting 2
10/53
env command
View a list of all the exported environment variables
and their respective values
$env
7/31/2019 Shell Scripting 2
11/53
Rules for Naming variable name (BothUDV and System Variable)
Variable name must begin with Alphanumeric character or
underscore character (_), followed by one or more
Alphanumeric character
Don't put spaces on either side of the equal sign when
assigning value to variable.
Variables are case-sensitive
7/31/2019 Shell Scripting 2
12/53
Rules for Naming variable name (BothUDV and System Variable)
You can define NULL variable as follows (NULL variable is
variable which has no value at the time of definition)
For e.g.
$ vech=
$ vech="
Do not use ?,* etc, to name your variable names.
7/31/2019 Shell Scripting 2
13/53
7/31/2019 Shell Scripting 2
14/53
Linux
7/31/2019 Shell Scripting 2
15/53
echo
Displays text or variables value on screen.
Syntax:-
echo [options] [string, variables...]
7/31/2019 Shell Scripting 2
16/53
Options of echo command
-n Do not output the trailing new line.
-e Enable interpretation of the following backslash
escaped characters in the strings:
\a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
7/31/2019 Shell Scripting 2
17/53
Example of echo command
$ echo -e "An apple a day keeps away \a\t\tdoctor\n"
7/31/2019 Shell Scripting 2
18/53
More about Quotes
There are three types of quotes
Double Quotes
' Single quotes
` Back quote
7/31/2019 Shell Scripting 2
19/53
Double Quotes
Anything enclose in double quotes removed meaning of
that characters (except \ and $).
echo Hello
$ echo "Today is date"
Can't print message with today's date.
7/31/2019 Shell Scripting 2
20/53
Single quotes
Enclosed in single quotes remains unchanged.
7/31/2019 Shell Scripting 2
21/53
Back quote
To execute command.
Example
$ echo "Today is `date`".
it will print today's date
7/31/2019 Shell Scripting 2
22/53
7/31/2019 Shell Scripting 2
23/53
Examples
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3 # remainder read as 20 mod 3and remainder is 2)
$ expr 10 \* 3 # Multiplication use \* not *
since its wild card)
$ echo `expr 6 + 3`
7/31/2019 Shell Scripting 2
24/53
Exit Status
$? variable of shell is used
$ ls
$ echo $?
It will print 0 to indicate command is successfu
7/31/2019 Shell Scripting 2
25/53
Arithmetic expression
Calculate the value of an expression
Enclose the expansion in $(( )).
Syntax
$((expression))
7/31/2019 Shell Scripting 2
26/53
expr command
echo `expr 10 + 2`
Var=`expr 10 + 2`
7/31/2019 Shell Scripting 2
27/53
Example
$ a=25
& b=20
echo $((a+b))
7/31/2019 Shell Scripting 2
28/53
Expression
combination of terms and operators
Result of an expression can be an arithmetic or a logical
result Arithmetic result is represented as a string of decimal
digits
Logical result is represented as a true condition or a
false condition
7/31/2019 Shell Scripting 2
29/53
Conditional execution
Test and [ ] command
If constructs
7/31/2019 Shell Scripting 2
30/53
Test command
Specify a condition that can be either true or false
test command or [ expr ] is used to see if an expression is true
Use square brackets [], instead of test command
Returns true or false
if it is true it return zero(0), otherwise returns nonzero(>0) for
false.
Syntax:
testexpression
[expression]
7/31/2019 Shell Scripting 2
31/53
test command
test $user_name= rani
[ $user_name= rani ]
7/31/2019 Shell Scripting 2
32/53
For Mathematics use following operator inShell Script
7/31/2019 Shell Scripting 2
33/53
For string Comparisons use
7/31/2019 Shell Scripting 2
34/53
Shell also test for file and directory types
7/31/2019 Shell Scripting 2
35/53
Logical Operators
Logical operators are used to combine two or more condition at a time
7/31/2019 Shell Scripting 2
36/53
Simple if construct
used for decision making in shell script
If given condition is true then command1 is executed.
condition is nothing but comparison between two values, for
compression we can use test or [expr ] statements or evenexist status can be also used.
An expression is nothing but combination of values,
relational operator (such as >,
7/31/2019 Shell Scripting 2
37/53
Simple If
Syntax:
if condition
then
command1 if condition is true or if exit status
of condition is 0 (zero)
...
fi
7/31/2019 Shell Scripting 2
38/53
7/31/2019 Shell Scripting 2
39/53
Example
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi
M ltil l if th l S t
7/31/2019 Shell Scripting 2
40/53
Multilevel if-then-else -Syntax
if condition
then
condition is zero (true - 0)
execute all commands up to elif statement
elif condition1
condition1 is zero (true - 0) execute all commands up to elif statement
elif condition2
condition2 is zero (true - 0)
execute all commands up to elif statement
7/31/2019 Shell Scripting 2
41/53
Multilevel if-then-else -Syntax
else
None of the above condtion,condtion1,condtion2
are true (i.e. all of the above nonzero or false) execute all commands up to fi
fi
L i l t l
7/31/2019 Shell Scripting 2
42/53
Logical operators example-a AND-o OR
if [ $a ge 60 -a $a -lt 80 ]
then
Echo distinction
fi
7/31/2019 Shell Scripting 2
43/53
exit command
Terminate the execution of a shell script and return
to the $prompt
Syntax
exit
7/31/2019 Shell Scripting 2
44/53
case.....esac construct
Check multiple values of a variable
7/31/2019 Shell Scripting 2
45/53
Syntax:
case $variable_name in
value1) command.
;;
value2) command.
;;
.
..*) command;;
esac
7/31/2019 Shell Scripting 2
46/53
Example
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
*) echo "Sorry, invalid data";;
esac
7/31/2019 Shell Scripting 2
47/53
Linux
7/31/2019 Shell Scripting 2
48/53
Loops
for loop
While loop
Until loop
7/31/2019 Shell Scripting 2
49/53
7/31/2019 Shell Scripting 2
50/53
Example
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
7/31/2019 Shell Scripting 2
51/53
While loop
Loop is executed as long as given condition is true
7/31/2019 Shell Scripting 2
52/53
Example multiplication table of a given
7/31/2019 Shell Scripting 2
53/53
Example multiplication table of a givennumber
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
Top Related