哈哈哈
1,$IFS 和空白
vim IFSandNull.sh
1 #!/bin/bash
2 # $IFS 处理空白的方法,与处理其它字符不同.
3
4 output_args_one_per_line()
5 {
6 for arg
7 do echo "[$arg]"
8 done
9 }
10
11 echo; echo "IFS=\" \""
12 echo "-------"
13
14 IFS=" "
15 var="a b c"
16
17 output_args_one_per_line $var # output_args_one_per_line `echo " a b c "`
18 #
19 # [a]
20 # [b]
21 # [c]
22
23 echo; echo "IFS=:"
24 echo "-----"
25
26 IFS=:;
27 var=":a::b:c:::"
28 output_args_one_per_line $var
29
30 echo
31 exit 0
结果:
2,$REPLY read 命令如果没有给变量,那么输入将保存在$REPLY 中.在 select 菜单中也可用,但是只 提供选择的变量的项数,而不是变量本身的值.
1 #!/bin/bash
2 # reply.sh
3
4 # REPLY 是'read'命令结果保存的默认变量. \
5 echo
6 echo -n "What is your favorite vegetable? "
7 read
8
9 echo "Your favorite vegetable is $REPLY."
10 # 当且仅当在没有变量提供给"read"命令时,
11 #+ REPLY 才保存最后一个"read"命令读入的值.
12
13 echo
14 echo -n "What is your favorite fruit? "
15 read fruit
16 echo "Your favorite fruit is $fruit."
17 echo "but..."
18 echo "Value of \$REPLY is still $REPLY."
19 # $REPLY 还是保存着上一个 read 命令的值,
20 #+ 因为变量$fruit 被传入到了这个新的"read"命令中.
21
22 echo
23
24 exit 0
结果:
这个脚本已经运行的时间
vim runtime.sh
1 # !/bi/bash
2 # runtime.sh 脚本运行时间
3
4 TIME_LIMIT=10
5 INTERVAL=1
6
7 echo
8 echo "Hit Control-C to exit before $TIME_LIMIT seconds."
9 echo
10
11 while [ "$SECONDS" -le "$TIME_LIMIT" ]
12 do
13 if [ "$SECONDS" -eq 1 ]
14 then
15 units=second
16 else
17 units=seconds
18 fi
19
20 echo "This script has been running $SECONDS $units."
21 # 在一台比较慢的或者是负载很大的机器上,这个脚本可能会跳过几次循环
22 #+ 在一个 while 循环中.
23 sleep $INTERVAL
24 done
25
26 echo -e "\a" # Beep!
27
28 exit 0
结果: