我按照指示执行一个看起来很清楚的脚本,运行它后我告诉第36行有错误。似乎无法理解这个问题。< / p>
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
答案 0 :(得分:1)
该脚本应为:
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
,这是必需的。
答案 1 :(得分:1)
这似乎很有效:
#!/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
答案 2 :(得分:0)
您有一个until
语句但没有done
来关闭循环。所以else
迷失了。
答案 3 :(得分:-1)
删除第35行的尾部冒号