#!/bin/sh
if [ -d $1 ]
then
echo 是目录
fi
if [ -f $1 ]
then
echo 是文件
fi
if [ -r $1 ]
then
echo 是可读的
fi
if [ -w $1 ]
then
echo 是可写的
fi
if [ -x $1 ]
then
echo 是可执行
fi
逻辑表达式:
三种:
1.非 !
2.与 -a
3.或 -o
if [ -f $1 -a $2 -gt 5]
then
echo \$2 is $2
echo "\$2 is bigger than 5 "
fi
if [ ! 2 -lt 5 ]
then
echo 2小于等于5;
fi
可以用逻辑操作符将两个测试表达式结合起来。仅需要用到一对方括号,而不能用两个, 否则将返回错误信息“too many arguments”。
[ "990" -le "995" ] -a [ "123" -gt "33" ]
-bash: [: too many arguments
第五部分 流程控制
1.if
第一种语法格式:
if 条件判断
then
command
fi
eg.
#!/bin/sh
if test 3 -eq 3
then
echo Yes
fi
第二种语法格式:
if 条件判断
then
command
esle
command
fi
eg.
#!/bin/sh
if [ -d /home/user01/demo ]
then
ls /home/user01/demo
else
mkdir /home/user01/demo
echo 目录创建成功!!
fi
第三种语法格式:
if 条件判断
then
command
elif 条件判断
then
command
elif 条件判断
then
command
...
else
command
fi
e.g.1
#!/bin/sh
echo please type age
read age
if test $age -ge 0 -a $age -lt 3
then
echo baby
elif [ $age -ge 3 -a $age -lt 7 ]
then
echo child
elif [ $age -ge 7 ] && [ $age -lt 18 ]
then
echo teenager
elif test $age -ge 18 && test $age -lt 40
then
echo youth
elif test $age -ge 40 && [ $age -lt 60 ]
then
echo man
else
echo older
fi
e.g.2
#!/bi/sh
# read socre and choose level
# read 相当于Java中的scanner 是一种交互式命令,读取设备的输入
echo "请输入学生成绩score:"
read score
if test $score -ge 90 -a $score -le 100
then
echo level A;
elif [ $score -ge 80 -a $score -lt 90 ]
then
echo level B;
elif [ $score -ge 70 ] && [ $score -lt 80 ]
then
echo level C;
elif test $score -ge 60 && test $score -le 70
then
echo level D;
else
echo 不及格;
fi
2. 循环语句
1)for 循环
第一种语法格式:
for((初始化变量值;结束循环条件;循环控制语句))
do
循环体
done
eg.
#!/bin/sh
sum=0
for ((i=0;i<10;i++))
do
echo $i
sum=$[ $sum + i ]
done
echo $sum
第二种语法格式:
for 变量 in 值1 值2 ...值N
do
循环体
done
eg1.
#!/bin/sh
for MONTH in Jan Feb Mar Apr May Jun July Aug Sep Oct Nov Dec
do
echo $MONTH
done
eg2.
#!/bin/sh
for file in `/bin/ls ~`;
do
echo $file;
done
eg3
for File in `ls /home/hadoop/shell`
do
chmod u+x $File
done
2)while 循环
第一种语法格式:
while [ condition #循环条件 ]
do
#statements
#【循环体】
#【循环控制】
done
eg1.
#!/bin/sh
i=1
while [ $i -le 10 ]
do
sum=$((sum+i))
i=$[ i + 1 ]
done
echo $sum
eg2.
#!/bin/sh
i=1
sum=0
while test $i -le 10
do
sum=$[ $sum + $i ]
i=`expr $i + 1`
done
echo $sum
第二种语法格式:
while read -r line
do
#【循环体】
done
eg.
#!/bin/sh
#Read /ect/sysconfig/network-scripts/ifcfg-eh0 and print out
FILE=/ect/sysconfig/network-scripts/ifcfg-eh0
while read -r line
do
echo $line
done < $FILE
3)case 类似于java中的 swich case
第一种语法格式:
#!/bin/sh
echo "input from: one two three"
read input
case $input in
one) echo "your input is one"
;;
two) echo "your input is two"
;;
three) echo "your input is three"
;;
*) echo your input is $input
esac
第二种语法格式:
#!/bin/sh
echo "input from :one two three ....."
read input
case $input in
one | two) echo "your input is one or two"
;;
three | four) echo "your input is three or four "
;;
five) echo "your input is five"
;;
*) echo your input is $input
esac
进程名称是crond
ps -ef | grep crond $查看此进程是否开启
默认进程是开启的,如果没有开启,可以使用命令手动开启
# service crond status
# service crond start
# service crond stop
# service crond restart
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
* * * * *
分钟 小时 天(of月) 月(of年) 星期(of周)