如何在 bash 脚本中循环执行命令列表 (list.txt)?
How can I loop through a list of commands (list.txt) in a bash script?
我在一个txt文件中有1000条命令的列表,我想在cmd中一个一个地执行它们。所以我为他们写了这个循环:
for command in list.txt;
do
$command
done
或
while read command
do
$command
done < list.txt
None 虽然有效!我应该用 IFS
表示分隔符吗?命令是 wget 命令,例如:
wget -w 3 --random-wait url -o filename
它们在 list.txt 中各占一行。最后但同样重要的是,有没有办法在每个命令之间进行延迟?当我一次复制粘贴所有命令时,它会忽略我在 wget 命令中指示的延迟,并且不会很好地对待服务器!!
给定文件 list.txt
的 bash 命令:
echo 'Command #1'
ls -1
echo 'Command #2'
sar 1 2
echo 'Command #3'
hostname
只需按运行:
执行文件中的所有命令
bash list.txt
示例输出为:
Command #1
list.txt
Command #2
Linux 2.6.32-504.16.2.el6.x86_64 (my.server.org) 06/02/2016 _x86_64_ (1 CPU)
08:56:48 AM CPU %user %nice %system %iowait %steal %idle
08:56:49 AM all 0.00 0.00 0.99 0.00 0.00 99.01
08:56:50 AM all 16.00 0.00 14.00 1.00 0.00 69.00
Average: all 7.96 0.00 7.46 0.50 0.00 84.08
Command #3
my.server.org
关于您的延迟请求,请尝试增加等待时间 (wget -w 10
) 或在每个命令后向您的 list.txt
文件添加 sleep 10
。
这是个坏主意,但如果你必须:
while IFS= read -r line <&3; do
line=${line%$'\r'} # trim DOS newlines
printf 'Evaluating following line: %s\n' "$line" >&2
eval "$line"
sleep 1
done 3<list.txt
值得注意的项目:
- 使用
while read
循环避免了读取文件时涉及的许多注意事项。参见 BashFAQ #1, and DontReadLinesWithFor。 [另外,for line in foo.txt
只迭代一次,foo.txt
-- 文件名本身 -- 作为 $line
的值,所以 永远不会 正确] .
- 对
read
使用 -r
参数会导致反斜杠文字被接受。
- 清除
IFS
变量可防止删除前导和尾随空格。
- 对文件内容使用文件描述符 3 可防止文件中的任何命令 运行 使用 stdin,从而防止文件的其余部分被 运行.
- DOS 文本文件使用 CRLF 换行符,而 UNIX 文本文件使用 CR。只要。
$'\r'
是 LF 字符的 bash 表示; ${foo%$'\r'}
是一个参数扩展,它从变量 foo
. 中去除任何尾随的 LF
你甚至不需要脚本。
$ cat list.txt | xargs $command
如果您的命令一次只能处理一个参数。
$ cat list.txt | xargs -n 1 $command
我在一个txt文件中有1000条命令的列表,我想在cmd中一个一个地执行它们。所以我为他们写了这个循环:
for command in list.txt;
do
$command
done
或
while read command
do
$command
done < list.txt
None 虽然有效!我应该用 IFS
表示分隔符吗?命令是 wget 命令,例如:
wget -w 3 --random-wait url -o filename
它们在 list.txt 中各占一行。最后但同样重要的是,有没有办法在每个命令之间进行延迟?当我一次复制粘贴所有命令时,它会忽略我在 wget 命令中指示的延迟,并且不会很好地对待服务器!!
给定文件 list.txt
的 bash 命令:
echo 'Command #1'
ls -1
echo 'Command #2'
sar 1 2
echo 'Command #3'
hostname
只需按运行:
执行文件中的所有命令bash list.txt
示例输出为:
Command #1
list.txt
Command #2
Linux 2.6.32-504.16.2.el6.x86_64 (my.server.org) 06/02/2016 _x86_64_ (1 CPU)
08:56:48 AM CPU %user %nice %system %iowait %steal %idle
08:56:49 AM all 0.00 0.00 0.99 0.00 0.00 99.01
08:56:50 AM all 16.00 0.00 14.00 1.00 0.00 69.00
Average: all 7.96 0.00 7.46 0.50 0.00 84.08
Command #3
my.server.org
关于您的延迟请求,请尝试增加等待时间 (wget -w 10
) 或在每个命令后向您的 list.txt
文件添加 sleep 10
。
这是个坏主意,但如果你必须:
while IFS= read -r line <&3; do
line=${line%$'\r'} # trim DOS newlines
printf 'Evaluating following line: %s\n' "$line" >&2
eval "$line"
sleep 1
done 3<list.txt
值得注意的项目:
- 使用
while read
循环避免了读取文件时涉及的许多注意事项。参见 BashFAQ #1, and DontReadLinesWithFor。 [另外,for line in foo.txt
只迭代一次,foo.txt
-- 文件名本身 -- 作为$line
的值,所以 永远不会 正确] .- 对
read
使用-r
参数会导致反斜杠文字被接受。 - 清除
IFS
变量可防止删除前导和尾随空格。 - 对文件内容使用文件描述符 3 可防止文件中的任何命令 运行 使用 stdin,从而防止文件的其余部分被 运行.
- 对
- DOS 文本文件使用 CRLF 换行符,而 UNIX 文本文件使用 CR。只要。
$'\r'
是 LF 字符的 bash 表示;${foo%$'\r'}
是一个参数扩展,它从变量foo
. 中去除任何尾随的 LF
你甚至不需要脚本。
$ cat list.txt | xargs $command
如果您的命令一次只能处理一个参数。
$ cat list.txt | xargs -n 1 $command