变量赋值
使用declare -i 定义整型类
[[email protected] scripts]# aa=5+6 [[email protected] scripts]# echo $aa 5+6 [[email protected] scripts]# declare -i aa [[email protected] scripts]# aa=5+6 [[email protected] scripts]# echo $aa 11
使用let,就无需定义为整型
[[email protected] scripts]# let bb=1+8 [[email protected] scripts]# echo $bb 9 [[email protected] scripts]# cc=$((5+8)) [[email protected] scripts]# echo $cc 13
echo $? 返回值
test 比较两个值得大小
-eq -lt -le -gt -ge
aa=3 bb=4
test $aa -eq $bb
echo $?
test $aa -lt $bb
echo $?
判断1 || 判断2
[ -f /etc/passwd ] 判断文件是否存在
echo $?
0
[ -r /etc/passwd ]
echo $?
0
[[email protected] scripts]# aa=3 bb=4 [[email protected] scripts]# [ $aa -lt $bb ] && echo ok ok [[email protected] scripts]# [ $aa -gt $bb ] && echo ok [[email protected] scripts]# [ $aa -gt $bb ] || echo ok ok
判断代码
[[email protected] scripts]# vim if.sh #!/bin/bash grep -q ^$1 /etc/passwd if [ "$?" -eq 0 ]; then echo "$1 is exist" else echo "$1 is not exist" fi
if...then
[[email protected] scripts]# vim age.sh #!/bin/bash read -p "Please input your age: " age if [ "$age" -le 0 ] || [ "$age" -ge 100 ]; then echo "Please input correct age" read age elif [ "$age" -gt 0 ] && [ "$age" -lt 20 ]; then echo "You are junior" elif [ "$age" -ge 20 ] && [ "$age" -lt 50 ]; then echo "You are adult" else echo "You are old" fi
while循环
[[email protected] scripts]# vim sum.sh #!/bin/bash sum=0 while [ $sum -lt 10 ] do let sum+=1 echo $sum done
#!/bin/bash read -p "Please input your name: " name while [ "$name" != tom ] do echo "Please input correct name" read name done ~
[[email protected] scripts]# vim case.sh #!/bin/bash xx=0 until [ "$xx" -gt 24 ] do case "$xx" in [0-9]|1[01]) echo "good morning" ;; 12) echo "it‘s lunch time" ;; 1[3-7]) echo "good afternoon" ;; *) echo "good evening" esac let xx+=1 done
时间: 2024-10-22 04:28:13