我正在遵循实现脚本的说明,这对我来说似乎很清楚,在运行它时,我被告知第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发布于 2017-09-14 08:25:50
该脚本应为:
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,这是必需的。
发布于 2017-09-14 08:34:19
这似乎工作得很好:
#!/bin/bash
while true
do
read -r -p $'Which VM should be backed up? [1, 2, 3, or All]\n\n\tPlease enter your selection: ' numanswer
case "$numanswer" in
1)
echo "Backing up VM #1"
gzip < /var/lib/libvirt/images/centos1.qcow2 > /root/centos1.qcow2.backup.gz
echo "VM #1 BACKUP DONE"
break
;;
2)
echo "Backing up VM #2"
gzip < /var/lib/libvirt/images/centos2.qcow2 > /root/centos2.qcow2.backup.gz
echo "VM #2 BACKUP DONE"
break
;;
3)
echo "Backing up VM #3"
gzip < /var/lib/libvirt/images/centos3.qcow2 > /root/centos3.qcow2.backup.gz
echo "VM #3 BACKUP DONE"
break
;;
All)
for num in 1 2 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
break
;;
*)
echo "Invalid input"
continue
;;
esac
done发布于 2017-09-14 08:18:37
您有一条until语句,但没有用于关闭循环的done。所以else迷失在他们的。
https://stackoverflow.com/questions/46208554
复制相似问题