繁体   English   中英

如果目录存在,则使用Unix Bash Shell编程

[英]Unix Bash Shell Programming if directory exists

所以我试图在bash shell脚本中进入if语句,但我认为我做错了什么,反正这里是我的示例代码。

#!/bin/bash
read sd
if [ -d "~/tmp/$sd" ]; then
    echo "That directory exists"
else
    echo "That directory doesn't exists"
fi
;;

我指向正确的目录吗? 我希望用户输入将被放入“sd”的内容,如果该子目录存在,那么它会说它确实存在,如果没有,那么它将转到else并说它不存在。

尝试:

if [ -d ~/tmp/"$sd" ]; then

要么:

if [ -d "$HOME/tmp/$sd" ]; then

引号可防止~扩展到您的主目录。

试试这个:-

#!/bin/bash
read sd
if [ -d ~/tmp/"$sd" ]; then
    echo "That directory exists"
else
    echo "That directory doesn't exists"
fi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM