第10行的语法错误:'fi'意外[重复]

时间:2022-10-20 22:42:49

This question already has an answer here:

这个问题在这里已有答案:

CorrectInstance=DEV
Instance_Name=DEV
if [ $Instance_Name == $CorrectInstance ]
then
   echo "Instance validated sucessfully"
else
   echo "Instance validation failed. Script exited."
   exit
fi

When I am running above script I am getting below error.

当我在脚本上面运行时,我遇到了错误。

Match.sh[3]: ^M: not found [No such file or directory]
Match.sh: line 3: syntax error at line 10: `fi' unexpected

Please sugeest

2 个解决方案

#1


^M is also \r So probably you had this on a Windows system at some point and edited it there where the end of line sequence is \r\n. Try running dos2unix on it if you have it available, otherwise you can use a tool like sed to remove whitespace from the ends of lines.

^ M也是\ r \ n所以可能你在某个时候在Windows系统上有这个并在那里编辑它,其中行序列的结尾是\ r \ n。如果有可用的话,尝试在其上运行dos2unix,否则你可以使用像sed这样的工具从行的末尾删除空格。

#2


Depending on the unix, I think that "==" is wrong.
It is supposed to be one equal sign. Note the spaces around the equal sign.
If you run it without those spaces it might assign a variable and return true instead of running any test.

根据unix,我认为“==”是错误的。它应该是一个等号。注意等号周围的空格。如果在没有这些空格的情况下运行它,它可能会分配一个变量并返回true而不是运行任何测试。

Put the variables in quotes too. That way if they are missing you have "" instead of a syntax error.

将变量也放在引号中。这样,如果它们丢失了,你就会有“”而不是语法错误。

And I use those {} just out of habit.

我只是出于习惯而使用那些{}。

If I have $XY=stuff

如果我有$ XY =东西

print ${XY}123 returns stuff123

print $ {XY} 123返回stuff123

print $XY123 returns Nothing because variable $XY123 is not defined.

print $ XY123返回Nothing,因为未定义变量$ XY123。

Redoing your bombed out line 3.
if [ "${Instance_Name}" = "${CorrectInstance}" ]; then

重做被轰炸的第3行。如果[“$ {Instance_Name}”=“$ {CorrectInstance}”];然后

blah blah blah

#1


^M is also \r So probably you had this on a Windows system at some point and edited it there where the end of line sequence is \r\n. Try running dos2unix on it if you have it available, otherwise you can use a tool like sed to remove whitespace from the ends of lines.

^ M也是\ r \ n所以可能你在某个时候在Windows系统上有这个并在那里编辑它,其中行序列的结尾是\ r \ n。如果有可用的话,尝试在其上运行dos2unix,否则你可以使用像sed这样的工具从行的末尾删除空格。

#2


Depending on the unix, I think that "==" is wrong.
It is supposed to be one equal sign. Note the spaces around the equal sign.
If you run it without those spaces it might assign a variable and return true instead of running any test.

根据unix,我认为“==”是错误的。它应该是一个等号。注意等号周围的空格。如果在没有这些空格的情况下运行它,它可能会分配一个变量并返回true而不是运行任何测试。

Put the variables in quotes too. That way if they are missing you have "" instead of a syntax error.

将变量也放在引号中。这样,如果它们丢失了,你就会有“”而不是语法错误。

And I use those {} just out of habit.

我只是出于习惯而使用那些{}。

If I have $XY=stuff

如果我有$ XY =东西

print ${XY}123 returns stuff123

print $ {XY} 123返回stuff123

print $XY123 returns Nothing because variable $XY123 is not defined.

print $ XY123返回Nothing,因为未定义变量$ XY123。

Redoing your bombed out line 3.
if [ "${Instance_Name}" = "${CorrectInstance}" ]; then

重做被轰炸的第3行。如果[“$ {Instance_Name}”=“$ {CorrectInstance}”];然后

blah blah blah