在bash中使用主字符(~)检查目录是否存在失败

Jus*_*tin 16 bash

为什么以下bash检查目录是否失败?

if [ ! -d "~/Desktop" ]; then
   echo "DOES NOT EXIST"
   exit 1;
fi
Run Code Online (Sandbox Code Playgroud)

~/Desktop确实存在。顺便说一下,这是在 Mac 上。


问题在于这种类型的脚本

read -p "Provide the destination directory: " DESTINATION

if [ ! -d $DESTINATION ]; then
    echo "\t'$DESTINATION' does not exist." >&2;
    exit 1;
fi
Run Code Online (Sandbox Code Playgroud)

qua*_*nta 24

删除目录周围的双引号以查看它是否有效:

if [ ! -d ~/Desktop ]; then
   echo "DOES NOT EXIST"
   exit 1;
fi
Run Code Online (Sandbox Code Playgroud)

其原因是波浪号扩展仅在未引用时才有效。

info "(bash) Tilde Expansion"

3.5.2 Tilde Expansion
---------------------

If a word begins with an unquoted tilde character (`~'), all of the
characters up to 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 `HOME' shell variable.  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.
Run Code Online (Sandbox Code Playgroud)

  • 波浪号扩展是在设置变量时完成的,而不是在评估时完成,所以这不是一个公平的例子。 (3认同)

小智 7

贾斯汀在他对量子回答的第一条评论中澄清了他的问题。他正在使用read(或通过其他一些动态方式)阅读一行文本,并希望扩展波浪号。

问题变成了“如何对变量的内容执行波浪号扩展?”

一般的方法是使用eval,但它带有一些重要的警告,即>变量中的空格和输出重定向 ( )。以下似乎对我有用:

read -p "Provide the destination directory: " DESTINATION

if [ ! -d "`eval echo ${DESTINATION//>}`" ]; then
    echo "'$DESTINATION' does not exist." >&2;
    exit 1;
fi
Run Code Online (Sandbox Code Playgroud)

尝试使用以下每个输入:

~
~/existing_dir
~/existing dir with spaces
~/nonexistant_dir
~/nonexistant dir with spaces
~/string containing > redirection
~/string containing > redirection > again and >> again
Run Code Online (Sandbox Code Playgroud)

解释

  • ${mypath//>}剔除>这可能在揍一个文件中的字符eval
  • eval echo ...是什么是实际的波浪线扩展
  • 周围的双引号eval是为了支持带空格的文件名。

作为对此的补充,您可以通过添加以下-e选项来改进 UX :

read -p "Provide the destination directory: " -e DESTINATION
Run Code Online (Sandbox Code Playgroud)

现在,当用户输入波浪号并点击选项卡时,它会展开。然而,这种方法并不能取代上面的 eval 方法,因为只有在用户点击选项卡时才会发生扩展。如果他只是输入 ~/foo 并按回车键,它将保留为波浪号。

也可以看看: