4. Continue和break后加参数表示外条的层数..................................................... 3
7. Echo的-e使转义字符生效................................................................................. 4
9. Exec执行命令后关闭当前shell.......................................................................... 4
1. shell里的函数返回值
[root@legend chapter02]# vim JAY
#!/bin/sh
foo()
{
echo JAY;
echo LEGEND;
}
result="$(foo)" #注意引用方式$(foo),因为foo不是变量,不能直接用$foo
echo $result
~
"JAY" 8L, 78C 已写入
[root@legend chapter02]# ./JAY
JAY LEGEND
[root@legend chapter02]# vim my_name
#!/bin/sh
yes_or_no() {
echo "Is your name $* ?"
while true
do
echo -n "Enter yes or no: "
read x
case "$x" in
y | yes ) return 0;;
n | no ) return 1;;
* ) echo "Answer yes or no"
esac
done
}
echo "Original parameters are $*"
if yes_or_no "$1" #此时向函数传递的参数只有wang,而且yes_or_no可以直接做条件,不用test命令。经测试if $1 “$2” 也可以正常将函数名作为命令参数传入。同时if后的test命令也可作为参数传入。但是注意,虽然yes_or_no返回1(失败)和0(成功)可以成为条件语句,但是直接用1和0却不能作为条件语句。但是可以用true和faulse
then
echo "Hi $1, nice name"
else
echo "Never mind"
fi
exit 0
~
[root@legend chapter02]# ./my_name wang liang
Original parameters are wang liang
Is your name wang ?
Enter yes or no: y
Hi wang, nice name
2. Test语句中的文件判定:
File Conditional Result
-d file True if the file is a directory.
-e file True if the file exists. Note that, historically, the -e option has not
been portable, so -f is usually used.
-f file True if the file is a regular file.
-g file True if set-group-id is set on file.
-r file True if the file is readable.
-s file True if the file has nonzero size.
-u file True if set-user-id is set on file.
-w file True if the file is writable.
-x file True if the file is executable.
3. Shell中的冒号:
可以作为一条空语句,可以作为条件语句,作用相当于true。
同时可以为变量条件化设置
$(var:=value) #如果var设置了一个值 就保留,如果没有或者为空 设置成value,注意$(var:=value)并不能当作一条命令来执行,应该如下作为:的参数。或者为echo的参数。
[root@legend chapter02]# : ${var5:?default}
而$(var:-value) 对var不会有任何改变,只是整个$(var:-value)的值会受var状态的影响,如果为空或不存在,则返回value,有则返回该值。
[root@legend chapter02]# : ${var6:?default} #这样会在var6不存在时输出var6: default
-bash: var6: default #存在时忽略此命令
[root@legend chapter02]# : ${var7:+default} #var7存在并不为空时返回bar,注意只是返回。不是输出。
4. Continue和break后加参数表示外条的层数
这个作用很少用,因为大大降低了程序的可读性。
5. .的作用
[root@legend chapter02]# vim classic #设置环境变量脚本
#!/bin/sh
version=classic
PATH=/usr/local/old_bin:/usr/bin:/bin:.
PS1="classic> "
~ ~
[root@legend chapter02]# vim latest
#!/bin/sh
version=latest
PATH=/usr/local/new_bin:/usr/bin:/bin:.
PS1=" latest version> "
~
[root@legend chapter02]# . ./classic #执行该脚本后的环境并没有消失,因为它没有新建一个shell,而是直接用的原来root的shell,所以能够看见脚本里的变量设置在脚本退出后仍然有效。
classic> echo $version
classic
classic> . latest
latest version> echo $version
latest
latest version> exit #输入exit后退出该环境,到了legend环境。
logout
[legend@legend ~]$
6. 各种环境变量的意义:
[root@legend chapter02]# echo $PS1 #PS1表示输入命令前的提示字符
[/u@/h /W]/$ #/u 用户 /h 主机 /w 目录 /$ 权限
[root@legend chapter02]# echo $PS2 #PS2表示换行符
>
[root@legend chapter02]#
7. Echo的-e使转义字符生效
[root@legend chapter02]# echo "string to output/c"
string to output/c
[root@legend chapter02]# echo -e "string to output/c"
string to output[root@legend chapter02]#
当然也可以用-n参数直接实现
[root@legend chapter02]# echo -n "string to output"
string to output[root@legend chapter02]#
8. eval赋值
[root@legend chapter02]# foo=100
[root@legend chapter02]# x=$foo #此时将foo认作变量,$foo认作变量值
[root@legend chapter02]# echo $x
100
[root@legend chapter02]# foo=10
[root@legend chapter02]# x=foo
[root@legend chapter02]# y="$"$x #将字符串$foo给了y,此时的$foo仅为字符串
[root@legend chapter02]# echo $y
$foo
[root@legend chapter02]# eval y="$"$x #将$foo的值10给了y,通过eval将$foo变为变量值
[root@legend chapter02]# echo $y
10
[root@legend chapter02]#
9. Exec执行命令后关闭当前shell
[root@legend chapter02]# exec wall "dfjkdfj" #执行了wall “lkfj”之后关闭了root的shell。脚本中在此命令之后的命令都不会再执行了。
Broadcast message from root (pts/0) (Fri Jul 24 14:03:47 2009):
dfjkdfj
[legend@legend ~]$
10. Exit
Exit 0 代表正常退出,剩下的1-125都表示各种出错代码。
例:[ -f file ] && exit 0 || exit1
11. Export
Export foo或者export foo=”kdfjkd”
将foo导出到所有当前shell的子shell及其孙shell之后的所有shell。
Set –a或者set –allexport可将所有变量导出。
12. Expr
[root@legend chapter02]# x=1
[root@legend chapter02]# expr $x + 1 #计算值,注意运算符前后有空格。
2
[root@legend chapter02]# x=10
[root@legend chapter02]# x=`expr $x + 1` #注意此键为反引号反引号的用法即替代命令,同时可以用x=$(($x+1))达到同样的目的
[root@legend chapter02]# echo $x
11
[root@legend chapter02]# x=10
[root@legend chapter02]# x='expr $x + 1' #单引号则识别为字符串
[root@legend chapter02]# echo $x
expr $x + 1
[root@legend chapter02]# x=10
[root@legend chapter02]# x=$(expr $x + 1) #$()和反引号的作用一样,将里面的运行输出作为参数表。
[root@legend chapter02]# echo $x
11
[root@legend chapter02]#value=12
[root@legend chapter02]#expr $value + 10 > /dev/null 2>&1 #控制错误信息和正常输出全部消失
[root@legend chapter02]#echo $? #存储当前最新的命令的返回代码
0 #说明返回正常,计算结果是一个数。
[root@legend chapter02]#value=hello
[root@legend chapter02]#expr $value + 10 > /dev/null 2>&1
[root@legend chapter02]#echo $?
2 #返回不正常,非数字参数
其中表达式有以下使用方式:注意用的时候如果有< > 前面必须加/转义符,不然将作为重定向符引起逻辑错误
expr1 | expr2 expr1 if expr1 is nonzero, otherwise expr2 前零则后,否则为前。
expr1 & expr2 Zero if either expression is zero, otherwise expr1 有零则零,无零则前。
expr1 = expr2 Equal
expr1 /> expr2 Greater than #执行后输出0(大于),1(不大于)
expr1 />= expr2 Greater than or equal to
expr1 /< expr2 Less than
expr1 /<= expr2 Less than or equal to
expr1 != expr2 Not equal
expr1 + expr2 Addition
expr1 - expr2 Subtraction
expr1 * expr2 Multiplication
expr1 / expr2 Integer division
expr1 % expr2 Integer modulo
expr的这些表达式均可作为条件语句,而且不能加反引号。
[root@legend chapter02]# vim legend2
x=10
if `expr $x > 1` #注意这个写法事实上是把expr $x的输出重定向到1这个文件里去了
then
echo "in if"
else
echo "in else"
fi
exit 0
~
"legend2" 8L, 97C 已写入
[root@legend chapter02]# ./legend2
in if
[root@legend chapter02]#
正确写法:
[root@legend chapter02]# vim legend2
x=10
if expr $x /> 1 #必须加转义字符
then
echo "in if"
else
echo "in else"
fi
exit 0
~
~
"legend2" 8L, 96C 已写入
[root@legend chapter02]# ./legend2
1
in if
13. Set的作用
设置当前参数列表
[root@legend chapter02]# set $PATH
[root@legend chapter02]# echo $@
/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@legend chapter02]# set date
[root@legend chapter02]# echo $@
date
[root@legend chapter02]# set $(date)
[root@legend chapter02]# echo $@
2009年 07月 24日 星期五 16:23:27 EDT
14. Shift
从大到小左移参数列表,不加参数为移一位,加n则移n位。$0将保留,但$1等将冲掉。
[root@legend chapter02]# set $(date)
[root@legend chapter02]# echo $@
2009年 07月 24日 星期五 16:27:06 EDT
[root@legend chapter02]# shift 2
[root@legend chapter02]# echo $@
24日 星期五 16:27:06 EDT
[root@legend chapter02]#
15. Trap
[root@legend chapter02]# vim _trap
#!/bin/sh
trap 'rm -f /tmp/my_tmp_file_$$' INT #执行ctrl+c发出INT信号时执行删除语句
echo creating file /tmp/my_tmp_file_$$
date > /tmp/my_tmp_file_$$
echo "Press interrupt (Ctrl-C) to interrupt...."
while [ -f /tmp/my_tmp_file_$$ ]; do
echo File exists
sleep 1
done
echo The file no longer exists
trap – INT #发出信号时执行默认的中断语句,后面的东西都不执行
echo creating file /tmp/my_tmp_file_$$
date > /tmp/my_tmp_file_$$
echo "Press interrupt (Ctrl-C) to interrupt...."
while [ -f /tmp/my_tmp_file_$$ ]; do
echo File exists
sleep 1
done
echo We never get here
exit 0
~
~
[root@legend chapter02]# ./_trap
creating file /tmp/my_tmp_file_4468
Press interrupt (Ctrl-C) to interrupt....
File exists
File exists
File exists
File exists
^CThe file no longer exists
creating file /tmp/my_tmp_file_4468
Press interrupt (Ctrl-C) to interrupt....
File exists
File exists
File exists
File exists
File exists
^C
[root@legend chapter02]#
16. $((…))高效实现运算
X=$(($x+1))
17. 参数扩展:
[root@legend chapter02]# vim legend3
#!/bin/sh
touch super2
touch super3
for i in 2 3
do
rm super${i} #不用大括号此时也不会有什么影响,但是如果文件名为2super,若用$isuper则有问题,系统认为isuper是一个变量而报错
done
exit 0
[root@legend chapter02]# vim param
#!/bin/sh
unset foo
echo ${foo:-bar}
foo=fud
echo ${foo:-bar}
foo=/usr/bin/X11/startx
echo ${foo#*/} #至少删去一个*/
echo ${foo##*/} #删到最后一个*/
bar=/usr/local/etc/local/networks
echo ${bar%local*} #删掉最后一个local*
echo ${bar%%local*} #删到最前一个local*
exit 0
~
[root@legend chapter02]# ./param
bar
fud
usr/bin/X11/startx
startx
/usr/local/etc/
/usr/
18. 即时文档