Identify differrent Linux shell environments Understand the redirection of input Understand and utilize command substitution Write and configure BASH script using variables, flow
Trang 2 Identify differrent Linux shell environments
Understand the redirection of input
Understand and utilize command substitution
Write and configure BASH script using
variables, flow controls interactive input, functions, arithmetic and arrays
Trang 4 Shells : Bourne (sh), Bourne Again
( bash ), Korn (ksh), C shell (csh, tcsh)
Programs start from command line have
separate environments : parameters,
variables , functions.
Trang 5 Shells : Bourne (sh), Bourne Again
( bash ), Korn (ksh), C shell (csh, tcsh)
Programs start from command line have
separate environments : parameters,
variables , functions.
Trang 6 bash config files :
~/.profile,
Default environment variables : PS1, PS2,
HOME, LOGNAME, SHELL, PATH, PAGER, LPDEST, PWD, DISPLAY, MAIL,
set, unset, export, … commands
Trang 7 gpm : mouse server deamon
up and down keys (~/.bash_history )
Ctrl+Z, Ctrl+C, *, ?, …
Trang 10 Sometimes the output of a command is a list (ex: ls) and we wish to execute
another command on each of the entries, use xargs
#ls | xargs grep README
Do Not use :
#ls –al | xarg grep README
Trang 12Note : not SPACES around “=” Ex: # VAR=“Hello World”
Trang 13 Quotes and Command Substitution
Note: # VAR=“Hello World”
# echo “ $VAR ” Hello World
# echo ‘ $VAR ’ $VAR
# VAR1= ` ls /var/log | wc –l `
# echo $VAR1
65
Trang 15 Passing Info to Sript
On the command line, info can be passed to script through pre-set variables called postion parameter.
$0 The name of script
$1-$9 Parameters being passed to script
$* String contains ALL parameters
passed to script separated by the first chacracter in IFS
$@ A list of ALL as separate string
$# Number of parameters on included
the command line
The shift command will shift the positional
parameters one or more position to the left or right.
Trang 16 Loops : do something more than one
Loop commands : for, while, until
Trang 19until <condition>
do
#list of commands to do
done
Trang 20echo $count count=$((count +1))
done
Output:
0 1 2 3
Trang 21 The variable $? contains the return code of the previous executed command or
application
≠0 Failure
The exit n command will cause the
script to quit and assign the value of n to
the variable $?
Trang 22 Test : use “[ ]” around expression
# what to do if the exp2 is true
else fi
Trang 24echo –n “$1 hits the “ case $1 in
Trang 26read VAR1 VAR2 …
If there is more input than you are
looking for, all the extras are put in the
last variable.
Trang 29function_name ()
{ }
Trang 30 Functions can be called in the main
script by function’s name, parameters are given on the command line.
It inherits ALL variables in main script
We can change the return code of
function by using return n command
Trang 31 More thrilling Examples
for file in $(ls file*) do
mv $file ${file%html}txt done
Trang 32Step 3 : run it (add script directory to
PATH environment or use absolute path)