1.文件操作
-d :对象存在且为目录返回值为“是”
-f :对象存在且为文件返回值为“是”
-L :对象存在且为符号连接返回值为“是”
-r :对象存在且为可读返回值为“是”
-s :对象存在且为长度非零返回值为“是”
-w :对象存在且为可写返回值为“是”
-x :对象存在且为可执行返回值为“是”
example:
#!/bin/bash
#clear
cd /usr/bin
if [ -f gnome-calculator ]
then
echo -e "you are the right!\n"
gnome-calculator
else
echo "not have!"
fi
#cd
2.循环控制语句
for循环、while循环、until循环
example:
#!/bin/bash
#循环控制语句
#for
for i in a,s,d,f,g,e 1,2,2,4,5,6,7,8 #参数之间的空格可表示换行,在循环中变量用来遍历列表中的所有对象
do
echo $i
done
echo "================="
total=0
for((j=0;j<=100;j++)); #for语句中的双括号不能省略,之后的分号可有可无
do
total=` expr $total + $j ` #表达式中的加号两边的空格不能省,否则会成为字符串的连接
done
echo "the sum is $total "
echo "================="
#while
total1=0
num=0
while((num<=100)); #只要while表达式为真,do和done之间的操作会一直进行
do
total1=` expr $total + $num `
((num+=1))
done
echo "the sum is $total1"
echo "================="
total2=0
num1=0
until [ $num1 -gt 100 ] #重复do和done之间的操作直到表达式成立为止
do
total2=` expr $total2 + $num1 `
num1=`expr $num1 + 1 `
done
echo "the sum is $total2"
echo "=================="
3.条件控制语句
一.if语句
#!/bin/bash
#clear
cd /usr/bin
if [ -f gnome-calculator ]
then
echo -e "you are the right!\n"
gnome-calculator
else
echo "not have!"
fi //用fi来结束
#cd
二.case语句
#!/bin/bash
case $USER in
huxuelin)
echo "you are husao"
;; //case的每一个操作的最后都有两个“;;”
guest)
echo "Welcome the guest!"
;;
root)
echo "you are the root!"
;;
*)
echo "Who you are the $USER ? "
;;
esac //用esac结尾
阅读(661) | 评论(0) | 转发(0) |