shell习题-判断文件存在


1 编写一个名为iffile程序,它执行时判断/bin目录下date文件是否存在?

2 编写一个名为greet的问候程序,它执行时能根据系统当前的时间向用户输出问候信息。设从半夜到中午为早晨,中午到下午六点为下午,下午六点到半夜为晚上。

 

参考答案:

1

#!/bin/bash
if [ -f /bin/date  ] 
then
    echo "/bin/date file exist."
else
    echo "/bin/date not exist."
fi

2 

#!/bin/bash
h=`date +%H`
if [ $h -ge 0 ] && [ $h -lt 12 ]
then
    echo "Good morning."
elif [ $h -ge 12 ] && [ $h -lt 18 ]
then
    echo "Good afternoon."
else
    echo "Good evening."
fi