0

我无法弄清楚以下脚本有什么问题。

#!/bin/bash 
if [ "$1" = "x" ] 
then
  echo Pushing web on sharada to origin.
else if [ "$1" = "y" ] 
then 
  echo Pulling web on sharada from origin.
else 
  echo "Usage : arg x to push or y to pull."
fi

我在xterm.

4

4 回答 4

3

你最后错过了关闭“fi”。else if构造实际上不是elif延续,而是新的生活if在前一个的else子句中if

所以,正确格式化你应该有:

#!/bin/bash 
if [ "$1" = "x" ] 
then
  echo Pushing web on sharada to origin.
else 
  if [ "$1" = "y" ] 
  then 
    echo Pulling web on sharada from origin.
  else 
    echo "Usage : arg x to push or y to pull."
  fi
fi # <-- this closes the first "if"
于 2012-12-11T17:46:42.883 回答
2

它应该是elif,而不是else if,如下所示:

if [ "$1" = "x" ] 
then
  echo Pushing web on sharada to origin.
elif [ "$1" = "y" ] 
then 
  echo Pulling web on sharada from origin.
else 
  echo "Usage : arg x to push or y to pull."
fi
于 2012-12-11T17:47:17.533 回答
1

你有两个ifs,因此你需要两个fis。

于 2012-12-11T17:46:44.773 回答
0

问题中描述的问题已经解决。我只是想在这里分享我的经验,因为我得到的错误消息是相同的,即使原因和解决方案略有不同。

#!/bin/bash

echo "a"

if [ -f x.txt ]; then \
    echo "b" \
fi;

echo "c"

该脚本产生了相同的错误消息,解决方案是将缺少的分号添加到echo "b"行尾:

#!/bin/bash

echo "a"

if [ -f x.txt ]; then \
    echo "b" ; \
fi;

echo "c"

直接原因与原问题相同。if没有关闭fi对子。然而,原因是由于缺少分号,该fi行混入了前一行。;

于 2020-12-16T06:14:07.827 回答