1、case ... in .... esac

case ... in .... esac ,他的语法如下:
case $变量名称 in
"第一个变量内容")
程序段
;;
"第二个变量内容")
程序段
;;
*)
不包含第一个变量内容与第二个变量内容的其它程序执行段
exit 1
;;
esac
要注意的是,这个语法是以 case 为开头,而以 esac 为结尾,

写个程序:让使用者能够输入 one, two, three , 并且将使用者的变量显示到屏幕上,如果不是 one, two, three 时,就告知使用者仅有这三种选择。
[root@linux scripts]# vi sh11.sh
#!/bin/bash
# Program:
# Let user input one, two, three and show in screen.
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "This program will print your selection !"
# read -p "Input your choice: " choice
# case $choice in
case $1 in
"one")
echo "Your choice is ONE"
;;
"two")
echo "Your choice is TWO"
;;
"three")
echo "Your choice is THREE"
;;
*)
echo "Usage {one|two|three}"
;;
esac
此时,您可以使用『 sh sh11.sh two 』的方式来下达指令,就可以收到相对应的响应了。 上面使用的是直接下达的方式,而如果使用的是交互式时,那么将上面第 10, 11 行的 "#" 拿掉, 并将 12 行加上批注 (#)

实练:

1 #!/bin/bash
  2
  3
  4 echo "practise to use "one" "two" "three""
  5 read -p "input your choise (from the up suggest):" answer
  6
  7 case "$answer" in
  8         "one")
  9         echo "your input is 'one', that is $answer"
 10         ;;
 11         "two")
 12         echo "your input is 'two', that is $answer"
 13         ;;
 14         "three")
 15         echo "your input is 'three', that is $answer"
 16         ;;
 17 *)
 18 echo "$answer is wrong,{you only have the hinted  three choices}"
 19 ;;
 20 esac

Loong:/home/yee/shell# sh case_practise.sh
practise to use one two three
input your choise (from the up suggest):one
your input is 'one', that is one
Loong:/home/yee/shell# sh case_practise.sh
practise to use one two three
input your choise (from the up suggest):two
your input is 'two', that is two
Loong:/home/yee/shell# sh case_practise.sh
practise to use one two three
input your choise (from the up suggest):three
your input is 'three', that is three
Loong:/home/yee/shell# sh case_practise.sh
practise to use one two three
input your choise (from the up suggest):xxxx
xxxx is wrong,{you only have the hinted  three choices}
Loong:/home/yee/shell#

2、function

function 也是拥有内建变量的~他的内建变量与 shell script 很类似, 函数名称代表示 $0 ,而后续接的变量也是以 $1, $2... 来取代的~ 这里很容易搞错喔~因为『 function fname() { 程序段 } 』内的 $0, $1... 等等与 shell script 的 $0 是不同的。以上面 sh11-2.sh 来说,假如我下达:『 sh sh11-2.sh one 』 这表示在 shell script 内的 $1 为 "one" 这个字符串。但是在 printit() 内的 $1 则与这个 one 无关。 我们将上面的例子再次的改写一下,让您更清楚!
[root@linux scripts]# vi sh11-3.sh
#!/bin/bash
# Program:
# Let user input one, two, three and show in screen.
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
function printit(){
echo "Your choice is $1"
}
echo "This program will print your selection !"
case $1 in
"one")
printit 1
;;
"two")
printit 2
;;
"three")
printit 3
;;
*)
echo "Usage {one|two|three}"
;;
esac
在上面的例子当中,如果您输入『 sh sh11-3.sh one 』就会出现『 Your choice is 1 』的字样~ 为什么是 1 呢?因为在程序段落当中,我们是写了『 printit 1 』那个 1 就会成为 function 当中的 $1

3、while done/until done

while do done, until do done
一般来说,循环最常见的就是底下这两种状态了:
while [ condition ]
do
程序段落
done
这种方式中, while 是『当....时』,所以,这种方式说的是『当 condition 条件成立时,就进行循环,直到 condition 的条件不成立才停止』的意思。
until [ condition ]
do
程序段落
done
这种方式恰恰与 while 相反,它说的是『当 condition 条件成立时,就终止循环, 否则就持续进行循环的程序段。』

