Linux Shell Scripting
Linux Shell Scripting
Now we write our first script that will print "Welcome in Vyomlabs" on
screen.
$ vi first.sh
#
# My first shell script
#
clear
echo "Welcome in Vyomlabs"
After saving the above script, you can run the script as follows:
$ chmod 755 first
$ ./first.sh
Shell Types
• CSH (C SHell) - The C shell's syntax and usage are very similar to the
C programming language.
• KSH (Korn SHell) - Created by David Korn at AT & T Bell Labs. The
Korn Shell also was the base for the POSIX Shell standard
specifications.
• TCSH - It is an enhanced but completely compatible version of the
Berkeley UNIX C shell (CSH).
Variables in Shell
-In Linux (Shell), there are two types of variable:
(1) System variables - Created and maintained by Linux itself. This type of variable
defined in
CAPITAL LETTERS.
(2) User defined variables (UDV) - Created and maintained by user. This type of
variable defined in
lower letters.
System Variable
You can see system variables by giving command like $ set
$ set
BASH=/bin/bash
BASH_VERSION=1.14.7(1)
COLUMNS=80
HOME=/home/vivek
LINES=25
LOGNAME=students
OSTYPE=Linux
PATH=/usr/bin:/sbin:/bin:/usr/sbin
PS1=[\u@\h \W]\$
PWD=/home/students/Common
SHELL=/bin/bash
USERNAME=vivek
User defined variables
(UDV)
(2) Don't put spaces on either side of the equal sign when
assigning value to variable. For e.g. In
following variable declaration there will be no error
$ no=10
But there will be problem for any of the following variable
declaration:
$ no =10
$ no= 10
$ no = 10
2) Read statement
Use to get input (data from user) from keyboard and store
(data) to variable.
Syntax:
read variable1, variable2,...variableN
$ vi vyom.sh
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, welcome in Vyomlabs"
Quotes in Shell Scripts
There are three types of quotes
Double Quotes = “ “
Back quote =` `
For e.g
Accept Two Numbers form users (keybord) and multiply them .
Vi vyom.sh
#!/bin/bash
read -p "Enter two numbers : " x y
ans=$(( x * y ))
echo "$x + $y = $ans"
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
file and directory types Operators
Test Meaning
-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
Logical Operators
Operator Meaning
! expression Logical NOT
expression1 -a expression2 Logical AND
expression1 -o expression2 Logical OR
Decision making in shell script
if...else...fi
In Above example :
./demo.sh 5
demo.sh --- Shell Script name($0)
5 ---------- First commandline argument ($1)
In Shell Scripting we refer this as follow.
Demo.sh it is $0
5 it is $1
Command line Argument:
$0 is the name of the command
$1 first parameter
$2 second parameter
$ 3 third parameter etc. etc
$# total number of parameters
$@ all the parameters will be listed
For e.g
Vi vyom.sh
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times in vyomlabs."
done
Save and close the file. Run it as follows:
chmod -R 755 vyom.sh
./vyom.sh
For e.g
vi while.sh
#!/bin/bash
# set n to 1
n=1
# continue until $n equals 5
while [ $n -le 5 ]
do
echo "Welcome $n times in vyomlabs."
n=$(( n+1 )) # increments $n
done
Functions
Syntax:
function-name ( )
{
command1
command2
.....
...
commandN
return
}
Example:
Type SayHello() at $ prompt as follows
$ SayHello()
{
echo "Hello $LOGNAME, Have nice day"
return
}
Syntax:
cut -f{field number} {file-name}
For E.g
We have 2 files with following data.( using Vi editor)
Paste utility
For.eg
Note- that join will only work, if there is common field in both file
and if values are identical to each other.
For .e.g
[oracle@r0491 ~]$ join sname smark
awk utility
Use of awk utility:
To manipulate data.
Sed utility