Linux Shell Scripting Tutorial

Linux OS Topics
Post Reply
User avatar
Shane
Captain
Captain
Posts: 226
Joined: Sun Jul 19, 2009 9:59 pm
Location: Jönköping, Sweden

Linux Shell Scripting Tutorial

Post by Shane » Thu Oct 01, 2009 2:43 am

What is a Linux Shell?

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 ?
  1. Shell script can take input from user, file and output them on screen.
  2. Write to a file.
  3. Save lots of time.
  4. To automate some day to day tasks.
  5. 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"
Save it as " firstshellscript.sh ".
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:
  1. bash firstshellscript.sh
  2. sh firstshellscript.sh
  3. ./firstshellscript.sh
Try all the above.
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
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 "
Escaping Characters
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 "
Try the same example without " \ ".

Shell Variables

In Linux (Shell), there are two types of variable:
  1. System variables - Created by Linux developers ( inbuilt variables ).
    These variables are defined in UPPER case.
  2. User defined variables (UDV) - Created by user.
    These variables are defined in lower case.
System Variables
To see the system variables try the command: # set
To print the system variables use echo:

Example:

Code: Select all

#!/bin/bash
echo $HOME
User Defined Variables
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
Save the above script as udv.sh
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
Save the above example script as quotes.sh
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"
Arithmetic
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`
In shell scripting we will be using the echo command to print the result of the arithmetic operations.

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
From the above example, we see that if we use double or single quote, the echo command directly prints " expr 3 + 2 ".
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!"
Result
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 >,>>,<
  1. >
    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.
  2. >>
    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.
  3. <
    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
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
Press CTRL + D to save.

Now issue the following command:
sort < friends | tr "[a-z]" "[A-Z]" > FRIENDS
cat FRIENDS

Result:

Code: Select all

BILL
BUSH
OBAMA
GEORGE
The above command has sorted the file " friends " and using pipe the output was translated to upper-case letters and then redirected its output to the file " FRIENDS ".

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
what the above command does:
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
The above command can be used to find the total count of the files on your system. This takes a long a time, so to get the command prompt and to start an other process, you can run the above command in background.

Running a process in background

Code: Select all

ls / -R | wc -l &
The ampersand (&) at the end of command tells the shell to run the command in background. You will a number printed on your screen. It is the process Id.
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
In the above example if condition was used to check if the value of x is equal to 7.
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
Example: if-else.sh

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
Example: nested-if-else.sh

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
running the script: nested-if-else.sh
./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
Syntax2:

Code: Select all

for($start_num; Range; $increment)
do 
{ code to execute }
done
Example1: for1.sh

Code: Select all

for ((  i = 0 ;  i <= 5;  i++  ))
do
  echo "Welcome $i times"
done
Result:

Code: Select all

./for1.sh
Welcome 0 times
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 times
Welcome 5 times
In the above example the initial value of " i = 0 ".
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
Result:

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
Nested for loop
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
Result:

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
......
.....
While Loop
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
Example: while.sh

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
Result:
./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
In the above example, the value of x is 6.
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
Example: case.sh

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
Result:
./case.sh

Code: Select all

Enter a digit between [ 1 and 4 ]
Enter a value for the variable x:
4
You have entered Four
The case.sh script takes the value for x from the user and process it accordingly.
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.
Post Reply

Return to “Linux”