开始学习吧:
Shell 语法 :
1.变量
Linux是区分大小写的系统;使用变量名前面加上$来访问变量的内容,为变量赋值时,只是需要变量名,变量会根据需要自动创建
$salutation=hello$echo $salutationhello$salutation="YesDear"$echo $salutationYesDear1.1 read 命令等待用户输入并且赋值
$read salutationWiegeht's$echo $salutationWiegeht's1.2 引号的使用双引号不会影响变量的替换,单引号和反斜杠不进行变量转换
$varTest="Test"$echo $varTestTest$echo"$varTest"Testg$echo '$varTest'$varTestecho /$varTest$varTest1.3环境变量$0:shell脚本的名字
$$脚本的进程号,脚本通常使用它生成一个临时文件eg:/tmp/tmpfile_$$
$#传给给脚本的参数个数
2.条件
test或[命令
eg1:检查一个文件是否存在
if test -f fred.cthen....fiorif[ test -f fred.c]; then....fips:[ A ]和检查内容A之间需要空格,如果想if和then同一行,那么需要分号2.1字符串比较str1 = str2 相等为真str2 != str2 不相等为真-n str1 不为空为真-z str1 为空为真2.2数字比较A -eq B 相等为真A -ne B 不相等为真A -gt B A>BA -ge B A>=BA -lt B A<BA -le B A<=B2.3文件条件测试-d file 为目录为真-e file 存在为真,尽量使用-f-f file 存在为真-g file 如果文件的set-group-id位被设置则为真-r file 可读为真-s file 大小不为0为真-u file 如果文件的set-user-id位被设置则为真-w file可写为真-x file可执行为真2.4控制结构(1).if语句 if condition then statements else statements fi(2).elif语句
#!/bin/shecho"Ii't morning? Please anser yes or no"read timeofdayif[ "$timeofday"="yes" ] then echo"Goodmorning"elif [ "$timeofday"="no" ];then echo"Goodafternoon"else echo "Sorry,$timeofday not recognized,Enter yes or no" exit1fiexit0本次语法笔记结束,下次继续shell语法(2)
新闻热点
疑难解答