linux - 庆典 shell : syntax error near unexpected token `else'

标签 linux bash shell scripting

<分区>

我正在按照说明执行对我来说似乎很清楚的脚本,在运行它时我被告知第 36 行有错误。似乎无法理解问题。

line 36: syntax error near unexpected token `else'
line 36: `  else'

代码:

   if [ "$answer" = "y" ] #Backup all VMs if answer is yes
  then
          for num in 1 2 3 #Determiant loop for 3 arguments: 1, 2, and 3
  do
          echo "Backing up VM #$num"
          gzip < /var/lib/libvirt/images/centos$num.qcow2 > /root/centos$num.qcow2.backup.gz
          echo "VM #$num BACKUP DONE"
  done

  elif [ "$answer = "n" ]
  then
          read -p "Which VM should be backed up? '(1/2/3)': " numanswer
   until echo "$numanswer" | grep "^[123]$" >> /dev/null # Look for match of single digit: 1, 2, or 3
  do
           read -p "Invalid Selection. Select 1,2, or 3: " numanswer
          echo "Backing up VM #$numanswer"
  gzip < /var/lib/libvirt/images/centos$numanswer.qcow2 > /root/centos$numanswer.qcow2.backup.gz
          echo "VM #$numanswer BACKUP DONE":
          else ### line 36 
          echo "Invalid Selection... Aborting program"

  exit2
  fi

最佳答案

脚本应为:

 27 elif [ "$answer = "n" ]
 28 then
 29     read -p "Which VM should be backed up? '(1/2/3)': " numanswer
 30     until echo "$numanswer" | grep "^[123]$" >> /dev/null # Look for match of single digit: 1, 2, or 3
 31     do
 32         read -p "Invalid Selection. Select 1,2, or 3: " numanswer
 33     done
 34     echo "Backing up VM #$numanswer"
 35     gzip < /var/lib/libvirt/images/centos$numanswer.qcow2 > /root/centos$numanswer.qcow2.backup.gz
 36     echo "VM #$numanswer BACKUP DONE":
 37 else
 38     echo "Invalid Selection... Aborting program"
 39     exit2
 40 fi

请注意 33 中的 done,这是必需的。

关于linux - 庆典 shell : syntax error near unexpected token `else' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46208554/

相关文章:

linux - 如何为 GOPATH 使用 Samba 服务器位置?

python - 使用权限从 python 运行 shell 脚本

linux - 仅从 CSV 文件的第 2 行和第 3 行中删除多余的逗号

c++ - 您如何将所有链接依赖项打包到一个 Linux 静态库中?

c - 如何将 ELF 可执行文件转换为 C 代码?生成的 C 代码不需要是人类可读的

python - 从 python 打开终端应用程序并在其中运行自定义脚本

bash shell : grepping between specific string and delimiter?

python - 将 Bash 脚本的剩余 `n-2` 个参数提供给调用的命令

python - Canopy 获取 shell 中的文件列表

linux - 在另一个进程上设置限制