You are on page 1of 34

Chapter 10

THE LINUX SHELL BASH SCRIPT

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Objectives

Identify different Linux shell environments Understand the redirection of input and output Understand and utilize command substitution Write and configure BASH script using variables, flow controls interactive input, functions, arithmetic and arrays

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

The Linux Shell

Shell is a interface between OS and user. It provides : A facility for launching and managing commands and programs An operating environment

A programming language

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

The Linux Shell

Type of shell : Bourne shell (sh), Bourne Again shell (bash), Korn shell (ksh), C shell (csh, tcsh), Programs start from command line have separate environments : parameters, variables, functions, aliases

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Shell Environment Customize


Type of variables: local (shell variable), global (environment variable)

List all local variables : use set command


List all global variables : use env command Default environment variables : PS1, PS2,

HOME, LOGNAME, SHELL, PATH, PAGER,


LPDEST, PWD, DISPLAY, MAIL,...

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Shell Environment Customize

bash configuration files :

/etc/profile
~/.bash_profile, ~/.bash_login, ~/.profile set : define a new variable

unset : undefine a variable


export : make a local variable becomes a global variable

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Using the bash shell

Alt+Fn : switch between virtual consoles gpm : mouse server daemon, use it to copy and paste even between different consoles Auto complete : use TAB key Up and down arrow keys : get history commands (store in ~/.bash_history)

Ctrl+Z, Ctrl+C, Ctrl+D, *, ?

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Redirecting Input and Output

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Redirecting Input and Output

Redirect input : use (<) or (<0) # mail admin@saigonctt.com < content Redirect output : use (>) or (1>) # ls l > list_file ( Use set o noclobber : prevent file overwriting ) Append : use (>>) Redirect error : use (2>)

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Pipe and Back ticks (` ,`)


Pipe (|): command1 | command2
Output of command1 becomes input of command2 # ls l |grep samba

Back ticks (``) or $()


1. # which passwd /usr/bin/passwd

2. # ls l /usr/bin/passwd
3. # ls l `which passwd`
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Background jobs
Job ( process) :

# ls l | grep *.jpg
Run job in the background : Append with & : # sleep 1000 & [1] 1234 If a job is running in foreground, suspend it by Ctrl+Z then run the following command to switch to background : # bg %job_id
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Shell Script
Shell script : is a text file contains shell commands, functions, aliases, flow control structures, loops, comments All comments begin with # but #! is used to identify a commands interpreter $ more example_script #!/bin/bash /usr/bin/smbd -D /usr/bin/nmbd -D
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Variables
Naming : not begin with a digit, usually in upper case letters

Assigning : not SPACES around =


VAR=value : assign value string to VAR VAR=`cmd` : the same VAR=$(cmd) ,assign output of cmd to VAR # VAR1=`ls /var/log | wc l` # echo $VAR1

65
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Variables

Quotes (single, double) : # VAR=Hello World

# echo $VAR
Hello World # echo $VAR

$VAR

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Variable Notation
Expand variables : use ${VAR}

# VAR1=This is a String ; echo $VAR1


This is a String # VAR2=$VAR1xyz ; echo $VAR2 Nothing #default # VAR3=${VAR1}xyz ; echo $VAR3 This is a Stringxyz # VAR4=${VAR1}xyz ; echo $VAR4 ${VAR1}xyz
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Passing Information to Sript


On the command line, information can be passed to script through pre-set positional parameters
$0 The name of the script $1-$9 Parameters are being passed to script $* A string contains ALL parameters passed to script, separated by the first character defined in IFS (Internal File Separator) variable $@ A list of ALL parameters as separate string

$# Number of parameters included on the command line

The shift command will shift the positional parameters one or more position from left to right
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Flow control

Loop : do something more than one time


Loop commands : for ,while ,until

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

The for Loop

Syntax : for <var> in <list of value>

do
# list of commands to do done

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

The for Loop Example


This script will rename all file .txt in current directory to .html

#!/bin/bash
for file in $(ls *.txt) do

newname=$(basename $file .txt).html;


mv $file $newname; done
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

The while and until Loop


Syntax : while <condition> do # list of commands to do done -------------------------------------------------------until <condition> do # list of commands to do done
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

The while Loop Example


count=0

while [ $count lt 4 ]
do echo $count count=$(($count +1)) done Output : 0

1
2 3
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

The until Loop Example


count=0

until [ $count ge 4 ]
do echo $count count=$(($count +1)) done Output : 0

1
2 3
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Return codes/Exit status

The variable $? contains the return code of the previous executed command or application.

0 Success
0 Failure The exit n command will cause the script to quit and assign the value of n to $? variable

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Tests and Conditions


Test : use [ ] around expression
If-then-else structure: if [ <exp1> ] # include SPACEs

then
#commands to do if the exp1 is true else

#commands to do if the exp1 is NOT true


fi
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

case Structure
case expression in

pattern1 )
action ;; pattern2 ) action ;; esac
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

case Test Example


while [ $1 ] do

echo n $1 hits the case $1 in a?c | ab? ) echo first case. ;; abcde ) echo second case. ;; abc123 ) echo third case. ;; *) echo third case. ;; esac done
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Input Interactive

We can input information into script when executing the script


Commands :

read
select

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

read Command

Allow to read values into variables Syntax :

read VAR1 VAR2


If there is more input than you are looking for, all the extras are put in the last variable.

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

read Command Example

#!/bin/bash

echo Enter 2 number, I will add them


read VAR1 VAR2 echo $VAR1 + $VAR2 = $(($VAR1+$VAR2))

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

select Command

It is great for creating menu Syntax : select <VAR> in <list> do # commands done

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Case Command example

#!/bin/bash VAR=$(ls) select i in $VAR do echo $i; exit; done

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Functions
Syntax : function function_name () { #commands } Or function_name () { #commands }
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Functions

Functions can be called in main script by functions name. It inherits ALL parameters in the main script We can change the return code of the function by using return n command

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

Summary

Step 1 : create script file (cat, vi, mc, ),enter script codes.
Step 2 : add execute permission mode to file ( chmod u+x file )

Step 3 : run it (add script directory to PATH environment or use absolute path)

Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB

LPI LPI 102 102

You might also like