学习脚本的第一个程序。
经验教训:简单问题不能复杂化。实现简易和复杂功能的区别对待,不要让自己的程序四不像。不断出现的错误可以帮助自己犯更少的错误。经验的积累来于此。
#!/bin/bash
#
#This is create by cXX
#
installvm(){
#########################################################
# 虚拟机的创建 #
#########################################################
sleep 4
echo "--------create virtual machine now!-----------"
vmname=$USER
path=/home/$vmname/virual
vmpath=/home/$vmname/.VirtualBox
vms=
vms=$(VBoxManage list vms | grep 'Name:'|head -1| awk '{print $2}')
echo "$vms"
for name4 in $(ls -al /home/$vmname|grep '{*}')
do
rm -rf $name4
done
while [ "$vms" != "" ]##[ -n "$vms" ]
do
if [ -e "$vmpath/VDI" ];then
for vdiname in ${vmpath}/VDI/* ##The vdiname is eq ${vmpath}/VDI/* when there is no file in
## the directory
do
echo "delete vdi now!"
rm -rf $vdiname
echo $vdiname
done
fi
echo "it is in unregistervm!"
echo "$vms"
if VBoxManage unregistervm $vms -delete;then
vms=$(VBoxManage list vms | grep 'Name:'|head -1| awk '{print $2}')
fi
if [ -e "${path}.xml" ];then
rm -rf ${path}.xml
vms=""
fi
done
if [ -e "${path}.xml" ];then
rm -rf ${path}.xml
fi
echo "del xml sucess"
echo "the path is $path"
if VBoxManage createvm -name $vmname -register -settingsfile $path
then echo "the virtual Machine have created!"
else echo "it can not create virtual Machine!" ; exit 11
fi
echo "-----------create virtual machine end----------"
echo "#modify memory"
echo "10"
#########################################################
# 指定内存 #
#########################################################
testmemory(){
echo "it is in $FUNCNAME"
}
echo "-----------modify the Memory now!-----------"
memsize=512
if VBoxManage modifyvm $vmname -memory $memsize
then echo "modify vm memory sucess!"
else echo "modify vm memory failed!" ; exit 22
fi
echo "----------modify the memory end-----------"
testdisk(){
echo "it is in $FUNCNAME"
}
i=20
startdevice(){
echo "------start the device $1 now!-----------"
if VBoxManage modifyvm $vmname $1 $2
then echo "start device sucess!"
else echo "start device failed!" exit 88
fi
echo "#add device $1 now...."
# sleep 2
echo "$i"
let "i+=3"
echo "--------start the device $1 end!----------"
}
#########################################################
# usb设备的必要添加