Using read to get user input and assigning that to a variable later on causes it to lose its case.

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月7日 00:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定