Bash 脚本因“条件表达式中的语法错误”而失败

Eth*_*han 1 linux script bash

我是 bash 脚本的新手,我只想做一个简单的小事,但是我在网上阅读的所有内容似乎都不适合我!

所以我在这里有这个脚本:

  #!/bin/bash
  if [[ $1 = "32"]]
  then
      mv config.h config64.h
      mv config32.h config.h
      mv Makefile Makefile64
      mv Makefile32 Makefile
      echo "READY FOR 32 BITS!"
  elif [[ $2 = "64" ]]
  then
      mv config.h config32.h
      mv config64.h config.h
      mv Makefile Makefile32
      mv Makefile64 Makefile
      echo "READY FOR 64 BITS!"
  fi
Run Code Online (Sandbox Code Playgroud)

我得到错误:

./switch-bits.sh: line 3: syntax error in conditional expression
./switch-bits.sh: line 4: syntax error near `then'
./switch-bits.sh: line 4: `then'
Run Code Online (Sandbox Code Playgroud)

所以我的手被扔在了这里……怎么了?

Uwe*_*Uwe 8

你需要在"32"和之间留一个空格]]


Flo*_*ris 8

为了详细说明我的评论,我建议按如下方式更改您的脚本:

#!/bin/bash
if [[ $1 = "32" ]]
then
  rm config.h
  ln -s config32.h config.h
  rm Makefile
  ln -s Makefile32 Makefile
  echo "READY FOR 32 BITS!"
elif [[ $1 = "64" ]]
then
  rm config.h
  ln -s config64.h config.h
  rm Makefile
  ln -s Makefile64 Makefile
  echo "READY FOR 64 BITS!"
fi
Run Code Online (Sandbox Code Playgroud)

还有一个更短(?更聪明)的版本:

#!/bin/bash
rm config.h
ln -s config$1.h config.h
rm Makefile
ln -s Makefile$1 Makefile
echo "READY FOR $1 BITS!"
Run Code Online (Sandbox Code Playgroud)

注意 - 如果您使用较短的版本,最好包括一些错误检查 - 即确保输入是“32”或“64”而不是其他内容。我会把它留给你...