Ch10 Linux Shell Bash Script
Ch10 Linux Shell Bash Script
Ch10 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
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
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
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
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
/etc/profile
~/.bash_profile, ~/.bash_login, ~/.profile set : define a new variable
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
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)
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
Redirect input : use (<) or (<0) # mail [email protected] < 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
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
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
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
Variables
Naming : not begin with a digit, usually in upper case letters
65
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
Variables
# 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
Variable Notation
Expand variables : use ${VAR}
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
Flow control
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
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
#!/bin/bash
for file in $(ls *.txt) do
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
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
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
then
#commands to do if the exp1 is true else
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
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
Input Interactive
read
select
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
read Command
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
#!/bin/bash
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
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
Nguy n ThThi NhNho, , P9, Q.T n Bnh, Tp.Tp. HCM SAIGONLAB 83 69-3 Nguyen P9, Q.TBinh, HCM SAIGONLAB
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
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
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