我安装了ZSH的Ubuntu 18.04.我有这个bash脚本来检查目录是否存在.
即使我有一个名为“〜/ .config”的目录,但条件总是为负.我似乎无法弄清楚我的代码有什么问题.
setup.sh
#!/bin/bash
if [ -d "~/.config" ] ; then
echo "Directory exist"
else
echo "Does not exist"
fi
使用chmod x ./setup.sh使文件可执行
输出始终不存在
解决方法:
由于您已编写“〜/ .config”,因此将其视为文字字符串.要允许~shell扩展,您需要不加引号:
#!/bin/bash
if [ -d ~/.config ] ; then
echo "Directory exist"
else
echo "Does not exist"
fi
Bash Pitfals, 26. echo “~”很好地解释了这一点:
26. echo “~”
Tilde expansion only applies when ‘~’ is unquoted. In this example echo writes ‘~’ to stdout, rather than the path of the user’s home directory.
Quoting path parameters that are expressed relative to a user’s home directory should be done using $HOME rather than ‘~’. For instance consider the situation where $HOME is “/home/my photos”.
06001
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。