UNIX的bash shell编程如果目录存在 [英] Unix Bash Shell Programming if directory exists

查看:96
本文介绍了UNIX的bash shell编程如果目录存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图进入的,如果在bash shell脚本语句,但我觉得我做错了什么,反正这是我的样品code。

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
;;

我是不是它指向正确的目录?我希望用户输入一些东西,将进入SD,如果然后这个子目录存在,它会说确实如此,如果不是那么它会去别的,说它不存在。

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.

推荐答案

尝试:

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

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

报价prevents 扩展到你的主目录。

这篇关于UNIX的bash shell编程如果目录存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