Lesson 27 -Shell advanced programming scripts
28.1 What is the script
Is a script that contains a series of command sequence of text files (which can be edited by the editor, and can be performed). When you run this script file, the file contains a sequence of commands will be executed automatically. For example, we create seven files, you can continue to create seven files, you can also create a script file (.sh file).
28.2 Scripting technology
1. Basic Structure
(1) parser: #! The latter part, which means that the script parser to operate;
(2) the command sequence: a large number of commands.
2. Variable
shell script allows the user to set up and use their own variables, which can make a number or string, the user need not specify its type, and need not be defined before use. An assignment of the right and left without spaces , comments begin with #.
#!/bin/sh
a=”hello world”
b=5
echo “A is: $a”
echo “B is %b”
NOTE: Before running the program we will find that wrong, because we are using different files in the file and linux under windows. So, we switched by dos2unix shell.sh. We want to install the appropriate rpm package before converting.
3. Parameters
Like C program, shell script can use the command-line parameters
$ #: The number of command-line parameters into the script
$ *: All command line parameter values between the individual parameter values left blank
$ 0: command itself (shell itself)
$ 1: The first command-line argument
$ 2: The second command-line argument
E.g:
#2.3
echo "number of var:" $#
echo "value of vars" $*
echo "name of script" $0
echo "value of var1" $1
echo "value of var2" $2
After we enter the command ./shell.sh a 3, shows
number of var:2
value of vars a 3
name of script ./shell.sh
value of var1 a
value of var2 3
4. The digital computing
shell script provides a special command expr to calculate mathematical expressions, such as expre 5 + 1, but when the results of the calculation should replicate to other variables, you must also use backticks. ~ Line following symbols is `
was expr = `20 / 10`
E.g:
#2.4
var1 = 10
var2 = 20
var3 = `expr $ var1 / $ var2`
Var4 = `expr $ var1 + $ var2`
echo $ var3
echo $ var4
The result: 0
30
5. Process control
(1) shell script is a basic flow control statements if-then, using the format:
if [condition] (Special note: the left and right sides of the square brackets must have spaces)
then
commands
else
commands
be
l conditions are
Compare operation |
Integer operations |
String Manipulation |
the same |
-eq |
= |
different |
-born |
!= |
more than the |
-gt |
> |
Less than |
-lt |
< |
greater than or equal to |
-give |
|
less than or equal to |
-the |
|
Is empty |
-with |
|
not null |
-n |
|
Example:
Comparison of integers a and b are equal: if [$ a = $ b] (EQ can also be used)
Determining the integer a is greater than an integer b: if [$ a -gt $ b]
Compare strings a and b are equal: if [$ a = $ b]
Determining whether a character string is empty: if [-z $ a]
Analyzing integer variable a is greater than b: if [$ a -gt $ b]
note:
l in the left and right parentheses are left blank
l "=" has left space
For file directory
-e file already exists
-f file is a regular file
-s file size is not zero
-d file is a directory
-r file for the current user can read
-w file for the current user can write
-x file can be executed for the current user
#!/bin/sh
folder=/home
[-R "$ folder"] && echo "Can read $ folder" of the current surface condition is true perform the following operations
[-F "$ folder"] || echo conditions, the current face of "this is not file" to perform operations when the latter is false
Example:
was = 10
if [ $var -gt 5 ]
then
echo "the value is qreater then 5"
be
Operating results: the value is qreater then 5
(2) for statement
shell script provides for statement, c programming language used to create a similar statement for the same cycle. Using the format:
for var in list
do
commands
done
Example:
#!/bin/bash
list="Sun Mon Tue Wed Thur Fri Sat"
for day in $list
do
echo $day
done
operation result:
Sun
Mon
Tue
Wed
Thur
Fri
Sat
(3) while statement
shell script provides a while statement, c programming language used to create a similar statement while in the same cycle. Using the format:
while condition
do
commands
done
Example:
#!/bin/bash
was = 5
while [ $var -gt 0 ]
do
echo $ var
var = `expr $ was - 1`
done
operation result:
5
4
3
2
1
6. sed Editor
Sed (Stream Editior): AKA stream editor lines (Hang) editor, editing a time line of the format:
sed [Options] 'command' file name
Common Options:
-n: after the specified processing line only shows the
-e: Make multiple editing tasks
-i: directly modify the contents of the file to read, instead of the output from the screen
l Common command options:
p print matching lines
a new
c alternative line
d delete positioning line
Some parts of the line s instead of
l sed example
These changes are in addition to adding -i no effect on the original file.
(1) show
sed -n '2p' tmp.txt only the display line 2
sed -n '1,3p' tmp.txt print line 1 to line 3 -F
sed -n '/ mov /' p tmp.txt containing movie print line
(2) deleted
sed '2d' tmp.txt delete row 2 (print out the rest)
sed '3, $ d' tmp.txt delete the third line to the last line, $: The last line
(3) query
sed -n '/ hello / p' tmp.txt including keyword query all the rows of hello
(4) instead of
sed '1c Hi' tmp.txt first row in place of the Hi
sed -n ‘/hello/p’ tmp.txt |sed ‘sed ‘s/hello/bird/g’
(5) inserted
sed -i '$ a bye' tmp.txt inserted in the last row bye
7. awk analyzer
awk is a powerful text analysis tool that reads the file line by line, with a space for the default delimiters dividing each line into multiple fields.
Use: awk '{pattern + action}' filenames
To find the contents of pattern
When a match is found the command action performed
example:
last -n 5 | awk '{print $ 1}' last -n 5 indicates the most recent five user login.
cat / etc / passwd | awk -F ':' '{print $ 1}' -F indicates the specified delimiter
awk –F ‘:’ ‘$1==”root” {print $0}’ /etc/passwd
28.3 integrated example
qcd analysis
qcd.sh
qcd ()
{
mkdir -p /.qcd/tmp
[ -x /.qcd/tmp/qcd_tmp ] || touch /.qcd/tmp/qcd_tmp
chmod 700 /.qcd/tmp
QD=/.qcd/tmp/qcd_tmp
export QD
/usr/local/sbin/qcde "$@"
[ ! `cat $QD` ] || cd "`cat $QD`"
rm -f "$QD"
unset QD;
}
install
#!/bin/sh
#qcd install
#written by xiewei 2004
setup_content=/etc/qcd
history_dir=$setup_content/history_dir
bin_content=/usr/local/sbin
prof_content=/etc/profile.d
setup()
{
#check
if [-r $ history_dir] # $ history_dir If the current user-readable
then
echo -n "You have installed qcd , overwrite it(y\Y or q\Q or n\N)? "
while read choice
do
if [ "$ choice" = "y" -o "$ choice" = "Y"] # to rewrite the installation path
then
break
be
if [ "$ choice" = "q" -o "$ choice" = "Q"] # do nothing exit
then
echo "Nothing to do!"
exit 1
be
if [ "$ choice" = "n" -o "$ choice" = "N"] # installed in the default position
then
cp -f ./qcd $bin_content/
cp -f ./qcd.sh $prof_content/
echo "install qcd OK, but do not overwrite it!"
echo "version is 1.00"
exit 1
be
echo -n "You have installed qcd, overwrite it(y\Y or q\Q or n\N)? "
done
be
if [ -r $setup_content ]
then
:
else
mkdir $setup_content
be
cp -f ./qcde $bin_content/
cp -f ./history_dir $setup_content/
cp -f ./qcd.sh $prof_content/
}
delete()
{
[ -r $history_dir ] || [ -r $bin_content ] || \
! echo "Your computer has not qcd!" || ! echo "Nothing to do!"
echo -n "Are you sure to delete qcd(y\Y or q\Q)? "
while read choice
do
if [ "$choice" = "y" -o "$choice" = "Y" ]
then
break
be
if [ "$choice" = "q" -o "$choice" = "Q" ]
then
echo "Nothing to do!"
exit 1
be
echo -n "Are you sure to delete qcd(y\Y or q\Q)? "
done
rm -rf $setup_content
rm -f $bin_content/qcd
rm -f $prof_content/qcd.sh
}
usage()
{
echo "<install> install qcd on your computer."
echo "<install del> delete qcd from your computer."
exit 1
}
echo "Qcd Install Software"
echo "Written By XieWei 2004"
if [$ # -eq 0] # if the number of parameters passed to 0? then
setup
echo "install qcd OK!"
echo "version is 1.00"
exit 1
be
if [$ # -gt 1] # number of arguments is greater than 0 if the incoming
then
usage
be
case $ 1 in # if the first argument is del
of the)
delete
echo "have delete qcd OK!"
;;
*)
usage # prompt
;;
esac