gpt4 book ai didi

bash - 不能在 rmdir 中使用带有波浪号的文件夹路径

转载 作者:行者123 更新时间:2023-11-29 09:51:22 26 4
gpt4 key购买 nike

我需要能够删除与任何用户系统的文档文件夹相关的目录。

rmdir: ~/Documents/Folder: No such file or directory

如果我手动输入扩展路径 (/Users/ricky/Documents/Folder),它工作正常。

我以为 bash 会自动扩展路径开头的波浪号?

更新:

在按照建议尝试了一系列不同的方法之后,我现在非常有信心问题在于我如何存储路径。我从逐行读取的文本文件中获取路径:

...
export_folder_path="$(echo $line | cut -f2 -d=)"
...

echo $export_folder_path
rmdir $export_folder_path
rmdir "$HOME/Documents/Folder\ 1"

输出如下:

$HOME/Documents/Folder\ 1
rmdir: $HOME/Documents/Folder\ 1: No such file or directory
rmdir: /Users/ricky/Documents/Folder\ 1: Directory not empty (This is actually what I want)

我无法弄清楚手动输入导出路径和使用变量之间的区别。为什么变量拒绝扩展 $HOME?我尝试了很多添加引号的变体,但都没有成功。

最佳答案

波浪号扩展并非在所有情况下都有效。您可以改为使用 HOME 变量:

rmdir $HOME/Documents/Folder

来自 bash manual :

Tilde Expansion If a word begins with an unquoted tilde character ('~'), all of the characters preceding the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the shell parameter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.

If the tilde-prefix is a '~+', the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is a '~-', the value of the shell variable OLDPWD, if it is set, is substituted. If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a '+' or a '-', the tilde-prefix is replaced with the corresponding element from the directory st, as it would be displayed by the dirs builtin invoked with the tilde- prefix as an argument. If the characters following the tilde in the tilde-prefix consist of a number without a leading '+' or '-', '+' is assumed.

If the login name is invalid, or the tilde expansion fails, the word is unchanged.

关于bash - 不能在 rmdir 中使用带有波浪号的文件夹路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com