Bash 如何在此shell脚本的命令行中输入参数?
我在笔记本电脑上设置VMWare ESXi用于学习。我想把事情自动化,得到了这个非常好的shell脚本。它需要cpu内核、iso路径、ram和磁盘空间等参数。我是个初学者。我知道这是一个非常基本的问题,但在互联网上找不到答案。这是密码 我不明白如何输入Bash 如何在此shell脚本的命令行中输入参数?,bash,shell,Bash,Shell,我在笔记本电脑上设置VMWare ESXi用于学习。我想把事情自动化,得到了这个非常好的shell脚本。它需要cpu内核、iso路径、ram和磁盘空间等参数。我是个初学者。我知道这是一个非常基本的问题,但在互联网上找不到答案。这是密码 我不明白如何输入的值-确切地说,所需的语法不清楚 #paratmers: machine name (required), CPU (number of cores), RAM (memory size in MB), HDD Disk size (in GB),
的值-确切地说,所需的语法不清楚
#paratmers: machine name (required), CPU (number of cores), RAM (memory size in MB), HDD Disk size (in GB), ISO (Location of ISO image, optional)
#default params: CPU: 2, RAM: 4096, DISKSIZE: 20GB, ISO: 'blank'
-
phelp() {
echo "Script for automatic Virtual Machine creation for ESX"
echo "Usage: ./create.sh options: n <|c|i|r|s>"
echo "Where n: Name of VM (required), c: Number of virtual CPUs, i: location of an ISO image, r: RAM size in MB, s: Disk size in GB"
echo "Default values are: CPU: 2, RAM: 4096MB, HDD-SIZE: 20GB"
}
CPU=2
RAM=4096
SIZE=20
ISO="ISO'S"
FLAG=true
ERR=false
n=vmoo
while getopts n:c:i:r:s: option
do
case $option in
n)
NAME=${OPTARG};
FLAG=false;
if [ -z $NAME ]; then
ERR=true
MSG="$MSG | Please make sure to enter a VM name."
fi
;;
c)
CPU=${OPTARG}
if [ `echo "$CPU" | egrep "^-?[0-9]+$"` ]; then
if [ "$CPU" -le "0" ] || [ "$CPU" -ge "32" ]; then
ERR=true
MSG="$MSG | The number of cores has to be between 1 and 32."
fi
else
ERR=true
MSG="$MSG | The CPU core number has to be an integer."
fi
;;
i)
ISO=${OPTARG}
if [ ! `echo "$ISO" | egrep "^.*\.(iso)$"` ]; then
ERR=true
MSG="$MSG | The extension should be .iso"
fi
;;
r)
RAM=${OPTARG}
if [ `echo "$RAM" | egrep "^-?[0-9]+$"` ]; then
if [ "$RAM" -le "0" ]; then
ERR=true
MSG="$MSG | Please assign more than 1MB memory to the VM."
fi
else
ERR=true
MSG="$MSG | The RAM size has to be an integer."
fi
;;
s)
SIZE=${OPTARG}
if [ `echo "$SIZE" | egrep "^-?[0-9]+$"` ]; then
if [ "$SIZE" -le "0" ]; then
ERR=true
MSG="$MSG | Please assign more than 1GB for the HDD size."
fi
else
ERR=true
MSG="$MSG | The HDD size has to be an integer."
fi
;;
\?) echo "Unknown option: -$OPTARG" >&2; phelp; exit 1;;
:) echo "Missing option argument for -$OPTARG" >&2; phelp; exit 1;;
*) echo "Unimplimented option: -$OPTARG" >&2; phelp; exit 1;;
esac
done
if $FLAG; then
echo "You need to at least specify the name of the machine with the -n parameter."
exit 1
fi
if $ERR; then
echo $MSG
exit 1
fi
if [ -d "$NAME" ]; then
echo "Directory - ${NAME} already exists, can't recreate it."
exit
fi
mkdir ${NAME}
vmkfstools -c "${SIZE}"G -a lsilogic $NAME/$NAME.vmdk
touch $NAME/$NAME.vmx
cat << EOF > $NAME/$NAME.vmx
config.version = "8"
virtualHW.version = "7"
vmci0.present = "TRUE"
displayName = "${NAME}"
floppy0.present = "FALSE"
numvcpus = "${CPU}"
scsi0.present = "TRUE"
scsi0.sharedBus = "none"
scsi0.virtualDev = "lsilogic"
memsize = "${RAM}"
scsi0:0.present = "TRUE"
scsi0:0.fileName = "${NAME}.vmdk"
scsi0:0.deviceType = "scsi-hardDisk"
ide1:0.present = "TRUE"
ide1:0.fileName = "${ISO}"
ide1:0.deviceType = "cdrom-image"
pciBridge0.present = "TRUE"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge4.functions = "8"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
ethernet0.pciSlotNumber = "32"
ethernet0.present = "TRUE"
ethernet0.virtualDev = "e1000"
ethernet0.networkName = "Inside"
ethernet0.generatedAddressOffset = "0"
guestOS = "other26xlinux-64"
EOF
MYVM=`vim-cmd solo/registervm /vmfs/volumes/datastore1/${NAME}/${NAME}.vmx`
vim-cmd vmsvc/power.on $MYVM
echo "The Virtual Machine is now setup & the VM has been started up. Your have the following configuration:"
echo "Name: ${NAME}"
echo "CPU: ${CPU}"
echo "RAM: ${RAM}"
echo "HDD-size: ${SIZE}"
if [ -n "$ISO" ]; then
echo "ISO: ${ISO}"
else
echo "No ISO added."
fi
echo "Thank you."
exit
参数:机器名称(必需)、CPU(内核数)、RAM(内存大小,以MB为单位)、HDD磁盘大小(以GB为单位)、ISO(ISO映像的位置,可选)
#默认参数:CPU:2,RAM:4096,DISKSIZE:20GB,ISO:“空白”
-
phelp(){
echo“用于为ESX自动创建虚拟机的脚本”
echo“用法:./create.sh选项:n”
echo“其中n:虚拟机的名称(必需),c:虚拟CPU的数量,i:ISO映像的位置,r:以MB为单位的RAM大小,s:以GB为单位的磁盘大小”
echo“默认值为:CPU:2,RAM:4096MB,HDD-SIZE:20GB”
}
CPU=2
RAM=4096
尺寸=20
ISO=“ISO的”
FLAG=true
错误=错误
n=vmoo
而getopts n:c:i:r:s:option
做
案例中的$option
n)
NAME=${OPTARG};
FLAG=false;
如果[-z$NAME];然后
错误=正确
MSG=“$MSG |请确保输入虚拟机名称。”
fi
;;
(c)
CPU=${OPTARG}
如果[`echo“$CPU”| egrep”^-?[0-9]+$”`];然后
如果[“$CPU”-le“0”]| |[“$CPU”-ge“32”];然后
错误=正确
MSG=“$MSG |核心数必须介于1和32之间。”
fi
其他的
错误=正确
MSG=“$MSG | CPU核心编号必须为整数。”
fi
;;
(一)
ISO=${OPTARG}
如果[!`echo“$ISO”| egrep”^.*\.(ISO)$“`];然后
错误=正确
MSG=“$MSG |扩展名应为.iso”
fi
;;
(r)
RAM=${OPTARG}
如果[`echo“$RAM”| egrep”^-?[0-9]+$”`];然后
如果[“$RAM”-le“0”];然后
错误=正确
MSG=“$MSG |请为VM分配超过1MB的内存。”
fi
其他的
错误=正确
MSG=“$MSG | RAM大小必须为整数。”
fi
;;
(s)
大小=${OPTARG}
如果[`echo“$SIZE”| egrep”^-?[0-9]+$”`];然后
如果[“$SIZE”-le“0”];然后
错误=正确
MSG=“$MSG |请为硬盘大小分配超过1GB的数据。”
fi
其他的
错误=正确
MSG=“$MSG |硬盘大小必须为整数。”
fi
;;
\?)echo“未知选项:-$OPTARG”>&2;phelp;出口1;;
:)echo“缺少-$OPTARG的选项参数”>&2;phelp;出口1;;
*)echo“未限制选项:-$OPTARG”>&2;phelp;出口1;;
以撒
完成
若为$FLAG;然后
echo“您至少需要使用-n参数指定机器的名称。”
出口1
fi
如果$ERR;然后
echo$MSG
出口1
fi
如果[-d“$NAME”];然后
echo“目录-${NAME}已存在,无法重新创建它。”
出口
fi
mkdir${NAME}
vmkfstools-c“${SIZE}”G-a lsilogic$NAME/$NAME.vmdk
触摸$NAME/$NAME.vmx
类别$NAME/$NAME.vmx
config.version=“8”
virtualHW.version=“7”
vmci0.present=“TRUE”
displayName=“${NAME}”
floppy0.present=“FALSE”
numvCPU=“${CPU}”
scsi0.present=“TRUE”
scsi0.sharedBus=“无”
scsi0.virtualDev=“lsilogic”
memsize=“${RAM}”
scsi0:0.present=“TRUE”
scsi0:0.fileName=“${NAME}.vmdk”
scsi0:0.deviceType=“scsi硬盘”
ide1:0.present=“TRUE”
ide1:0.fileName=“${ISO}”
ide1:0.deviceType=“cdrom映像”
pciBridge0.present=“TRUE”
pciBridge4.present=“TRUE”
pciBridge4.virtualDev=“pcieRootPort”
pciBridge4.functions=“8”
pciBridge5.present=“TRUE”
pciBridge5.virtualDev=“pcieRootPort”
pciBridge5.functions=“8”
pciBridge6.present=“TRUE”
pciBridge6.virtualDev=“pcieRootPort”
pciBridge6.functions=“8”
pciBridge7.present=“TRUE”
pciBridge7.virtualDev=“pcieRootPort”
pciBridge7.functions=“8”
ethernet0.pciSlotNumber=“32”
ethernet0.present=“TRUE”
ethernet0.virtualDev=“e1000”
ethernet0.networkName=“内部”
ethernet0.generatedAddressOffset=“0”
guestOS=“其他26xlinux-64”
EOF
MYVM=`vim cmd solo/registervm/vmfs/volumes/datastore1/${NAME}/${NAME}.vmx`
vim cmd vmsvc/power.on$MYVM
echo“虚拟机现在已设置&虚拟机已启动。您的配置如下:”
echo“Name:${Name}”
回显“CPU:${CPU}”
回显“RAM:${RAM}”
echo“硬盘大小:${size}”
如果[-n“$ISO”];然后
回显“ISO:${ISO}”
其他的
echo“未添加ISO。”
fi
回声:“谢谢你。”
出口
将它们作为
-k值
对输入,其中k
是用于标识参数的字母
例如:
./create.sh -n vmname -c 2 -i /path/to/file.iso -s 512
这会将名称设置为
vmname
,CPU数量设置为2,ISO文件名设置为/path/to/file.ISO
,等等。使用消息对您毫无帮助,但仍试图传递信息。没有代码,我就不知道该怎么办。幸运的是,通过shell脚本,您可以看到代码。用法消息应更类似于:
{
echo "Usage: $0 -n 'vm name' [-c cpus][-i iso-image][-r ram][-s disk]"
echo " -n 'vm name' Name of VM (required)"
echo " -c cpus Number of virtual CPUs"
echo " -i iso-image Location of an ISO image"
echo " -r ram RAM size in MB"
echo " -s disk Disk size in GB"
echo "Default values are: CPU: 2, RAM: 4096MB, HDD-SIZE: 20GB"
} >&2
注意:编写shell脚本时,请确保使用信息清楚地记录在使用消息中,或者在脚本中的注释中。您展示的shell脚本就是一个如何不执行此操作的示例。如果没有脚本中实际解析参数的部分,我不知道我们应该如何提供帮助。用法文本