Shell Scripts 脚本检查

shell • script脚本检查
 sh [-nvx]  scripts.sh 
选项与参数: 
 -n  :不执行script,仅查询语法的问题; !!
-v  :在执行script前,先将scripts的内容输出到屏幕上; 
 -x  :将使用到的script内容显示到屏幕上,这是很有用的参数; !!!
 

shell 里面条件控制语句

case结构条件句
case • $变量名称 in • “ 值1")
程序段1
;; •
“ 值2")
程序段2
;; •
*) •
exit • 1 •
;; •
esac

案例:判断用户输入的是哪个数,1-7显示输入的数字,1显示 Mon,2 :Tue,3:Wed,4:Thu,5:Fir,6-7:weekend,其它值的
时候,提示:please input [1,7]
 

#!/bin/bash
print(){
echo “today is :$1"
}
case $1 in "one")
print "Mon"
;;
"two")
print "Tue"
;;
"three")
print "Wed"
;;
*)
echo "you must input one/two/three"
exit 1
esac
exit 0
 

条件判断语句
• 多分支结构
if [ • 条件1 • ];then
条件1成立,执行指令集1
elif [ • 条件2 • ];then
条件2成立,执行指令集2
else
条件都不成立,执行指令集3
fi

#!/bin/bash
#compare the size of the two numbers
if [ $# -ne 2 ];then
echo "usage is:bash $0 num1 num2"
exit 1;
fi
a=$1
b=$2
if [ $a -gt $b ]
then
echo "yes $a>$b"
exit 0
elif [ $a -eq $b ];then
echo "yes $a=$b"
exit 0
else
echo "yes $a<$b"
exit 0
fi
 

循环控制语句

循环可以不断地执行某个程序段落,直到用户设置的 条件达成为止,这称之为不定循环,除这之外,还有另外 一种已经固定要运行多少次的循环,这称之为固定循环。
– 不定循环:while • do • done,until do • done •
– 固定循环:for • … do • done •
while循环语句

while [ condition ]; do

命令

done

while [ condition ]

do

命令

done

 案例: 每隔两秒时间打印系统的负载情况

#!/bin/bash

while true

do

  uptime  # 系统的负载情况

  sleep 2  # 休眠2s

done

until循环语句  跟while的循环刚好是相反的。 当条件是false的是执行
until • [ • condition • ];do
命令
done
或者
until • [ • condition • ]
do
命令
done
#!/bin/bash
i=0
until [ $i -gt 5 ] #大于5
do
let square=i*i
echo "$i * $i = $square"
let i++
done
 

猜你喜欢

转载自blog.csdn.net/tryll/article/details/86604409
今日推荐