【问题标题】:User input to designate how many variables are in a script, then assign a value to each variable用户输入指定脚本中有多少个变量,然后为每个变量分配一个值
【发布时间】:2017-07-23 04:15:31
【问题描述】:

我试图让用户输入他们想要与之交互的 IP 地址的数量,然后输入每个 IP 地址并将其分配给一个变量。该脚本最终将编写并执行第二个脚本。第二个脚本的原因是我正在 ssh-ing 到一个 AP,以便将 x 个 AP 聚集在一起,一旦 SSH 发生,bash/python 变量就不再通过(AP 有它自己的语言),所以在运行 ssh 脚本之前,它们必须被翻译成纯文本。下面的代码可以运行,但只允许 2 个 ip 地址(我不知道如何使用 $cvar 创建多个变量),并且不允许我决定输入多少个 ip 地址:

#!/bin/bash
echo -e "How many AP's do you want to Cluster?:"
#this is the variable to define how many ips to ask for
read cvar 

echo -e "Enter last 2 digits of AP #1:"
read ip1
echo -e "Enter last 2 digits of AP #2:"
read ip2
#I want this to continue for x number of ip addresses(defined by $cvar)

echo -e "Enter a name for your Cluster:"
read cname
#code below is executed within the AP terminal(commands are unique to that shell)
    echo "#!/bin/bash
ssh -T admin@192.168.0.$ip1 <<'eof'
configure
cluster
add $cname
add-member 192.168.0.$ip1 user ***** password ********
save
add-member 192.168.0.$ip2 user ***** password ********
save
exit
operate $cname
save
exit
" > ./2ndScript.sh
    chmod a+x ./2ndScript.sh
    /bin/bash ./2ndScript.sh

【问题讨论】:

  • 你需要一个array
  • 您有两个答案建议您使用数组,这应该足以让您自己改进脚本,所以我不会添加另一个。相反,我将向您推荐this answer 底部的 bash 函数,以帮助您验证 IP 地址。 :-)

标签: bash variables


【解决方案1】:

不用重写整个脚本,这里是一个sn-p。

#!/bin/bash

# IP is an array
IP=()

# Read number of IP Addresses to be read in
read -p "How many AP's do you want to Cluster?: " cvar

loop=1
while [ $loop -le $cvar ]
do
    read -p "Enter last 2 digits of AP #${loop}: " IP[$loop]
    loop=$((loop+1))
done

【讨论】:

  • 当然,正确的脚本会在继续之前验证输入的值。
【解决方案2】:

数组是你的朋友。采取以下措施;

echo -e "Enter last 2 digits of AP #1:"
read ip1
echo -e "Enter last 2 digits of AP #2:"
read ip2
#I want this to continue for x number of ip addresses(defined by $cvar)

我们可以创建一个for 循环,然后为每个地址添加一个元素到数组中。在这个for 循环中,$i 将告诉我们当前处于哪个循环,从 0 开始。由于它是自动递增的,因此我们可以使用它来指定要更新的数组索引。

for (( i=0; i<$cvar; i++ )); do
    echo -e "Enter last 2 digits of AP #$((i+1)):"
    read #With no arguments, read assigns the output to $REPLY
    #optional; this allows the user to enter "done" to end prematurely
    #if [[ $REPLY == "done" ]]; then break; fi
    ip[$i]=$REPLY #ip is the name of the array, and $i points to the index

done

如果您使用该可选代码片段,您甚至不必询问用户想要多少个地址。您可以改为将for 循环替换为while true; do,并指示用户输入“完成”(或任何其他退出命令)以结束地址收集(尽管您需要在某处定义i=0,然后递增如果你换成while,它会在循环结束时结束。

现在您有一个从${ip[0]}${ip[n]} 排序的用户输入地址的值列表。您可以稍后使用另一个for 循环来提取它们;

for ((i=0;i<${#ip[*]};i++)); do
    #Insert code here. For example:
    #echo "${ip[$i]} #echos the currently selected value of the array
    #echo "${ip[$i]}" >> file.txt #appends file.txt with the current value
done

【讨论】:

  • 感谢您的出色回答。我仍然有将它们注入第二个脚本的问题。收集到的变量需要写入到 ssh 脚本(./2ndScript.sh)中的特定位置。如何为用户输入的每个 ip[$i] 写一个“add-member ...”行?
  • @Brock 您可以使用我在第二个for loop 示例中展示的循环。我正在编辑更多信息,但我对这条线感到困惑; ssh -T admin@192.168.0.$ip1 &lt;&lt;'eof'。您拥有此处列出的第一个 IP。是否需要为每个 IP 完成整个部分,或者是否也假定第一个 IP 是第二个脚本的连接?
  • 做了一些草率的工作,但我想通了: for ((i=0;i> tempfile.txt 。然后: cat tempfile.txt >> ./2ndScript.sh rm tempfile.txt
  • ssh 线路用于与第一个 AP 连接。 $ip1 是“拨号”的 IP 地址。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 2020-07-09
  • 1970-01-01
  • 2021-06-05
相关资源
最近更新 更多