Shell编程-数学测试:
-eq:等于(Equal)
-ne:不等于(Not Equal)
-gt:大于(Greater Than)
-lt:小于(Lesser Than)
-ge:大于或等于(Greater or Equal)
-le:小于或等于(Lesser or Equal)
Shell编程-逻辑测试:
&&:逻辑与
||:逻辑或
!: 逻辑否
Shell编程-用户交互:
read -p 'please input your name: ' name
echo $name
Shell编程实例:
1.光盘挂载
if [ ! -e /media/BaseOS ]
then
mount /dev/cdrom /media &> /dev/null
echo 'cdrom is ok'
else
echo 'cdrom is ok'
fi
2.创建用户
read -p 'please input your name:' name
if [ ! -z $name ]
then
read -p 'please input your pass:' pass
if [ ! -z $pass ]
then
useradd $name
echo $pass | passwd --stdin $name &> /dev/null
echo "your name is ${name},your pass is ${pass},create is ok"
else
echo 'pass is empty'
fi
else
echo 'name is empty'
fi
3.内存判断
mem=`free -m |grep 'Mem'|awk '{print $4}'`
if [ $mem -lt 400 ]
then
echo "mem is no,now is ${mem}"
else
echo "mem is yes,now is ${mem}"
fi
4.循环输出
for name in `cat /etc/passwd | awk -F: '{print $1}'`
do
echo $name
sleep 1
done