使用SHELL脚本,删除指定文件或目录,
使用方法:bash /sh/delfiles.sh 11.txt 22.txt
C#代码
- #!/bin/bash
- # 检查是否有参数传入
- if [ "$#" -eq 0 ]; then
- echo "[ERROR] No files to delete! At least one parameter is required! "
- exit 1
- fi
- # 打印脚本名称
- echo "Script Name: $0"
- # 打印脚本所在的目录
- echo "Script Directory: $(dirname $0)"
- # 打印当前工作目录
- echo "Working Directory: $PWD"
- # 进入当前工作目录
- cd $PWD
- echo "=================================================="
- # 循环遍历所有参数并删除文件
- for file in "$@"; do
- if [ -f "$file" ]; then
- rm "$file"
- echo "Deleted file ==> ./$file"
- elif [ -d "$file" ]; then
- rm -rf "$file"
- echo "Deleted directory ==> ./$file"
- else
- echo "File or directory does not exist: $file"
- fi
- done
- echo "=================================================="