The Linux Shell and BASH Scripting

Chia sẻ bởi Nguyễn Việt Vương | Ngày 29/04/2019 | 76

Chia sẻ tài liệu: The Linux Shell and BASH Scripting thuộc Bài giảng khác

Nội dung tài liệu:

The Linux Shell and BASH Scripting
Objectives
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
$ more example_script
#!/bin/bash
/usr/bin/nmbd -D
/usr/bin/smbd -D
The Linux Shell
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
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 :
/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
Alt+Fn
gpm : mouse server deamon
up and down keys (~/.bash_history )
Ctrl+Z, Ctrl+C, *, ?, …
Redirecting Input and Output
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
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
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
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
Naming : not begin with a digit
Assigning : VAR=value
VAR=$(command)
VAR=`command`
Note : not SPACES around “=”

Ex: # VAR=“Hello World”
Variables
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}
# 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
${VAR1}xyz
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.
Flow control
Loops : do something more than one
Loop commands : for, while, until
The for loop
Syntax :
for in
do
#list of commands to do
done
The for loop Example
#!/bin/bash
for file in $(ls *.txt)
do
newname=“$(basename $file .txt).html”
mv $file $newname
done
The while and until loop
Syntax :
while
do
#list of commands to do
done

until
do
#list of commands to do
done
The while loop Example
count=0
while [$count –lt 4]
do
echo $count
count=$((count +1))
done
Output:
0
1
2
3
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 the variable $?
Tests and Conditions (p.10-18)
Test : use “[ ]” around expression
if-then-else :
if [ ] # include SPACEs
then
# what to do if the exp1 is true
elseif [ ]
then
# what to do if the exp2 is true
else
fi
Tests and Conditions
Case test:
case expression in
pattern1 )
action
;;
pattern2 )
action
;;

esac
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 “fallout case.” ;;
esac
shift
done
Input
We can input the information into script when executing the script ( interactive)

Commands :
read
select

Input - read
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
#!/bin/bash
echo “Enter 2 number, i will add them”
read VAR1 VAR2
echo “$VAR1 + $VAR2 =$(($VAR1+$VAR2))”
Input - select
It is great for creating menu
Syntax :
select VAR [in list]
do
#commands that use $VAR
done
Functions
Syntax:
function function_name
{
}
Or
function_name ()
{
}
Functions
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
Arithmetic Expression :
Arrays : var[subscript]
More thrilling Examples
for file in $(ls file*)
do
mv $file ${file%html}txt
done
Summary
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)
* Một số tài liệu cũ có thể bị lỗi font khi hiển thị do dùng bộ mã không phải Unikey ...

Người chia sẻ: Nguyễn Việt Vương
Dung lượng: | Lượt tài: 5
Loại file:
Nguồn : Chưa rõ
(Tài liệu chưa được thẩm định)