Shell 存储/home/$USER进行测试的一种方法

Shell 存储/home/$USER进行测试的一种方法,shell,Shell,我是shellscript新手,我不希望用户在运行程序时必须键入目录的完整路径 #!/bin/bash # Tests the directory, if there is a return #XXX I want to prevent the user from entering # the entire path /home/$USER echo "Enter search directory: " read dir if test -d "$dir&

我是shellscript新手,我不希望用户在运行程序时必须键入目录的完整路径

#!/bin/bash

# Tests the directory, if there is a return
#XXX I want to prevent the user from entering 
# the entire path /home/$USER

echo "Enter search directory: "
read dir

if test -d  "$dir"
then
    echo "Found directory: $dir"
else
    echo "Directory not found!"
fi
执行示例:

> ./dir
>> Enter the directory: 
>>> /home/$USER/Downloads
>>>> Directory Found: /home/user/Downloads
我想要的是:

> ./dir
>> Enter the directory:
>>> Downloads
>>>> Directory Found: /home/user/Downloads

在第一个问题中包含一些代码做得很好!(Q) 但是。。。我们(我)不知道你漏掉一篇要打字的文章是什么意思。请编辑Q以显示脚本的示例用例。你是说你想用两种方法?您想要
/myScript mySpecialDir
而不是
/myScript
,它会提示您进入搜索目录:?同样,不要在评论中回复,用一个你想如何使用你的脚本以及为什么它现在不起作用的示例来改进你的Q。祝你好运非常感谢。我知道我说过不要发表评论,但当我设法解决它时,我问我的问题时并不清楚。我想存储/home/$USER,这样用户就不必键入。我使用变量dir:,dir=“/home/$USER/$dir”实现了这一点。您改进了您的Q,因此评论很好。很高兴您找到了解决方案!继续发布!(我建议您考虑添加一个变量,如
base\u dir=/home/$USER
,并将其附加到
$dir
。否则您可能会得到
/home/$USER/Downloads/home/$USER/Downloads/?。
。祝您好运。谢谢。我实现了这个,而且效果更好!在第一个问题中包含了一些代码!(Q)但是…我们(我)不知道你省略要键入的段落是什么意思。请编辑你的Q以显示脚本的示例用例。你是说有两种方法可以使用它吗?你想要
/myScript mySpecialDir
而不是仅仅
/myScript
,这将提示你
“进入搜索目录:"
?同样,不要在评论中回复,通过一个示例来改进您的Q,说明您希望如何使用脚本以及为什么它现在不起作用。祝您好运!谢谢!我知道我说过不要评论,但当我设法解决它时,我问我的问题时不清楚。我想存储/home/$USER,这样用户就不必键入。我使用e variable dir:,dir=“/home/$USER/$dir。你改进了你的Q,所以评论很好。很高兴你找到了解决方案!继续发布!(我建议您考虑添加一个变量,如
base\u dir=/home/$USER
,并将其附加到
$dir
。否则您可能会得到
/home/$USER/Downloads/home/$USER/Downloads/?。
。祝您好运。谢谢。我实现了这个,而且它变得更好了!