zsh:解析错误:预期的条件:“$ 1”

时间:2013-02-06 10:08:02

标签: zsh

我尝试用gentoos java-config编写脚本来编译java文件,但我最终收到错误 zsh:解析错误:预期的条件:“$ 1”任何人都可以告诉我这意味着什么以及为什么它会在函数的第16行报告。

function jComp() {

local java_mods = ""

if (( $# == 0)); then
  echo "using javac on .java in folder"
  `javac *.java`
  return 0

elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
  echo "Usage: jComp [java modules] [file]"
  echo
  echo "Options:"
  echo "    -h,   This help message."
  echo "modules has to be in the (java-config -l) list" 
  echo
  echo "Report bugs to <tellone.diloom@gmail.com>."

  return 0
fi

if [[ "$(java-config -v)" == "" ]]; then
  echo "This script depends on java-config"
  return 1
elif [[ "$1" =="-d" ]] || [[ "$1" == "--default"]]; then
 `javac -cp .:$(java-config -p junit-4) *.java`
  if [[ $# == 2 ]]; then
    `javac -c .:$(java-config -p junit-4) "$2"`
     return 0
  fi
fi


while (( $# > 1 )); do
if [[ ! -f "$1" ]]; then
    java_mods="$java_mods $1"
    shift
    continue
fi
done

`javac -cp .:$(java-config $java_mods)`

return 0
}

欢迎链接和评论。提前致谢

2 个答案:

答案 0 :(得分:6)

看起来您的代码正在尝试将存储在参数$ 1中的字符串与字符串-d进行比较,但比较在双等号后面缺少空格:

elif [[ "$1" =="-d" ]] || [[ "$1" == "--default"]]; then
               ^

elif [[ "$1" == "-d" ]] || [[ "$1" == "--default"]]; then

我没有尝试过代码但是,如果它解决了,请尝试让我知道!

顺便说一句,看起来第二次比较也会因双边方括号前的空格不足而失败:

elif [[ "$1" == "-d" ]] || [[ "$1" == "--default"]]; then
                                                 ^
elif [[ "$1" == "-d" ]] || [[ "$1" == "--default" ]]; then

答案 1 :(得分:0)

所有回退的命令看起来都不对。您想要运行命令,而不是将其输出解释为要运行的命令,对吗?如果是这样,请从javac调用中删除所有反引号。

然后[[ "$1" =="-d" ]]中缺少一个空格,使==成为一个单独的标记(另一个标记为leroyse)。