Shell is an environment or a user program for interacting with the Operating System. It is not a part of kernel, but uses the system kernel to execute programs, etc.
What is a Shell Script?
Shell accept command from you (via keyboard) and execute them. But there will be a situation where you will need to use the same set of commands one by one. In such situations you can store these commands in a text file and then execute this text file instead of entering the commands one by one. Such files can be referred as Shell scripts.
The main reasons why to Write Shell Script ?
- Shell script can take input from user, file and output them on screen.
- Write to a file.
- Save lots of time.
- To automate some day to day tasks.
- Automate some System Administration task.
First Shell Script
Open up your favourite text editor. You can use any text editor to write the shell scripts. Normally the shell scripts has the file extension (.sh). In our example programs we will be saving the files with .sh extension.
The first line of every SHELL script is a commented line directed toward the interpreter.
firstshellscript.sh:
#!/bin/bash
Try the command: which bash
Use this location for your shell scripts.
Lets start with a simple script to print " Hello World "
Example:
Code: Select all
#!/bin/bash
echo "Hello World"
You will need to give execute permissions for your scripts.
Examples:
$ chmod +x firstshellscript.sh
or
$ chmod 755 firstshellscript.sh
Now how to run your script?
There are 3 different ways to run a shell script:
- bash firstshellscript.sh
- sh firstshellscript.sh
- ./firstshellscript.sh
In our examples we will be using any of these options.
Shell Syntax
SHELL follows a very specific syntax and it's is important to develop good syntax habits as it will help you to debug your code later and save time.
General Syntax in Linux Shell Scripting
Case Sensitivity
As you know Linux is case sensitive, the file names, variables, and arrays used in shell scripts are also case sensitive.
Check out this Example:
Code: Select all
#!/bin/bash
string1=10
String1=20
echo "The value of string1 is $string1 "
echo "The value of String1 is $String1 "
Comments are used to escape from the code.
This part of the code will be ignored by the program interpreter.
Adding comments, make things easy for the programmer, while editing the code in future.
Example:
Code: Select all
#!/bin/bash
string1=10 # Assigning value for string1
String1=20 # Assigning value for String2
# Now print the values
echo "The value of string1 is $string1 "
echo "The value of String1 is $String1 "
The escape character we use is the backslash ( \ ).
This is used to escape any type of character that might interfere with our code.
For example using the dollor sign.
Code: Select all
#!/bin/bash
echo "The value of \$1 USD is \$1 CAD "
Shell Variables
In Linux (Shell), there are two types of variable:
- System variables - Created by Linux developers ( inbuilt variables ).
These variables are defined in UPPER case. - User defined variables (UDV) - Created by user.
These variables are defined in lower case.
To see the system variables try the command: # set
To print the system variables use echo:
Example:
Code: Select all
#!/bin/bash
echo $HOME
How to define User defined variables (UDV)?
To define a User defined variables use the following syntax:
# variable name=value
How to access value of UDV?
Use a $ symbol to access
Lets try creating a User defined variable and accessing it.
Code: Select all
#!/bin/bash
value=100
echo $value
chmod 755 udv.sh
./udv.sh
You will find 100 printed on the screen!
Strings
Strings are scalar and there is no limit to the size of a string.
Any characters, symbols, or words can be used to make up your string.
How to define a string?
We use single or double quotations to define a string.
"Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except \ and $).
'Single quotes' - Enclosed in single quotes remains unchanged.
`Back quote` - To execute command
Example:
Code: Select all
#!/bin/bash
single='Single quoted'
double="Double quoted"
echo $single
echo $double
chmod 755 quotes.sh
./quotes.sh
String Formatting
Character Description
-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
Example:
Code: Select all
#!/bin/bash
echo -e "Hello \t Welcome to Linux Shell Scripting"
Here we will learn how to perform arithmetic operations using shell scripts.
We use the keyword " expr " to perform arithmetic operations.
Syntax:
expr op1 math-operator op2
Example:
Code: Select all
$ expr 3 + 2
$ expr 3 - 2
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 3 + 2`
Note:
If you use echo command, before expr keyword we use ` (back quote) sign.
Here both double and single quote will not give you the desired result.
Try the following commands:
Code: Select all
$ echo "expr 3 + 2" # Result expr 3 + 2
$ echo 'expr 3 + 2' # Result expr 3 + 2
$ echo `expr 3 + 2` # Result 5
To get the result of this expression, we need to use the back quote.
User Interaction in Shell Scripts
In some cases the script needs to interact with the user and accept inputs.
In shell scripts we use the read statement to take input from the user.
The Read Statement
Used to get input (data from user) from keyboard and store (data) to variable.
Syntax:
read variable1, variable2,...variableN
Lets write a shell script to ask the user his name and print it. While running this script it waits for the user input.
Code: Select all
#Script to read your name from key-board
echo "Enter your name:"
read name
echo "Hello $name, Welcome to the world of Linux!"
Save the above file as your-name.sh
Run it as follows:
$ chmod 755 your-name.sh
$ ./your-name.sh
Enter your name: Tom
Hello Tom, Welcome to the world of Linux!
Input - Output redirection in Shell Scripts
Using shell scripts, we can redirect
- the output of a command to a file
or
- redirect an output file as an input to other commands.
There are mainly 3 types of redirection. They are >,>>,<
- >
Example:
ls > ls-file
The above command will redirect the output of the " ls " to the file " ls-file ".
If the file " ls-file " already exist, it will be overwritten. Here you will loose the existing data. - >>
Example:
date >> ls-file
The output of the date command will be appended to the file " ls-file ".
In this case you will not loose any data. The new data gets added to the end of the file. - <
Example:
cat < ls-file
This redirection symbol takes input from a file.
In the above example the cat command takes the input from the file " ls-file " and displays the "ls-file" content.
Pipes and Filters are some of the important utilities in shell script. Here we will learn the use of Pipes and Filters.
Pipes
A pipe is used to connect the output of one program or command to the input of another program or command without a temporary file. we use the symbol " | " for pipes.
Example:
Creating a new file " friends ".
Code: Select all
cat > friends
bill
bush
obama
george
Now issue the following command:
sort < friends | tr "[a-z]" "[A-Z]" > FRIENDS
cat FRIENDS
Result:
Code: Select all
BILL
BUSH
OBAMA
GEORGE
Filters
A filter is a program that accepts input, transforms it or does something useful with it and outputs the transformed data. Some important filter commands are awk, tee, grep, sed, spell, and wc.
Example:
Code: Select all
ls -l | grep -a "^d" | tee dir.lst | wc -l
The ls command lists all the files and directories. The grep command takes only the directories. Using tee command, we write the result to the file " dir.list " and the wc -l prints the total number of lines in that file.
We have used 3 filters in the above example:
The grep, tee and wc.
Process
What is Process?
Process is kind of program or task in execution. Each command, program or a script in Linux is a process.
Managing a process
You might have seen some process taking long time to execute. You can run such process in the background.
Example:
Code: Select all
ls / -R | wc -l
Running a process in background
Code: Select all
ls / -R | wc -l &
So for each process you will have a process Id.
Some of the important commands associated with processes.
ps -> display currently running process
kill {PID} -> to kill a particular process
killall {Process-name} -> to kill all the process having the same name.
ps -ag -> to get information about all running process
kill 0 -> to kill all process except your shell
linux-command & -> start a process in background
ps aux -> to get all the details regarding the running process.
ps ax | grep process-name -> to check a particular process.
top -> to see currently running processes and there memory usage.
pstree -> to see currently running processes in a tree structure.
if statements
If statements are conditional statements used to test whether the condition is true or false and then continue executing the script.
if condition
Syntax:
Code: Select all
if condition
then
execute some commands
...
...
fi
Example: if.sh
Code: Select all
# SOME VARIABLES
x=7
# TESTING...
if [ $x -eq 7 ]
then
{
echo "$x is equal to 7!"
}
fi
If the condition is true it will print the result.
If else condition
Syntax:
Code: Select all
if condition
then
condition is true
execute some commands
else
if condition is not true
execute some commands up
fi
Code: Select all
# Get the value from the user
echo " Enter a value for the variable x:"
read x
# TESTING the value of x is 7 or not!
if [ $x -eq 7 ]
then
{
echo "$x is equal to 7!"
}
else
{
echo "$x is not equal to 7!"
}
fi
running the script: if-else.sh
sh if-else.sh
Enter a value for the variable x:
3
3 is not equal to 7!
In the above example, based on the value of x given by you, the script will determine if the value of x is 7 or not.
Nested If else condition
Syntax:
Code: Select all
if condition
then
if condition
then
.....
..
do this
else
....
..
do this
fi
else
...
.....
do this
fi
Code: Select all
# Get the value from the user
echo " Enter a value for the variable x:"
read x
# TESTING the value of x is 0 or not!
if [ $x -eq 0 ]
then
{
echo "$x is equal to 0!"
}
else
{
if [ $x -gt 0 ]
then
{
echo "$x is greater than 0!"
}
else
{
echo "$x is less than 0!"
}
fi
}
fi
./nested-if-else.sh
Enter a value for the variable x:
-10
-10 is less than 0!
Here the script can determine the value entered by you is less than 0 , greater than 0 or equal to 0. Try running this script with different values.
For loop in shell scripts
A for loop is used to repeat the same set of code.
If we want our script to repeat the same set of code say around 10 or 100 times, we can use a for loop.
Syntax1:
Code: Select all
for { variable name } in { list }
do
{ code to execute }
done
Code: Select all
for($start_num; Range; $increment)
do
{ code to execute }
done
Code: Select all
for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done
Code: Select all
./for1.sh
Welcome 0 times
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
The loop executed until the value of " i " reached 5.
Example2: for2.sh
Code: Select all
# Get the value from the user
echo " Enter a value for the variable x:"
read x
# For loop
for (( a = $x; a <= 10; a++ ))
do
echo "The value of x is $a now"
done
Code: Select all
./for2.sh
Enter a value for the variable x:
7
The value of x is 7 now
The value of x is 8 now
The value of x is 9 now
The value of x is 10 now
You can nest the for loop.
To understand the nesting of for loop try the following shell script.
Example: nested-for.sh
Code: Select all
for (( i = 1; i <= 5; i++ )) # 1st loop
do
for (( j = 1 ; j <= 5; j++ )) # 2nd loop
do
echo "The value of i is $i "
echo "The value of j is $j "
done
echo -e "\n" ## print the new line
done
Code: Select all
./nested-for.sh
The value of i is 1
The value of j is 1
The value of i is 1
The value of j is 2
The value of i is 1
The value of j is 3
......
.....
A while loop is executed as long as a given condition is true. Here we will learn the use of while loops.
Syntax:
Code: Select all
while [ condition ]
do
{ code to execute }
....
done
Code: Select all
#!/bin/sh
# Get the value from the user
echo " Enter a value for the variable x:"
read x
while [ $x -le 10 ]
do
echo "The value of x is $x"
x=`expr $x + 1`
done
./while.sh
Code: Select all
Enter a value for the variable x:
6
The value of x is 6
The value of x is 7
The value of x is 8
The value of x is 9
The value of x is 10
Each time the loop is executed, the x value is incremented. The loop continues until the value of x is less than 10.
Once the condition is satisfied, the control come out of the loop.
Case Statement
The case statement can be used to match several values against one variable.
The case statement is very useful when you want to provide options for the users.
Example:
Select 1 to say " Hello "
Select 2 to say " Good Bye "
Syntax:
Code: Select all
case $variable-name in
pattern1) { code to execute };;
pattern2) { code to execute };;
patternN) { code to execute };;
*)
{ code to execute };;
esac
Code: Select all
#!/bin/sh
# Get the value from the user
echo "Enter a digit between [ 1 and 4 ]"
echo "Enter a value for the variable x:"
read x
case $x in
"1") echo "You have entered One";;
"2") echo "You have entered Two";;
"3") echo "You have entered Three";;
"4") echo "You have entered Four";;
*) echo "Sorry, You have entered a value beyond limit";;
esac
./case.sh
Code: Select all
Enter a digit between [ 1 and 4 ]
Enter a value for the variable x:
4
You have entered Four
In the above example, we entered 4 as the value of x. Try this example script with various other values and see the results. Also try adding more options to the case statement.