[英]linux script to run commands within a command
首先,我在腳本編寫方面非常虛弱,因此請只接受此算法來解釋我的問題:
階段1:從Server-1獲取所有信息,並將其保存在文本文件中。
階段2:將文本文件復制到Server-2,然后重新運行腳本以創建Server-1中存在的類似信息
階段1:
file1.txt:
第1行
第2行
第3行
第4行
如果我在file1.txt的row1上運行command1,它將得到如下輸出:
./command1 row1 > output
貓輸出
冬季:1456
夏季:5467
春季:2314
秋季:3443
同樣,使用command1的所有其他行將具有其他4個具有不同季節代碼的輸出。 為簡單起見,有4個輸出。 我所處理的-每個命令輸出中都有大約40個輸出。
現在,我需要知道如何將這16個輸出放入output.txt中,以便可以在另一台服務器上重新創建相同的信息?
-
@mofoe的回復后更新:
嘿,首先謝謝你。 我認為這是朝着正確的方向前進。 因此,我認為這將我帶到整理輸出的最后一步,這樣在將數據輸入另一台服務器時將很有用。
考慮此ping命令具有靜態輸出,而忽略任何動態或更改值,請考慮一下它的某些命令輸出,這些輸出將用於輸入位於其他位置的另一台服務器。 由於將運行許多不同的命令以進入系統,因此僅粘貼整個文本文件或整行是無法完成的。 只有一部分是重要和必要的。 再一次,我非常感謝甚至回答了一個me腳的問題。 謝謝。
這就是我們現在的立場。 我正在使用Ping命令向您解釋這種情況,它實際上與ping無關。 將其視為其他命令的標准和靜態輸出。
[root@host-11 test]# cat file1.txt
#only 2 hosts are typed in here, originally there are 500+
host-11
host-12
[root@host-11 test]# cat script
#!/bin/sh
while read line; do
/bin/ping -c 4 $line >> output.txt
done < file1.txt
[root@host-11 test]# cat output.txt
PING host-11 (x.x.x.x) 56(84) bytes of data.
64 bytes from host-11 (x.x.x.x): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from host-11 (x.x.x.x): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from host-11 (x.x.x.x): icmp_seq=3 ttl=64 time=0.038 ms
64 bytes from host-11 (x.x.x.x): icmp_seq=4 ttl=64 time=0.034 ms
--- host-11 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.023/0.032/0.038/0.009 ms
PING host-12 (y.y.y.y) 56(84) bytes of data.
64 bytes from host-12 (y.y.y.y): icmp_seq=1 ttl=64 time=0.228 ms
64 bytes from host-12 (y.y.y.y): icmp_seq=2 ttl=64 time=0.267 ms
64 bytes from host-12 (y.y.y.y): icmp_seq=3 ttl=64 time=0.264 ms
64 bytes from host-12 (y.y.y.y): icmp_seq=4 ttl=64 time=0.246 ms
--- host-12 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.228/0.251/0.267/0.019 ms
因此,現在,我需要一種有組織的方式的文本文件,如下所示:
貓輸出.txt
host-name IP icmp_seq1 icmp_seq2 rtt
host-11: (x.x.x.x) time=0.023 time=0.036 0.023/0.032/0.038/0.009
host-12: (y.y.y.y) time=0.228 time=0.267 0.228/0.251/0.267/0.019
** all other hosts that might have been included in the file1.txt
因此,一旦有了這個,我將必須運行4個不同的命令才能在另一台服務器中輸入此命令。 我希望現在更有意義了嗎?
請嘗試以下操作:
#!/bin/sh
while read line; do
./command1 $line >> output.txt
done < file1.txt
該腳本遍歷file1.txt
每一行,在該行上運行./command1
並將輸出附加到output.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.