2022国赛29:linux脚本题解析
大赛试题内容:
( 十四) ) 脚本
【任务描述】为了减少重复性任务的工作量,节省人力和时间,请采
用脚本,实现快速批量的操作。
1.在 linux6 上编写/root/CreateFile.sh 的 shell 脚本,创建 20 个文
件/root/test/File101 至/root/test/File120,如果文件存在,则先删除再创
建;每个文件的内容同文件名,如 File101 文件的内容为“File101”。
解答过程:
[root@cs6 ~]# vim CreateFile.sh
#!/bin/bash
mkdir /root/test
cd /root/test
for num in {101..120}
do
if [ -e File${num} ];then
rm -rf File${num}
else
touch File${num}
echo "File${num}">>File${num}
fi
done
保存退出。
[root@cs6 ~]#chmod 777 CreatFile.sh //设置读写执行权限
[root@cs6 ~]#./CreatFile.sh //运行脚本文件