编程是一种思想。
现在时间是15点56,都没多少时间了
1, echo 一些诡异的变量
vim strange01.sh
1 #!/bin/bash
2 # weirdvars.sh: echo 诡异的变量
3
4 var="'(]\\{}\$\""
5 echo $var # '(]\{}$"
6 echo "$var" # '(]\{}$" 并没有什么不同
7
8 echo
9
10 IFS='\'
11 echo $var # '(] {}$" \ 转换成空格了?明显和 IFS 有关系么!又不傻!
12 echo "$var" # '(]\{}$"
13
14 exit 0
15
vim test504.sh
1 #!/bin/bash
2 # ex18.sh
3
4 # Does a 'whois domain-name' lookup on any of 3 alternate servers:
5 # ripe.net, cw.net, radb.net
6
7 # 把这个脚本重命名为'wh',然后放到/usr/local/bin 下
8
9 # 需要 3 个符号链接
10 # ln -s /usr/local/bin/wh /usr/local/bin/wh-ripe
11 # ln -s /usr/local/bin/wh /usr/local/bin/wh-cw
12 # ln -s /usr/local/bin/wh /usr/local/bin/wh-radb
13
14 E_NOARGS=65
15 if [ -z "$1" ]
16 then
17 echo "Usage: `basename $0` [domain-name]"
18 exit $E_NOARGS
19 fi
20
21 # Check script name and call proper server.
22 case `basename $0` in # Or: case ${0##*/} in
23 "wh" ) whois $1@whois.ripe.net;;
24 "wh-ripe") whois $1@whois.ripe.net;;
25 "wh-radb") whois $1@whois.radb.net;;
26 "wh-cw" ) whois $1@whois.cw.net;;
27 * ) echo "Usage: `basename $0` [domain-name]";;
28 esac
29
30 exit $?
转义符
vim escaped.sh:
1 #!/bin/bash
2 # escaped.sh: 转义符
3
4 echo; echo
5 echo "\v\v\v\v" # 逐字的打印\v\v\v\v .
6
7 # 使用-e 选项的 echo 命令来打印转义符
8 echo "============="
9
10 echo "VERTICAL TABS"
11 echo -e "\v\v\v\v" # Prints 4 vertical tabs.
12
13 echo "=============="
14 echo "QUOTATION MARK"
15
16 echo -e "\042" # 打印" (引号, 8 进制的 ASCII 码就是 42).
17 echo "=============="
18
19 # The $'\X' construct makes the -e option unnecessary.
20 # 如果使用$'\X'结构,那-e 选项就不必要了
21
22 echo; echo "NEWLINE AND BEEP"
23 echo $'\n' # 新行.
24 echo $'\a' # Alert (beep).
25
26 echo "==============="
27 echo "QUOTATION MARKS"
28 # 版本 2 以后 Bash 允许使用$'\nnn'结构
29 # 注意这种情况,'\nnn\是 8 进制
30 echo $'\t \042 \t' # Quote (") framed by tabs.
31
32 # 当然,也可以使用 16 进制的值,使用$'\xhhh' 结构
echo $'\t \x22 \t' # Quote (") framed by tabs.
34
35 # 早一点的 Bash 版本允许'\x022'这种形式
36 echo "==============="
37 echo
38
39
40 # 分配 ASCII 字符到变量中
41 # ---------------------
42 quote=$'\042' # \042 是",分配到变量中
43 echo "$quote This is a quoted string, $quote and this lies outside the quotes."
44
45 echo
46
47 # Concatenating ASCII chars in a variable.
48 # 变量中的连续的 ASCII char.
49 triple_underline=$'\137\137\137' # 137 是 8 进制的 ASCII 码'_'.
50
51 echo "$triple_underline UNDERLINE $triple_underline"
52
53 echo
54
55 ABC=$'\101\102\103\010' # 101, 102, 103 是 8 进制的码 A, B, C.
56 echo $ABC
57
58 echo; echo
59
60 escape=$'\033' # 033 是 8 进制码 for escape.
61 echo "\"escape\" echoes as $escape"
62 #"escape" echoes as 没有变量被输出
63
64
65 exit 0
\v\v\v\v
=============
VERTICAL TABS
==============
QUOTATION MARK
"
==============
NEWLINE AND BEEP
===============
QUOTATION MARKS
"
"
===============
" This is a quoted string, " and this lies outside the quotes.
___ UNDERLINE ___
ABC
"escape" echoes as
vim escaped01.sh
1 #!/bin/bash
2
3 echo "hello" # hello
4 echo "\"hello\",he said" # "hello" he said
5
6 # $号本身,跟在\$后的变量名,将不能扩展
7 echo "\$vort01" # 结果是 $vort01
8 # \号本身.
9 echo "\\" # 结果是\
10
11 echo \z # z
12 echo \\z #\z
13 echo '\z' # \z
14 echo '\\z' # \\z
15 echo "\z" #\z
16 echo "\\z" #\z
17
18 # # 命令替换 echo `echo \z` # z
19 echo `echo \\z` # z
20 echo `echo \\\z` # \z
21 echo `echo \\\\z` # \z
22 echo `echo \\\\\\z` # \z
23 echo `echo \\\\\\\z` # \\z
24 echo `echo "\z"` # \z
25 echo `echo "\\z"` # \z
26
27 # Here document
28 cat <<EOF
29 \z
30 EOF
31
32 cat <<EOF
33 \\z
34 EOF
效果:
hello
"hello",he said
$vort01
\
z
\z
\z
\\z
\z
\z
z
\z
\z
\z
\\z
\z
\z
./escaped01.sh: line 34: warning: here-document at line 28 delimited by end-of-file (wanted `EOF')
\z
EOF
cat <<EOF
\z
EOF
vim escaped02.sh
1 #!/bin/bash
2 variable=\
3 echo "$variable"
4 # Will not work - gives an error message:
5 # 将不能正常工作- 将给出一个错误消息:
6 # test.sh: : command not found
7 # 一个"裸体的" 转义符将不能够安全的分配给变量.
8 #
9 # What actually happens here is that the "\" escapes the newline and
10 # 这里其实真正发生的是 variable=\,这句被 shell 认为是没有完成,\被认为是一个续行符
11 #+ 这样,下边的这句 echo,也被认为是上一行的补充.所以,总的来说就是一个非法变量分配
12
13 variable=\
14 23skidoo
15 echo "$variable" # 23skidoo
16 # 这句就可以使用,因为这是一个合法的变量分配
17
18 variable=\
19 # \^ 转义一个空格
20
21 echo "$variable" # 显示空格
22
23 variable=\\
24 echo "$variable" # \
25
26 variable=\\\
27 echo "$variable"
28 # 不能正常工作,给出一个错误
29 # test.sh: \: command not found
30 #
31 # 第一个转义符把第 2 个\转义了,但是第 3 个又变成"裸体的"了,
32 #+ 与上边的例子的原因相同
33
34 variable=\\\\
35 echo "$variable" # \\
36 # 转了两个\
37 # 没问题
效果:
./escaped02.sh: line 3: : command not found
23skidoo
./escaped02.sh: line 19: ^: command not found
23skidoo
\
./escaped02.sh: line 27: \: command not found
\\
扩展性操作:
进程查看
vim process.sh
1 #!/bin/bash
2
3 name="$1"
4
5 echo
6
7
8 if [ -n $name ]
9 then
10 ps -ef |grep $name
11 fi
12
13 exit 0
如查看tomcat: