Sie sind auf Seite 1von 34

Chapter 10

THE LINUX SHELL


BASH SCRIPT

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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
•…

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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…

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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,...

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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, *, ?

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
Redirecting Input and Output

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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>)

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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`

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
Variables

Quotes (single, double) :


# VAR=“Hello World”
# echo “$VAR”
Hello World
# echo ‘$VAR’
$VAR

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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
SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
Flow control

Loop : do something more than one time


Loop commands : for ,while ,until

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
The for Loop

Syntax :
for <var> in <list of value>
do
# list of commands to do
done

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
The while Loop Example
count=0
while [ $count –lt 4 ]
do
echo $count
count=$(($count +1))
done
Output :
0
1
2
3

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
The until Loop Example

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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
case Structure

case expression in
pattern1 )
action
;;
pattern2 )
action
;;

esac

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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
SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
Input Interactive

We can input information into script when executing


the script
Commands :
read
select

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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.

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
read Command Example

#!/bin/bash
echo “Enter 2 number, I will add them”
read VAR1 VAR2
echo “$VAR1 + $VAR2 = $(($VAR1+$VAR2))”

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
select Command

•It is great for creating menu


•Syntax :
select <VAR> in <list>
do
# commands
done

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
Case Command example

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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
Functions

Syntax :
function function_name ()
{
#commands
}
Or
function_name ()
{
#commands
}
SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102
Functions

Functions can be called in main script by function’s


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

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 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)

SAIGONLAB 83 Nguyễn
69-3 ThịThi
Nguyen Nhỏ,Nho,
P9, Q.Tân Bình, Tp.Tp.
P9, Q.TBinh, HCMHCM LPI 102

Das könnte Ihnen auch gefallen