2017-06-16 77 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