簡體   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