16.字符串运算符
= :判断两个字符串是否相等
string1="+"
string2="-"
if [ $string1 = $string2 ]
then
echo "YES"
else
echo "NO"
fi
!= :判断两个字符串不相等
-z :判断字符串长度是否为0,为0为真
string=""
if [ -z $string ]
then
echo "0"
else
echo "NO"
fi
输出 0
string="aabbbb"
if [ -z $string ]
then
echo "0"
else
echo "NO"
fi
输出为 NO
-n :判断字符串长度是否不为0,不为0为真
str :判断字符串长度是否为0,字符串是否存在
if [ $string ]
17.文件运算符
-d:目录文件
-f:普通文件
-r:可读权限
-w:可写权限
-x:可执行权限
-e:文件是否存在
if [ -e 0908 ]
then
echo "YES"
else
mkdir 0908
fi
18.case命令
case 操作数 in
模式1)
语句1
;;
模式2)
语句2
;;
*) ————————————相当于if语句中的else
语句3
esac
printf "input 2 nums: "
read num1
read num2
printf "input yun suan"
read op
case $op in
"+")
let value=num1+nu2
printf "$value\n"
;;
"-")
let value=num1-num2
printf "$value\n"
;;
"*")
let value=num1*num2
printf "$value\n"
;;
"/")
let value=num1/num2
printf "$value\n"
;;
esac
注意 当输入 *)时表示输入错误代码
19.while循环
sum=0
i=1
while [ $i -le 100 ] # ((i<=100))
do
let sum+=i
let i++
done
printf "sum=%d\n" "$sum"
sum=0
i=1
while ((i<=100))
do
if ((i%2==0))
then
let sum+=i
fi
let i++
done
printf "sum=%d\n" "$sum"
打印以下图形
*
***
*****
*******
i=1
while [ $i -le 4 ]
do
blank=1
while [ $blank -le `expr 4 - $i` ]
do
printf " "
let blank++
done
star=1
let num=2*i-1
while [ $star -le $num ]
do
printf "*"
let star++
done
printf "\n"
let i++
done