Shell 脚本练习1:
请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如生成的文件名为2020-04-20.log,并且把磁盘的使用情况写到到这个文件中。日记文件放在/usr/local/log/下。脚本判断创建此目录,如果没有这个目录就创建目录,如果有就继续执行。
本题练习知识点有:
一、判断语句配合验证命令是否成功的使用。
二、 日期命令配合参数的使用。
三、磁盘命令的使用。
四、 重定向的符号使用。
[root@zl_cloud sbin]# vi test1.sh
# /bin/bash
# shell test1
# writen by ZLING 2020-04-08
DIR=/usr/local/log/
cd /root/
if [ -e $DIR ]
then
break
else
mkdir -p $DIR
fi
time=`date +"%Y-%m-%d"`
touch /usr/local/log/$time.log
df -h > /usr/local/log/$time.log
~
~
~
"test1.sh" 19L, 345C written
[root@zl_cloud sbin]#
答案解析:
一步步来,先做到每一步的代码都能实现。
- 先判断目录是否存在
方法 ①:判断语句配合判断目录是否存在的选项的使用:
[root@zl_cloud sbin]# cat test1.sh
# /bin/bash
# shell test1
# writen by ZLI