函数名称作为循环中的变量

函数名称作为循环中的变量

概述:我将变量保存在配置文件中并稍后调用它们。

每个名为 FailOverVM 的条目旁边都有一个数字,如 FailOverVM1,我想检查它是否有数据并生成一个名为 FailOverVM1() 的函数,该函数稍后在脚本中启动 $FailOverVM1Name,它恰好是“Plex Server”

我可以像 StartVM1() 一样手动执行此操作,并且可以工作,但稍后我可能会扩展到 15 个并希望它进行相应调整。

为了澄清,我可以稍后用 Case 语句启动虚拟机,但我无法理解变量本身就是一个变量。我希望我没有让任何人感到困惑。也许我让这种方式比现在或需要的更加复杂。

#!/bin/bash
. "${BASH_SOURCE%/*}/configlocation.conf"
. $Configuration
checkVM1=$(nc -vvz $FailOverVM1IP $FailOverVM1Port 2>&1)
VMCount=$(grep "FailOverVM.Name" /media/VirtualMachines/Current/Configuration.conf | wc -l)
pinggateway=$(ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error = error)
STATE="error";

while [  $STATE == "error" ]; do
    #do a ping and check that its not a default message or change to grep for something else
    STATE=$(ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null && echo ok || echo error)

    #sleep for 2 seconds and try again
    sleep 2
 done

for i $VMCount; do
if [ -z "$FailOverVM$VMCountName" ];
    echo "$FailOverVM$VMCountName"
fi
done

StartVM1(){
  if [[ $checkVM1 = "Connection to $FailOverVM1IP $FailOverVM1Port port [tcp/*] succeeded!" ]]; then
    echo '$FailOverVM1Name is up'
  else
   echo "$FailOverVM1Name down"
   su -c 'VBoxManage startvm $FailOverVM1Name -type headless' vbox
  fi
}

到目前为止我在测试脚本中得到了什么

#!/bin/bash
. "${BASH_SOURCE%/*}/configlocation.conf"
. $Configuration
Pre='$FailOverVM'
post="FailOverVM"
name="Name"
VMCount=$(grep "FailOverVM.Name" $Configuration | wc -l) #Count entires in config file wirn FailOverVM*Name
while [[ $i -le $VMCount ]]
do
#if [ -z $Pre$i"Name" ];then #If the variable $FailOverVM*Name is not blank
   $post$i=$Pre$i$Name
   echo "$post$i" #print it
#else
#    echo $Pre$i"Name" "was empty"
#fi
    ((i = i + 1))
done

输出:

./net2.sh: line 11: FailOverVM=$FailOverVM: command not found
FailOverVM
./net2.sh: line 11: FailOverVM1=$FailOverVM1: command not found
FailOverVM1
./net2.sh: line 11: FailOverVM2=$FailOverVM2: command not found
FailOverVM2
./net2.sh: line 11: FailOverVM3=$FailOverVM3: command not found
FailOverVM3
./net2.sh: line 11: FailOverVM4=$FailOverVM4: command not found
FailOverVM4
./net2.sh: line 11: FailOverVM5=$FailOverVM5: command not found
FailOverVM5
./net2.sh: line 11: FailOverVM6=$FailOverVM6: command not found
FailOverVM6

这里的问题是没有 $FailOverVM 旁边没有数字,而“命令未找到 FailOverVM5”(或任何其他数字)是怎么回事,我不知道我发出了一个。但最大的问题是它没有从配置文件中获取变量 $FailOVerVM* 。我需要它用于 func 循环。




带有 @dave_thompson_085 帮助的新修改脚本

#!/bin/bash
. "${BASH_SOURCE%/*}/configlocation.conf"
. $Configuration
for i in ${!FailOverName[@]}; do 
  selip=FailOverIP[${i}]
  selport=FailOverPort[${i}]
  checkVM[$i]=$(nc -vvz ${!selip} ${!selport} 2>/devnull)
  echo ${!selip}
  echo ${!selport}
  echo FailOverName[${i}]
done

StartVM() { # first argument to a function is accessed as $1 or ${1}
  selname=FailOverName[${i}]
  if [[ checkVM[$i] =~ 'succeeded' ]]; then # only need to check the part that matters
    echo number $i name ${!selname} already up 
  else 
    echo starting number $i name ${!selname}
    echo su -c "VboxManager startvm '${!selname}' -headless" vbox # note " because ' $
  fi
}
#done
StartVM 1 # and 
StartVM 2 # etc

输出

root@6120:~/.scripts# ./net2.sh -v
192.168.1.6
32400
FailOverName[1]
192.168.1.5
80
FailOverName[2]
192.168.1.7
80
FailOverName[3]
192.168.1.1
1030
FailOverName[4]
starting number 4 name finch
su -c VboxManager startvm 'finch' -headless vbox
starting number 4 name finch
su -c VboxManager startvm 'finch' -headless vbox
root@6120:~/.scripts# 

配置文件

#
FailOverVM1IP='192.168.1.6'
FailOverVM1Port='32400'
FailOverVM1Name='Plex Server'
FailOverVM1NASHDD='/media/VirtualMachines/Current/Plex\ Server/Plex\ Server.vmdk'
FailOverVM1LocalHDD='/home/vbox/VirtualBox\ VMs/Plex\ Server/Plex\ Server.vmdk'

FailOverVM2IP='192.168.1.7'
FailOverVM2Port='32402'
FailOverVM1Name='Plex Server2'
FailOverVM2NASHDD='/media/VirtualMachines/Current/Plex\ Server/Plex\ Server.vmdk'
FailOverVM2LocalHDD='/home/vbox/VirtualBox\ VMs/Plex\ Server/Plex\ Server.vmdk'

FailOverVM3IP='192.168.1.8'
FailOverVM3Port='32403'
FailOverVM3Name='Plex Server3'
FailOverVM3NASHDD='/media/VirtualMachines/Current/Plex\ Server/Plex\ Server.vmdk'
FailOverVM3LocalHDD='/home/vbox/VirtualBox\ VMs/Plex\ Server/Plex\ Server.vmdk'

FailOverVM4IP='192.168.1.9'
FailOverVM4Port='32404'
FailOverVM4Name='Plex Server4'
FailOverVM4NASHDD='/media/VirtualMachines/Current/Plex\ Server/Plex\ Server.vmdk'
FailOverVM4LocalHDD='/home/vbox/VirtualBox\ VMs/Plex\ Server/Plex\ Server.vmdk'

FailOverVM5IP='192.168.1.10'
FailOverVM5Port='32405'
FailOverVM5Name='Plex Server5'
FailOverVM5NASHDD='/media/VirtualMachines/Current/Plex\ Server/Plex\ Server.vmdk'
FailOverVM5LocalHDD='/home/vbox/VirtualBox\ VMs/Plex\ Server/Plex\ Server.vmdk'

FailOverIP[1]=192.168.1.6 FailOverName[1]=robin FailOverPort[1]=32400
FailOverIP[2]=192.168.1.5 FailOverName[2]=bluejay FailOverPort[2]=80
FailOverIP[3]=192.168.1.7 FailOverName[3]=sparrow FailOverPort[3]=80
FailOverIP[4]=192.168.1.1 FailOverName[4]=finch FailOverPort[4]=1030

VM1LogDirLogDir='/media/VirtualMachines/Logs/Plextstart'


PlexServerIP='192.168.1.6'
PlexPort='32400'
mydate=`date '+%Y-%m-%d_%H%M'`
rsyncfrom=
NASPlexvmHDD='/media/VirtualMachines/Current/Plex\ Server/Plex\ Server.vmdk'
LocalPlexvmDHDD='/home/vbox/VirtualBox\ VMs/Plex\ Server/Plex\ Server.vmdk'


PlexVMname='Plex Server'
PlexStartLogDir='/media/VirtualMachines/Logs/Plextstart'
RouterIp='192.168.1.1'

因此它可以看到所有虚拟机,但只执行最后一个和两次。



#!/bin/bash
. "${BASH_SOURCE%/*}/configlocation.conf"
. $Configuration
for i in ${!FailOverName[@]}; do 
  selip=FailOverIP[${i}]
  selport=FailOverPort[${i}]
  checkVM[$i]=$(nc -vvz ${!selip} ${!selport} 2>&1)
  echo ${!selip}
  echo ${!selport}
  #echo ${i}
#done

StartVM() { # first argument to a function is accessed as $1 or ${1}
  selname=FailOverName[${i}]
  if [[ $checkVM[$i] =~ 'succeeded' ]]; then # only need to check the part that matters
    echo number $i name ${!selname} already up 
  else 
    echo starting number $i name ${!selname}
    echo su -c "VboxManager startvm '${!selname}' -headless" vbox # note " because ' prevents the variable expansion
  fi
}
StartVM
done

注意:检查虚拟机是否已在运行尚未起作用,但这不是我问的问题,因此这符合标准。

答案1

旁白:您可以wc -l使用 来消除grep -c FailoverVM.Name configfile
但如果您想使用超过 9 位十进制的数字(例如 123456789abcdef),您的模式需要为FailoverVM[0-9][0-9]?NameFailoverVM[0-9]{1,2}Name-E扩展模式。
也是for i $VMCount一个语法错误;我猜你的意思是for i in $(seq $VMCount)


你可以间接读取变量在 bash 中使用!(bang) 和另一个包含名称的变量:

for i in $(seq $VMCount); do
  selname=FailoverVM${i}Name
  selip=FailoverVM${i}IP
  selport=FailoverVM${i}Port
  echo name ${!selname} is IP ${!selip} and port ${!selport}
done

这不像是大错特错,eval但仍然很笨拙。但是您不能以这种方式设置变量,因此您应该使用数组。并且您不能对函数执行此操作,因此请编写一个接受参数的函数来告诉它要使用哪些(一组)变量:

for i in $(seq $VMCount); do 
  selip-Failover${i}IP
  selport=Failover${i}Port
  checkVM[$i]=$(nc -vvz ${!selip} ${!selport} 2>/devnull)
}
StartVM() { # first argument to a function is accessed as $1 or ${1}
  selname=FailoverVM${1}Name
  if [[ checkVM[$1] =~ 'succeeded' ]]
  # only need to check the part that matters
  then echo number $1 name ${!selname} already up 
  else echo starting number $1
    su -c "VboxManager startvm ${!selname} -headless" vbox
  # note " because ' prevents the variable expansion
  fi
]
...
StartVM 1 # and 
StartVM 2 # etc

OTOH,如果您可以更改配置以使用数组变量来完成这样的所有事情

FailoverIP[1]=10.255.1.1 FailoverName[1]=robin
FailoverIP[2]=10.255.2.2 FailoverName[2]=bluejay
etc

那会让一切变得更加简单。然后你不需要重新 grep 文件来计算条目数,你可以使用例如${#FailoverName[@]}

相关内容