简体   繁体   English

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

[英]Unix Bash Shell Programming if directory exists

So I'm trying to get into an if statement in a bash shell script but I think I'm doing something wrong, anyways here's my sample code. 所以我试图在bash shell脚本中进入if语句,但我认为我做错了什么,反正这里是我的示例代码。

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

Am I pointing it to the correct directory? 我指向正确的目录吗? I want the user to input something which will be put into "sd" and if that subdirectory exists then it'll say it does, if not then it will go to the else and say it doesn't exist. 我希望用户输入将被放入“sd”的内容,如果该子目录存在,那么它会说它确实存在,如果没有,那么它将转到else并说它不存在。

Try: 尝试:

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

or: 要么:

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

Quoting prevents expansion of ~ into your home directory. 引号可防止~扩展到您的主目录。

Try this:- 试试这个:-

#!/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