Shell脚本编程——下

文章目录
  1. 1. 运算符
    1. 1.1. 算术运算符
    2. 1.2. 关系运算符
    3. 1.3. 布尔运算符
    4. 1.4. 字符串运算符
    5. 1.5. 文件测试运算符
  2. 2. 条件
    1. 2.1. if…fi
    2. 2.2. if…else…fi
    3. 2.3. if…elif…fi
    4. 2.4. test
    5. 2.5. case
  3. 3. 循环
    1. 3.1. for
    2. 3.2. while
    3. 3.3. until
    4. 3.4. for_break
    5. 3.5. while_break
    6. 3.6. for_continue
    7. 3.7. while_continue
  4. 4. 函数
    1. 4.1. hello
    2. 4.2. return
    3. 4.3. two_function
    4. 4.4. 参数
  5. 5. 源码分享
  6. 6. 书签

运算符

算术运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh

a=10
b=20
echo "a=$a, b=$b"
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi

关系运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh

a=10
b=20
echo "a=$a, b=$b"
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi

布尔运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/sh

a=10
b=20
echo "a=$a, b=$b"
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
echo "$a -lt 100 -a $b -gt 15 : returns true"
else
echo "$a -lt 100 -a $b -gt 15 : returns false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

字符串运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh

a="abc"
b="efg"
echo "a=$a, b=$b"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi
if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi

文件测试运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh

file="../start/helloworld.sh"
if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi
if [ -s $file ]
then
echo "File size is not zero"
else
echo "File size is zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi

条件

if…fi

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh

a=10
b=20
echo "a=$a, b=$b"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi

if…else…fi

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh

a=10
b=20
echo "a=$a, b=$b"
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi

if…elif…fi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh

a=10
b=20
echo "a=$a, b=$b"
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi

test

test 命令用于检查某个条件是否成立,与方括号[]类似。

1
2
3
4
5
6
7
8
9
10
#!/bin/sh

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi

case

case … esac 与其他语言中的 switch … case 语句类似,是一种多分枝选择结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh

echo -e "Input a number between 1 to 4: \c"
read aNum
case $aNum in
1) echo 'You select 1'
;;
2) echo 'You select 2'
;;
3) echo 'You select 3'
;;
4) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

option="${1}"
case ${option} in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
esac

循环

for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash

for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done

for str in 'This is a string'
do
echo $str
done

for FILE in $HOME/.bash*
do
echo $FILE
done

while

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash

COUNTER=0
while ( $COUNTER <= 5 )
do
COUNTER=`expr $COUNTER + 1`
echo $COUNTER
done

while [ $COUNTER -lt 10 ]
do
COUNTER=`expr $COUNTER + 1`
echo $COUNTER
done

echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film: '
while read FILM
do
echo "Yeah! great film the $FILM"
done

until

1
2
3
4
5
6
7
8
#!/bin/bash

a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done

for_break

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

for var1 in 1 2 3
do
for var2 in 0 5
do
if [ $var1 -eq 2 -a $var2 -eq 0 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done

while_break

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

while :
do
echo -n "Input a number between 1 to 5: "
read aNum
case $aNum in
1|2|3|4|5) echo "Your number is $aNum!"
;;
*) echo "You do not select a number between 1 to 5, game is over!"
break
;;
esac
done

for_continue

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done

while_continue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

while :
do
echo -n "Input a number between 1 to 5: "
read aNum
case $aNum in
1|2|3|4|5) echo "Your number is $aNum!"
;;
*) echo "You do not select a number between 1 to 5!"
continue
echo "Game is over!"
;;
esac
done

函数

hello

1
2
3
4
5
6
7
8
9
#!/bin/bash

# Define your function here
Hello () {
echo "http://www.voidking.com"
}

# Invoke your function
Hello

return

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash

funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
}

funWithReturn

# Capture value returnd by last command
ret=$?
echo "The sum of two numbers is $ret !"

two_function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash

# Calling one function from another
number_one () {
echo "Url_1 is http://www.voidking.com"
number_two
}
number_two () {
echo "Url_2 is http://www.baidu.com"
}
number_one

number_three () {
echo "Url_3 is http://www.baidu.com"
}

unset -f number_three

参数

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $4 !"
# 参数个数
echo "The amount of the parameters is $# !"
# 传递给函数的所有参数
echo "The string of the parameters is $* !"
}
funWithParam 1 2 3 4

源码分享

https://github.com/voidking/shell.git

书签

Linux Shell脚本教程:30分钟玩转Shell脚本编程
http://c.biancheng.net/cpp/shell/