0% found this document useful (0 votes)
22 views32 pages

The Linux Shell and BASH Scripting

Uploaded by

tuanbuianh3286
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views32 pages

The Linux Shell and BASH Scripting

Uploaded by

tuanbuianh3286
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

The Saigon CTT

The Linux Shell and


BASH Scripting
 Objectives
The Saigon CTT

 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
 Example of Script
The Saigon CTT

$ more example_script
#!/bin/bash
/usr/bin/nmbd -D
/usr/bin/smbd -D
 The Linux Shell
The Saigon CTT

 Shells : Bourne (sh), Bourne Again


(bash), Korn (ksh), C shell (csh, tcsh)

 Programs start from command line have


separate environments : parameters,
variables , functions.
 The Linux Shell
The Saigon CTT

 Shells : Bourne (sh), Bourne Again


(bash), Korn (ksh), C shell (csh, tcsh)

 Programs start from command line have


separate environments : parameters,
variables , functions.
 Shell Environment Customize
 bash config files :
The Saigon CTT

• /etc/profile
• ~/.bash_profile,
~/.bash_login,
~/.profile, ..
 Default environment variables : PS1, PS2,
HOME, LOGNAME, SHELL, PATH,
PAGER, LPDEST, PWD, DISPLAY,
MAIL, ..

 set, unset, export, … commands


 Using the bash shell
The Saigon CTT

 Alt+Fn

 gpm : mouse server deamon

 up and down keys (~/.bash_history )

 Ctrl+Z, Ctrl+C, *, ?, …
 Redirecting Input and Output
The Saigon CTT

 Input (<) or (<0)


#mail [email protected] < file_name
 Output (>) or (1>)
# ls –l > file_name
( set –C or set –o noclobber : prevent
overwrite )
 Append (>>)
 Error (2>)
 Redirecting Input and Output
The Saigon CTT

 Pipe (|)
#ls –l *.JPG | tee file_name
#ls –al /root | grep error >error_file

 Back ticks (`) or “$()”


# which passwd
/usr/bin/passwd
# ls –l `which passwd`
-r-sr-xr-x 1 root root 13476 Aug 7 /usr/bin/passwd
 Redirecting Input and Output
The Saigon CTT

 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
 Background jobs
The Saigon CTT

 Job
#ls –l *.JPG | tee file_name
#ls –al /root | grep error >error_file

 Backround jobs :
- Append with (&)
or
- Ctrl+Z and #bg %job_id
 Variables
The Saigon CTT

 Naming : not begin with a digit


 Assigning : VAR=value
VAR=$(command)
VAR=`command`
Note : not SPACES around “=”

Ex: # VAR=“Hello World”


 Variables
The Saigon CTT

 Quotes and Command Substitution

Note: # VAR=“Hello World”


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

# VAR1=`ls /var/log | wc –l`


# echo $VAR1
65
 Variable Notation
 Expand variables : use ${VAR}
The Saigon CTT

# VAR1=“This is a String1”
# VAR2=“$VAR1xyz”
# VAR3=“${VAR1}xyz”
# VAR4=‘${VAR1}xyz’
# echo $VAR1; echo $VAR2; echo $VAR3
# echo $VAR4
This is a String1
Nothing # default
This is a String1xyz
 Passing Info to Sript
The Saigon CTT

 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.
 Flow control
The Saigon CTT

 Loops : do something more than one

 Loop commands : for, while, until


 The for loop
The Saigon CTT

 Syntax :
for <variable> in <list>
do
#list of commands to do
done
 The for loop Example
The Saigon CTT

#!/bin/bash
for file in $(ls *.txt)
do
newname=“$(basename $file .txt).html”
mv $file $newname
done
 The while and until loop
The Saigon CTT

 Syntax :
while <condition>
do
#list of commands to do
done

until <condition>
do
#list of commands to do
done
 The while loop Example
count=0
The Saigon CTT

while [$count –lt 4]


do
echo $count
count=$((count +1))
done
Output:
0
1
2
 Return codes/Exit status
The Saigon CTT

 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
the variable $?
 Tests and Conditions (p.10-18)
The Saigon CTT

 Test : use “[ ]” around expression


 if-then-else :
if [ <exp1> ] # include SPACEs
then
# what to do if the exp1 is true
elseif [ <exp2> ]
then
# what to do if the exp2 is true
else
fi
 Tests and Conditions
The Saigon CTT

 Case test:
case expression in
pattern1 )
action
;;
pattern2 )
action
;;

esac
 case test Example
while [ $1 ]
The Saigon CTT

do
echo –n “$1 hits the “
case $1 in
a?c | ab? )
echo “first case.” ;;
abcde )
echo “second case.” ;;
abc123 )
echo “third case.” ;;
* )
echo “fallout case.” ;;
esac
shift
done
 Input
The Saigon CTT

 We can input the information into script


when executing the script ( interactive)

 Commands :
read
select
 Input - read
The Saigon CTT

 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.
 Input – read Example
The Saigon CTT

#!/bin/bash

echo “Enter 2 number, i will add them”

read VAR1 VAR2

echo “$VAR1 + $VAR2 =$(($VAR1+$VAR2))”


 Input - select
The Saigon CTT

 It is great for creating menu

 Syntax :

select VAR [in list]

do

#commands that use $VAR

done
 Functions
The Saigon CTT

Syntax:
function function_name
{
}
Or
function_name ()
{
}
 Functions
The Saigon CTT

 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
 Advanced bash Concepts
The Saigon CTT

 Arithmetic Expression :
 Arrays : var[subscript]
 More thrilling Examples
for file in $(ls file*)
do
mv $file ${file%html}txt
done
 Summary
The Saigon CTT

Step 1 : create script file (cat, vi, mc, …),


enter script codes.

Step 2 : add execute permission mode to


file ( chmod +x file )

Step 3 : run it (add script directory to


PATH environment or use absolute path)

You might also like