Upload
m3onh0x84
View
240
Download
2
Embed Size (px)
Citation preview
8/9/2019 Ch10 Linux Shell Bash Script
1/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Chapter 10Chapter 10
THE LINUX SHELLTHE LINUX SHELL
BASH SCRIPTBASH SCRIPT
8/9/2019 Ch10 Linux Shell Bash Script
2/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Objectives
Identify different Linux shell environments
Understand the redirection of input and outputUnderstand and utilize command substitution
Write and configure BASH script using variables,
flow controls interactive input, functions, arithmeticand arrays
8/9/2019 Ch10 Linux Shell Bash Script
3/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
The Linux Shell
Shellis a interface between OS and user. It provides :
A facility for launchingand managingcommands andprograms
An operating environment
Aprogramming language
8/9/2019 Ch10 Linux Shell Bash Script
4/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM 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 separateenvironments : parameters, variables, functions,
aliases
8/9/2019 Ch10 Linux Shell Bash Script
5/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Shell Environment Customize
Type of variables: local(shell variable),global(environment variable)
List all local variables : use set commandList all global variables : use env command
Default environment variables : PS1, PS2,
HOME, LOGNAME, SHELL, PATH, PAGER,
LPDEST, PWD, DISPLAY, MAIL,...
8/9/2019 Ch10 Linux Shell Bash Script
6/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM 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 localvariable becomes aglobalvariable
8/9/2019 Ch10 Linux Shell Bash Script
7/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Using the bash shell
Alt+Fn : switch between virtual consoles
gpm : mouse server daemon, use it to copy and pasteeven 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, *, ?
8/9/2019 Ch10 Linux Shell Bash Script
8/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Redirecting Input and Output
8/9/2019 Ch10 Linux Shell Bash Script
9/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Redirecting Input and Output
Redirect input : use ()
# ls l > list_file
( Use set o noclobber : prevent file overwriting )
Append : use (>>)
Redirect error : use (2>)
8/9/2019 Ch10 Linux Shell Bash Script
10/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Pipe and Back ticks (` ,`)
Pipe (|): command1 | command2
Output of command1 becomes input of command2
# ls l |grep sambaBack ticks (``) or $()
1. # which passwd
/usr/bin/passwd2. # ls l /usr/bin/passwd
3. # ls l `which passwd`
8/9/2019 Ch10 Linux Shell Bash Script
11/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM 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
8/9/2019 Ch10 Linux Shell Bash Script
12/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Shell Script
Shell script : is a textfile contains shell commands,functions, aliases, flow control structures, loops,comments
All comments begin with # but #! is used toidentify a commands interpreter
$ more example_script
#!/bin/bash/usr/bin/smbd -D
/usr/bin/nmbd -D
8/9/2019 Ch10 Linux Shell Bash Script
13/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Variables
Naming : not begin with a digit, usually in upper caseletters
Assigning : not SPACES around =
VAR=value : assign value string to VAR
VAR=`cmd` : the same VAR=$(cmd) ,assign outputofcmd to VAR
# VAR1=`ls /var/log | wc l`
# echo $VAR1
65
8/9/2019 Ch10 Linux Shell Bash Script
14/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Variables
Quotes (single, double) :
# VAR=Hello World# echo $VAR
Hello World
# echo $VAR
$VAR
8/9/2019 Ch10 Linux Shell Bash Script
15/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM 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
8/9/2019 Ch10 Linux Shell Bash Script
16/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Passing Information to Sript
On the command line, information can be passed toscript 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 (InternalFile Separator) variable
$@ A list of ALL parameters as separate string$# Number of parameters included on the command line
The shift command will shiftthe positional parametersone or more position from leftto right
8/9/2019 Ch10 Linux Shell Bash Script
17/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Flow control
Loop : do something more than one time
Loop commands : for ,while ,until
8/9/2019 Ch10 Linux Shell Bash Script
18/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
The for Loop
Syntax :
for in do
# list of commands to do
done
8/9/2019 Ch10 Linux Shell Bash Script
19/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
The for Loop Example
This script will rename all file .txt in current directoryto .html
#!/bin/bashforfile in $(ls *.txt)
do
newname=$(basename $file .txt).html;
mv $file $newname;
done
8/9/2019 Ch10 Linux Shell Bash Script
20/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
The while and until Loop
Syntax :while
do
# list of commands to do
done
--------------------------------------------------------
until
do# list of commands to do
done
8/9/2019 Ch10 Linux Shell Bash Script
21/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
The while Loop Example
count=0
while [ $count lt 4 ]
do
echo $countcount=$(($count +1))
done
Output :
0
1
2
3
8/9/2019 Ch10 Linux Shell Bash Script
22/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
The until Loop Example
count=0
until [ $count ge 4 ]
do
echo $countcount=$(($count +1))
done
Output :
01
2
3
8/9/2019 Ch10 Linux Shell Bash Script
23/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM 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 andassign the value of n to $? variable
8/9/2019 Ch10 Linux Shell Bash Script
24/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Tests and Conditions
Test : use [ ] around expression
If-then-else structure:
if [ ] # include SPACEsthen
#commands to do ifthe exp1 is true
else #commands to do ifthe exp1 is NOT true
fi
8/9/2019 Ch10 Linux Shell Bash Script
25/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
case Structure
case expression in
pattern1 )
action
;;
pattern2)
action
;;
esac
8/9/2019 Ch10 Linux Shell Bash Script
26/34
8/9/2019 Ch10 Linux Shell Bash Script
27/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Input Interactive
We can input information into script when executingthe script
Commands :
read
select
8/9/2019 Ch10 Linux Shell Bash Script
28/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM 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 lastvariable.
8/9/2019 Ch10 Linux Shell Bash Script
29/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
read Command Example
#!/bin/bashecho Enter 2 number, I will add them
read VAR1 VAR2
echo $VAR1 + $VAR2 = $(($VAR1+$VAR2))
8/9/2019 Ch10 Linux Shell Bash Script
30/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
select Command
It is great for creating menu
Syntax :
select in
do
# commands
done
8/9/2019 Ch10 Linux Shell Bash Script
31/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Case Command example
#!/bin/bash
VAR=$(ls)
select i in $VAR
do
echo $i;
exit;done
8/9/2019 Ch10 Linux Shell Bash Script
32/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Functions
Syntax :
functionfunction_name ()
{
#commands}
Or
function_name ()
{
#commands
}
8/9/2019 Ch10 Linux Shell Bash Script
33/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Functions
Functions can be called in main script by functionsname.
It inherits ALL parameters in the main script
We can change the return code of the function by using
return n command
8/9/2019 Ch10 Linux Shell Bash Script
34/34
SAIGONLAB 69-3 Nguyen Thi Nho, P9, Q.TBinh, Tp. HCM LPI 102SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Summary
Step 1 : create script file (cat, vi, mc, ),enter script
codes.Step 2 : add execute permission mode to file ( chmod u+xfile )
Step 3 : run it (add script directory to PATHenvironment or use absolute path)