使用 shell 从列表中删除文件

Deleting files from a list using shell

我是 Applescript & Shell 的初学者,我正在编写一个脚本,在某个时候需要我删除 .txt 文件中列出的文件。我在 Whosebug 上进行了广泛搜索,并能够从我的 Applescript 中提出以下命令,我是 运行...

do shell script "while read name; do
    rm -r \"$name"\
done < ~Documents/Script\ Test/filelist.txt"

它似乎可以识别并读取文件,但我收到一条错误消息,我不明白为什么:

error "rm: ~/Documents/Script Test/filetodelete.rtf: No such file or directory" number 1

就是说,我可以导航到那个确切的目录并验证确实存在具有该名称和该扩展名的文件。有人可以帮助阐明为什么会发生此错误吗?

你打错了。该文件的路径很可能是 ~/Documents,而不是 ~Documents(在 Bash 中是帐户名为 Documents 的用户的主目录)。

如果您的 shell 不是 Bash,它甚至可能不支持 ~ $HOME

在数据文件中,您也不能使用~来引用您的主目录。你可以用一个简单的替换来增加循环来支持这个:

while read -r file; do
    case $file in '~'*) file=$HOME${file#\~};; esac
    rm -r "$file"
done < ~/"Documents/Script Test/filelist.txt"

另请注意 read -r 的使用,以避免 read 的遗留默认行为出现一些讨厌的问题。