View
254
Download
0
Category
Preview:
Citation preview
8/10/2019 Linux Shell Scripts
1/29
Linux Shell Scripts
8/10/2019 Linux Shell Scripts
2/29
Road Map
To understand the types of shell.
To understand Shell variables.
To understand the programming constructs.
To understand the System Variables.
To customize the shell.
8/10/2019 Linux Shell Scripts
3/29
Reference
UNIX Bourne Shell Scriptinghttp://www.esscc.uq.edu.au/~ksteube/Bshell/
http://www.esscc.uq.edu.au/~ksteube/Bshell/http://www.esscc.uq.edu.au/~ksteube/Bshell/8/10/2019 Linux Shell Scripts
4/29
What is Shell.
A program that interprets users requests to runprograms.Provides environment for user programs. A command line interpreter that
Reads user inputs.Executes commands.
8/10/2019 Linux Shell Scripts
5/29
Types of ShellShell Name Developed By Remark
BASH ( Bourne- Again SHell )
Brian Fox andChet Ramey
Most common shell inLinux. It's Freeware shell.
CSH (C SHell) Bill Joy The C shell's syntax andusage are very similar tothe C programminglanguage.
KSH (KornSHell)
David Korn
TCSH See the manpage.Type $ man tcsh
TCSH is an enhanced butcompletely compatibleversion of the BerkeleyUNIX C shell (CSH).
Tip: To find all available shells in your system type following command:$ cat /etc/shells
To find your current shell type following command
$ echo $SHELL
8/10/2019 Linux Shell Scripts
6/29
What Shell Does ?
Shell allows three types of commands: An internal command.
ls,cat,cd
An executable file that contains a sequence of shellcommand lines..sh file
An executable file that contains object code produced bycompilation.
.o file generated from a c file
8/10/2019 Linux Shell Scripts
7/29
Why to Write Shell Script ?
Shell script can take input from user, file andoutput them on screen.
Useful to create our own commands.Save lots of time.To automate some task of day today life.
System Administration part can be alsoautomated.
8/10/2019 Linux Shell Scripts
8/29
Shell Variables.
Positional Parameters Acquire values from the position of argumentsin command line
Special ParametersCreated and maintained by Linux itself.
Named variablesCreated and maintained by user.
8/10/2019 Linux Shell Scripts
9/29
Positional Parameters Examples.
Acquire values from the position of argumentsin command line.
$1, $2, $3,..$9
sh file1 10 20 30
$1 $2 $3
8/10/2019 Linux Shell Scripts
10/29
Special Parameters.
SpecialParameter Meaning Usage$$ PID number of the current shell echo $$ , Then type tcsh and echo $$
$#Number of Command Line
Arguments Run Special.sh in the example$* Displays all command line option Run Special.sh in the example
$? Exit statusecho $? If command is successfullyrun then it will give 0 else 1. try to copya non existant file and echo $?
$! Process number of the lastbackground command
sleep 3000 & , echo $!
8/10/2019 Linux Shell Scripts
11/29
Named Variables.
User-defined variablethat can be assigneda value.Used extensively in
shell-scripts. Used for readingdata, storing anddisplaying it.
Example 1 Example 2
$> a=10 , echo $a $> name=Binaya $> echo$name
8/10/2019 Linux Shell Scripts
12/29
The read/display Statement
Use to get input (data from user) from keyboard andstore (data) to variable.Syntax : read variable_name.
Example : read sname
VariableName
8/10/2019 Linux Shell Scripts
13/29
Program Execution.
shCommand to execute program in Unix.Syntax : sh Example : sh file1
8/10/2019 Linux Shell Scripts
14/29
Test or [expr] command.
Test command or [ expr ] is used to see if anexpression is true,If it is true it return zero(0), otherwise returns
nonzero for false.In place of writing test explicitly, the user could alsouse [ ].test or [ expr ] works with1.Integer ( Number without decimal point)2.File types3.Character strings
8/10/2019 Linux Shell Scripts
15/29
Test or [expr] command for Integers
Using Test Using [ expr]
5 == 6 is equal to if test 5 -eq 6 if [ 5 -eq 6 ]5 != 6 is not equal to if test 5 -ne 6 if [ 5 -ne 6 ]5 < 6 is less than if test 5 -lt 6 if [ 5 -lt 6 ]5 6 is greater than if test 5 -gt 6 if [ 5 -gt 6 ]
5 >= 6 is greater than or equalto
if test 5 -ge 6 if [ 5 -ge 6 ]
In ShellNormal ArithmeticalStatement
Meaning
Tip: Make sure that there is a space when you use [].
8/10/2019 Linux Shell Scripts
16/29
Test or [expr] command for Strings
string1 = string2 string1 is equal to string2string1 != string2 string1 is NOT equal to string2string1 string1 is NOT NULL or not defined-n string1 string1 is NOT NULL and does exist
-z string1 string1 is NULL and does exist
Operator Meaning
8/10/2019 Linux Shell Scripts
17/29
Test or [expr] command for files
-s file Non empty file -f file Is File exist or normal file and not a directory-d dir Is Directory exist and not a file
-w file Is writeable file-r file Is read-only file -x file Is file is executable
Operator Meaning
8/10/2019 Linux Shell Scripts
18/29
Logical Operators
! expression Logical NOTexpression1 -a expression2 Logical ANDexpression1 -o expression2 Logical OR
Operator Meaning
8/10/2019 Linux Shell Scripts
19/29
expr command.
Used for evaluating shell expressions.Used for arithmetic and string operations.
Example : expr 7 + 3 would give an output 10.
When used with variables, back quotes needto be used.
Operator has to be preceded and followed by a space.
8/10/2019 Linux Shell Scripts
20/29
Shell Quotes
Case Answer Single-Quote (' ') Takes Literally x=10; echo 'echo $x' echo $xSingle- Back Quote (``) Interpret the result X=`expr $x + $y`Double Quote ("") Evaluate the variables ($) x=10; echo 'echo $x' echo 10
Quote Type Usage
Tip 1: Make sure that single command work on command line.
Tip 2 : Be careful about the space.
y =`expr $x+$x` Not OKy =`expr $x + $x` OK
8/10/2019 Linux Shell Scripts
21/29
Program Constructs
iffor while
untilcase
8/10/2019 Linux Shell Scripts
22/29
if statement.
Syntax:
i f con t ro l comm and
then
else
fi
8/10/2019 Linux Shell Scripts
23/29
for statement.
Syntax.
for variable-name in value1 value2 .. .. valueN
d o
< c o m m a n d s >
done
8/10/2019 Linux Shell Scripts
24/29
while statement.
Syntax.
w h i l e c o n t ro l c o m m a n d
d o
< c o m m a n d s >
d o n e
8/10/2019 Linux Shell Scripts
25/29
until statement.
Syntax.
unt i l control command
d o
d o n e
8/10/2019 Linux Shell Scripts
26/29
case statement.
Syntax.
case value in
cho ice1) com m ands ; ;
cho ice2) com m ands ; ;
....
....esac
The symbols ;; are used as option terminators.
8/10/2019 Linux Shell Scripts
27/29
Useful Shell Scripting commands.
breakTo come out of a loop.
continue
To jump to the start of loop.exit
To prematurely terminate a program.
: or #
To interpret the rest of line as comments.
8/10/2019 Linux Shell Scripts
28/29
export command.
exportTo make a variable a part of environment and also beaccessible to the child process (or shell)
Give an Example of Oracle ENV variable
8/10/2019 Linux Shell Scripts
29/29
Recommended