2017-06-16 74 views
0

如何從一個文本文件中的線路運行命令,我的文本文件的一行看起來像這樣在文本文件中運行命令在bash

echo "RouterPing;`ping -c4 -w4 -q DeviceIP| tail -2 |awk '{print}' ORS=' '`;$(date)" >> somefile.txt & 

我有了成千上萬行的文件由外部程序生成,並希望執行其中的每一行。我需要每行必須準確運行,就好像我從bash shell中運行它

回答

1

您可以在下面使用,但我會強烈建議不要從文件執行1000個命令,

#!/usr/bin/bash 
filename="$1" 
while read -r line 
do 
$line 
    done < "$filename" 

如何使用 ./this_file_name.sh file_with_commands

1
you$ bash somefile.txt 

只要確保你的文件是可執行文件(chmod 744 somefile.txt

+0

其實,如果你調用的bash像這個文件不需要是可執行的。 Bash將只執行文件而不管頭文件或文件擴展名。 – stobiewankenobi

+0

謝謝澄清@ChristopherStobie。 只有在文件被調用時才需要執行模式,例如'./ somefile.txt' – jgmh

0

與點(「」)(我刪除了&,因爲如果在後臺運行該命令它不會工作)

echo "RouterPing;`ping -c4 -w4 -q DeviceIP| tail -2 |awk '{print}' ORS=' '`;$(date)" >> somefile.txt 

. somefile.txt