如果我想要计算 1+2+3+....+100 这个数据呢? 利用循环啊~他是这样的:
[root@linux scripts]# vi sh13.sh
#!/bin/bash
# Program:
# Try to use loop to calculate the result "1+2+3...+100"
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
s=0
i=0
while [ "$i" != "100" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"
嘿嘿!当您执行了『 sh sh13.sh 』之后,就可以得到 5050 这个数据才对啊!这样瞭呼~ 那么让您自行做一下,如果想要让使用者自行输入一个数字,让程序由 1+2+... 直到您输入的数字为止, 该如何撰写呢?

实练:

版本一:while done

1 #!/bin/bash
  2
  3 read -p "please input the stop number:" input
  4 s=0
  5 i=0
  6 while [ "$i" != "$input" ]
  7 do
  8         i=$(($i+1));# echo "i=$i"
  9         s=$(($s+$i));# echo "s=$s"
 10 done
 11 echo "the result is: $s"

Loong:/home/yee/shell# sh self_add.sh
please input the stop number:100
the result is: 5050
Loong:/home/yee/shell#

版本二:until done

  1 #!/bin/bash
  2
  3 read -p "please input the stop number:" input
  4 s=0
  5 i=0
  6 until [ "$i" = "$input" ]                                 #只需修改两个地方即可。
  7 do
  8         i=$(($i+1));# echo "i=$i"
  9         s=$(($s+$i));# echo "s=$s"
 10 done
 11 echo "the result is: $s"
~                           
please input the stop number:50
the result is: 1275
Loong:/home/yee/shell# sh self_add2.sh
please input the stop number:100
the result is: 5050
Loong:/home/yee/shell# sh self_add2.sh
please input the stop number:4
the result is: 10
Loong:/home/yee/shell#

4、for...do....done

for 这种语法,则是『 已经知道要进行几次循环』的状态!他的语法是:
for (( 初始值; 限制值; 执行步阶 ))
do
程序段
done
这种语法适合于数值方式的运算当中,在 for 后面的括号内的三串内容意义为:
• 初始值:某个变量在循环当中的起始值,直接以类似 i=1 设定好;
• 限制值:当变量的值在这个限制值的范围内,就继续进行循环。例如 i<=100;
• 执行步阶:每作一次循环时,变量的变化量。例如 i=i+1。
值得注意的是,在『执行步阶』的设定上,如果每次增加 1 ,则可以使用类似『i++』的方式,亦即是 i 每次循环都会增加一的意思。好,我们以这种方式来进行 1 累加到 100 的循环吧!
[root@linux scripts]# vi sh14.sh
#!/bin/bash
# Program:
# Try do calculate 1+2+....+100
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is==> $s"

还可以利用底下的方式来进行非数字方面的循环运作喔!
for var in con1 con2 con3 ...
do
程序段
done
以上面的例子来说,这个 $var 的变量内容在循环工作时:
1. 第一次循环时, $var 的内容为 con1 ;
2. 第二次循环时, $var 的内容为 con2 ;
3. 第三次循环时, $var 的内容为 con3 ;
4. ....
我们可以做个简单的练习。假设我有三种动物,分别是 dog, cat, elephant 三种, 我想每一行都输出这样:『There are dogs...』之类的字样,则可以:
[root@linux scripts]# vi sh15.sh
#!/bin/bash
# Program:
# Using for .... loop to print 3 animal
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
for animal in dog cat elephant
do
echo "There are ""$animal""s.... "
done

另外,依次测试目录可以如下方式:

filelist=`ls $dir`
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm "
done

5、shell script 的追踪与 debug

[root@linux ~]# sh [-nvx] scripts.sh
参数:
-n :不要执行 script,仅查询语法的问题;
-v :再执行 sccript 前,先将 scripts 的内容输出到屏幕上;
-x :将使用到的 script 内容显示到屏幕上,这是很有用的参数!
范例:
范例一:测试 sh16.sh 有无语法的问题?
[root@linux ~]# sh -n sh16.sh
# 若语法没有问题,则不会显示任何信息!
范例二:将 sh15.sh 的执行过程全部列出来~
[root@linux ~]# sh -x sh15.sh
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/vbird/bin
+ export PATH
+ for animal in dog cat elephant
+ echo 'There are dogs.... '
There are dogs....
+ for animal in dog cat elephant
+ echo 'There are cats.... '
There are cats....
+ for animal in dog cat elephant
+ echo 'There are elephants.... '
There are elephants....
# 使用 -x 真的是追踪 script 的好方法,他可以将所有有执行的程序段在执行前列出来,
# 如果是程序段落,则输出时,最前面会加上 + 字号,表示他是程序代码而已,
# 实际的输出则与 standard output 有关啊~如上所示。
在上面的范例二当中,我们可以透过这个简单的参数 -x 来达成 debug 的目的,这可是一个不可多得的参数, 通常如果您执行 script 却发生问题时,利用这个 -x 参数,就可以知道问题是发生在哪一行上面了!



摘自 《鸟哥的私房菜》

posted on 2022-07-05 18:13  我在全球村  阅读(155)  评论(0编辑  收藏  举报