繁体   English   中英

在bash中的文本文件中运行命令

[英]Run commands in text file in bash

如何从文本文件中的一行运行命令,文本文件的一行看起来像这样

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

我有一个文件,该文件具有由外部程序生成的数千行,并希望执行其中的每一行。 我需要像在bash shell中一样运行每一行

您可以运行:

bash file.txt 

您可以在下面使用,但是我极不建议您从文件执行1000条命令,

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

如何使用./this_file_name.sh file_with_commands

you$ bash somefile.txt

只要确保您的文件是可执行文件( chmod 744 somefile.txt

带点('。')(我删除了&,因为如果命令在后台运行它将无法正常工作)

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

. somefile.txt

暂无
暂无

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

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