shell 脚本
任务描述:请采用 shell 脚本,实现快速批量的操作。
在 linux4 上编写/root/createfile.sh 的 shell 脚本,创建 20 个文件
/root/shell/file00 至/root/shell/file19,如果文件存在,则删除再创建;
每个文件的内容同文件名,如 file00 文件的内容为“file00”。用
/root/createfile.sh 命令测试。
第一步创建目录和脚本文件
代码如下:
#!/bin/bash
# 循环创建20个文件
for i in {0..19}
do
# 构建文件路径
file="/root/shell/file$(printf "%02d" $i)"
# 如果文件存在,则删除
if [ -f $file ]; then
rm $file
fi
# 创建文件并写入内容
echo "file$(printf "%02d" $i)" > $file
done
添加执行权限
chmod +x /root/createfile.sh
执行脚本,2行代码,任用一个即可
./createfile.sh
/root/createfile.sh
可以自己测试一下效果,是否满足要求