指令合集
给用户(user)对于文件sh1添加执行(x)权限:
chmod u+x sh1
判断真假,输出为0为真,输出为1为假:
echo $?
求值
使用expr(expression)用来计算表达式:
expr 6+12+18
x=1
y=2
expr $x \* 3
a=`expr $x +1`
echo `expr $x + $y`
Shell程序设计
Shell程序的位置参数
#test-sh2
cp test-sh1 test-temp
more test-temp
rm test-temp
#test-sh3
cp $1 $2
more $2
rm $2
6
测试命令
test:真(0),假(1),这个命令后面加参数用来做不同的测试,执行完这个语句后使用
echo $?
来查看结果,也可以把test后面的内容用中括号[]括起来,起同样的效果。例如
test -e file
[-e file]
是一样的效果。
文件测试
文件file是否存在(exist):
test -e file
串测试
字符串string是否为空
数值比较
x
控制结构
用;、&&、||连接两个命令,其中,;连接两个命令的时候,只是顺序地执行各个命令,&&的话,
if语句
例6:根据时间问候
hour=`date|cut -c12-13`
if["$hour" -ge 0 -a "$hour" -le 11]
then
echo "good morning"
elif["$hour" -ge 12 -a "$hour" -le 17]
then
echo "good afternoon"
else
echo "good evening"
fi
例7:
里面的"^$user"是指只找user里面储存字符打头的内容。
问题
子shell是干什么的