Unix Bash Shell Programming if directory exists

匿名 (未验证) 提交于 2019-12-03 02:51:02

问题:

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.

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

回答1:

Try:

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

or:

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

Quoting prevents expansion of ~ into your home directory.



回答2:

Try this:-

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!