Shell script programming

Shell introduction

 

Shell simply means that the command parser that converts the user input commands to machines able to perform procedures accordingly.

 

Shell script is a text file that contains a series of command sequences (batch processing). When you run this script file, the file contains a sequence of commands that will be executed.

 

HelloWorld

 

First line of Shell script must be in the following format: #!/bin/bash

 

Symbol #! is used to specify the script file parser. Example using bash, you can also use other shell. Such as #!/bin/sh.

 

When editing the script, must have the executable property. chmod +x filename

 

Shell script Hello.sh

 

view plaincopy to clipboardprint?
#!/bin/bash
echo “hello world!”
mkdir ./helloworld
#!/bin/bash
echo “hello world!”
mkdir ./helloworld

 

Variables in the Shell

 

In Shell programming, all variables are represented by strings, and does not require variable declaration in advance.

 

Shell script s1.sh

 

view plaincopy to clipboardprint?
#!/bin/sh
Behind the # sign is a comment
#set variable a
a=”hello”
#print a
echo “A is: $a”
#!/bin/sh
Behind the # sign is a comment
#set variable a
a=”hello”
#print a
echo “A is: $a”

 

Passing command line parameters

 

$#: Number of command line arguments passed the script

 

$*: All of the command line argument, between the values of the parameters with spaces

 

$0: the command itself (shell file name)

 

$1: the first command line argument

 

$2: the second command-line argument

 

Shell script s2.sh

 

view plaincopy to clipboardprint?
#!/bin/sh
echo “numer of vars:”$#
echo “values of vars:”$*
echo “value of var1:”$1
echo “value of var2:”$2
echo “value of var3:”$3
echo “value of var4:”$4
#!/bin/sh
echo “numer of vars:”$#
echo “values of vars:”$*
echo “value of var1:”$1
echo “value of var2:”$2
echo “value of var3:”$3
echo “value of var4:”$4

 

Running./s2.sh a b c d

 

Output:

 

numer of vars:4
values of vars:a b c d
value of var1:a
value of var2:b

 

value of var3:c

 

value of var4:d

 

The local variables in Shell

 

Shell script S3.sh

 

view plaincopy to clipboardprint?
#!/bin/bash
hello=”var1″
echo $hello
function funcl
{
local hello=”var2″
echo $hello
}
funcl
echo $hello
#!/bin/bash
hello=”var1″
echo $hello
function funcl
{
local hello=”var2″
echo $hello
}
funcl
echo $hello

 

When the variable is first assigned a value plus the local keyword declares a local variable

 

Note: (1) variable assignment, the = cannot have a space on both sides

 

(2) BASH the end statement does not require a semicolon

 

In the Shell control structure

 

If statement

 

if[expression]

 

then

 

#code block

 

if

 

if[expression]

 

then

 

#code block

 

else

 

#code block

 

fi

 

The comparison operators

 

Integer comparison operation operation string string integer operations operations operations operations
The same as-EQ = greater than or equal to-GE
Different-ne! = less than or equal to-Le
More than-gt >-z
Less than-Lt < not null-n

 

Use case:

 

Compare integer a is greater than the integer b:If[$ $ b] determine whether the string is null: If[-z $a]

 

Note:

 

(1) in the [ and ] symbols left and right with spaces (2) = there are spaces left

 

Shell script S4.sh

 

view plaincopy to clipboardprint?
#!/bin/bash
a=$1
b=$2
if [ -z $a ] || [ -z $b ]
then
echo “please enter 2 no”
exit 1
fi
if [ $a -eq $b ] ; then
echo “number a = number b”
else if [ $a -gt $b ]
then
echo “number a>number b”
elif [ $a -lt $b ]
then
echo “number a<number b”
fi
fi
#!/bin/bash
a=$1
b=$2
if [ -z $a ] || [ -z $b ]
then
echo “please enter 2 no”
exit 1
fi
if [ $a -eq $b ] ; then
echo “number a = number b”
else if [ $a -gt $b ]
then
echo “number a>number b”
elif [ $a -lt $b ]
then
echo “number a<number b”
fi
fi

 

Judge

 

-e 文件已经存在-f 文件是普通文件-s 文件大小不为零-d 文件是一个目录

 

-r 文件对当前用户可以读取-w 文件对当前用户可以写入-x 文件对当前用户可以执行

 

shell脚本s5. sh

 

view plaincopy to clipboardprint?
#!/bin/sh
folder =/home
[-r “$folder”] && echo “Can read $folder”
[-f “$folder”] || echo “this is not file”
#!/bin/sh
folder =/home
[-r “$folder”] && echo “Can read $folder”
[-f “$folder”] || echo “this is not file”

 

shell脚本s6. sh

 

view plaincopy to clipboardprint?
#!/bin/bash
DIR = $1
#if the string empty
if [“$DIR” = “”]
then
echo “usage: ‘ basename $0 ‘ directory to create” >&2
exit 1
fi
echo “dir” $DIR
if [-d $DIR]
then
echo “The directory already exist”
exit 0
else
echo “The directory does exist”
echo-n “Create is now? [Y/N]: “
read create
if [“$create” = “y”] || [“$create” = “Y”]
then
echo “creating now”
if [mkdir $DIR]
DIR = “”
fi

 

if [“$DIR” = “”]
then
echo “create directory success”
else
echo “create directory error”
fi
elif [“$create” = “n”] || [“$create” = “N”]
then
echo “does not create directory”
exit 0
else
echo “Errors order”
exit 1
fi
fi
#!/bin/bash
DIR = $1
#if the string empty
if [“$DIR” = “”]
then
echo “usage: ‘ basename $0 ‘ directory to create” >&2
exit 1
fi
echo “dir” $DIR
if [-d $DIR]
then
echo “The directory already exist”
exit 0
else
echo “The directory does exist”
echo-n “Create is now? [Y/N]: “
read create
if [“$create” = “y”] || [“$create” = “Y”]
then
echo “creating now”
if [mkdir $DIR]
DIR = “”
fi

 

if [“$DIR” = “”]
then
echo “create directory success”
else
echo “create directory error”
fi
elif [“$create” = “n”] || [“$create” = “N”]
then
echo “does not create directory”
exit 0
else
echo “Errors order”
exit 1
fi
fi

 

For loops

 

For loop construct in the c language is different, in the basic structure of for loop in bash

 

for var in [list]

 

do

 

#code lock

 

done

 

Where $var is the loop control variable, [list] is a collection of var traverse, do/done contains the body of the loop.

 

In addition, if you wrote for and do on the same line, must precede do ;.

 

Shell script S7.sh

 

view plaincopy to clipboardprint?