Bash 用户输入指定脚本中的变量数量,然后为每个变量分配一个值

Bash 用户输入指定脚本中的变量数量,然后为每个变量分配一个值,bash,variables,Bash,Variables,我试图允许用户输入他们想要与之交互的ip地址的数量,然后输入每个ip地址并将其分配给一个变量。该脚本最终将编写并执行第二个脚本。使用第二个脚本的原因是,为了将x个AP聚集在一起,我正在对AP进行ssh,并且一旦发生ssh,就不再传递bash/python变量(AP有自己的语言),因此必须在运行ssh脚本之前将它们翻译为纯文本。下面的代码起作用,但只允许2个ip地址(我不知道如何使用$cvar创建多个变量),并且不允许我决定输入多少ip地址: #!/bin/bash echo -e "How ma

我试图允许用户输入他们想要与之交互的ip地址的数量,然后输入每个ip地址并将其分配给一个变量。该脚本最终将编写并执行第二个脚本。使用第二个脚本的原因是,为了将x个AP聚集在一起,我正在对AP进行ssh,并且一旦发生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
#/bin/bash
echo-e“您希望群集多少AP?”
#这是定义要请求多少IP的变量
读取cvar
echo-e“输入AP的最后两位数字#1:”
阅读ip1
echo-e“输入AP#2的最后两位数字:
阅读ip2
#我希望x个ip地址(由$cvar定义)的情况继续下去
echo-e“输入集群的名称:”
阅读cname
#以下代码在AP终端内执行(命令对于该外壳是唯一的)
回声“#!/bin/bash

ssh-Tadmin@192.168.0.$ip1在不重写整个脚本的情况下,下面是一个片段

#!/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

数组是您在这里的朋友

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

为此,您需要一个数组。您有两个答案建议您使用数组,这应该足以让您自己改进脚本,因此我不会再添加另一个数组。相反,我会让您参考的底部的bash函数来帮助您验证IP地址。:-)当然,正确的脚本会验证在continui之前输入的值ng on。谢谢你的回答。我仍然有将它们注入第二个脚本的问题。收集的变量需要写入ssh脚本(./2ndScript.sh)中的特定位置。如何为每个ip[$I]编写“添加成员…”行由用户输入?@Brock您可以使用循环,如我在第二个
for loop
示例中所示。我正在编辑更多信息,但我对这一行感到困惑;
ssh-Tadmin@192.168.0.$ip1这需要一些草率的工作,但我想出来了:为了((i=0;i>tempfile.txt。然后:cat tempfile.txt>>。/2ndScript.sh rm tempfile.txt ssh线用于与第一个AP接口。$ip1是“拨号”的ip地址。下面的
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