Linux Shell 脚本:创建或删除文件/目录 - logical
#!/bin/bash
if [ ! -e '/root/test/logical' ]; then touch '/root/test/logical' else if [ -f '/root/test/logical' ]; then rm '/root/test/logical' mkdir '/root/test/logical' else rm -r '/root/test/logical' fi fi
解释:
- 首先使用条件语句判断文件或目录是否存在,使用 -e 参数判断,若不存在则进入 if 语句。
- 若不存在则使用 touch 命令创建一个文件。
- 如果存在,则再次使用条件语句判断其类型,使用 -f 参数判断是否为文件,若为文件则删除之后使用 mkdir 命令创建一个目录,否则使用 -r 参数删除该目录。
原文地址: https://www.cveoy.top/t/topic/nvv0 著作权归作者所有。请勿转载和采集!