英文:
Using read to get user input and assigning that to a variable later on causes it to lose its case
问题
让我们假设我有一个目录,/user/Jack/PROJECT1/tool1。
我尝试在我的Shell脚本中检查是否存在这个目录。
这是我正在处理的代码片段:
echo -e "Enter complete path: "
read verifydir
if [ ! -d $verifydir ]
then
echo -e $verifydir "is not a valid directory"
else
mydir=$verifydir
fi
exedir="$mydir/tool1"
pgmname="$exedir/mytool.exe"
if [ ! -f "$pgmname" ]
then
echo -e $pgmname "not found."
exit -l
fi
我的问题是,虽然第一个检查(目录的存在)有效,但第二个检查(pgmname)如果目录名称全部大写(例如PROJECT1),则无法找到程序。它将PROJECT1转换为project1。我该如何解决这个问题?
英文:
Let's say I have a directory, /user/Jack/PROJECT1/tool1.
I'm trying to check to see if this directory exists in my shell script.
Here's the snippet of code I'm dealing with:
echo -e "Enter complete path: "
read verifydir
if [ ! -d $verifydir ]
then
echo -e $verifydir "is not a valid directory"
else
mydir=$verifydir
fi
exedir="$mydir/tool1"
pgmname=$exedir"/mytool.exe"
if [ ! -f "$pgmname" ]
then
echo -e $pgmname "not found."
exit -l
fi
My problem is that while the first check (the existence of the directory) works, the second check (for pgmname) is not finding the program if the directory name is in all caps (like PROJECT1). It's converting the PROJECT1 to project1. How can I fix this?
答案1
得分: 0
以下是翻译好的部分:
我认为错误出在其他地方,即在下一行:
如果 [ ! -f "pgmname" ]
这应该是
如果 [ ! -f "$pgmname" ]
一旦这个问题解决了,我们可以进一步讨论。
英文:
I think the error is elsewhere, i.e. in next line:
if [ ! -f "pgmname" ]
This should be
if [ ! -f "$pgmname" ]
Once this is fixed, we can talk further.
答案2
得分: -1
你可以使用 ls
如果 [[ $(ls $pgmname) ]]
请详细说明你的问题是什么
英文:
You can use ls
if [[ $(ls $pgmname) ]]
Please elaborate on what your problem is
答案3
得分: -1
我解决了这个问题。之前在代码中,有人设置了typeset -l pgmname。
谢谢。
英文:
I solved the issue. Previously in the code, someone set typeset -l pgmname .
Thanks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论