繁体   English   中英

bash脚本完成后,更改变量并重新启动脚本

[英]After bash script completion, change variable and restart script

我有一些工作脚本,在开始时输入启动参数(服务器IP,用户登录名和root登录名),每次需要在另一台服务器上重新启动此脚本时,都需要编辑脚本以更改服务器IP变量。 我如何更改此设置,以便在变量中输入一堆IP地址,也许在某些数组中,并且当脚本以第一个IP结尾时,它将转到第二个,依此类推,直到IP列表的末尾?

脚本示例:

##!/bin/bash

serv_address="xxx.xx.xx.xxx"

"here goes some script body"

使用文本文件来存储ips之类的

$ cat ip.txt
xxx.xx.xx.xxx
xxx.xx.xx.xxx
xxx.xx.xx.xxx
xxx.xx.xx.xxx

然后修改您的脚本

#!/bin/bash
while read ip
do
#some command on "$ip"
done<ip.txt # Your text file fed to while loop here

或使用bash数组

declare ip_array=( xxx.xx.xx.xxx xxx.xx.xx.xxx xxx.xx.xx.xxx )
for ip in "${ip_array[@]}"
do
#something with "$ip"
done

两者都使您可以灵活地稍后添加/删除IP地址。

您确实确实需要一个数组,然后可以通过循环对其进行迭代。

serv_address=(xxx.xx.xx.xxx yyy.yy.yyy.yy)

for address in "${serv_address[@]}"; do
    if ! ping -c 1 "$serv_address"; then
        echo "$serv_address is not available" >&2
        continue
    fi
    # Do some stuff here if the address did respond.
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM