如何在 Linux 终端中循环一个可执行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11005756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:49:23  来源:igfitidea点击:

How to loop an executable command in the terminal in Linux?

linuxloopsfile-io

提问by Linux Rules

Let me first describe my situation, I am working on a Linux platform and have a collection of .bmpfiles that add one to the picture number from filename0022.bmpup to filename0680.bmp. So a total of 658 pictures. I want to be able to run each of these pictures through a .exe file that operates on the picture then kicks out the file to a file specified by the user, it also has some threshold arguments: lower, upper. So the typical call for the executable is:

让我先介绍我的情况,我的工作在Linux平台上,并有一个集合.bmp,从一个添加到图片文件数量filename0022.bmp最多filename0680.bmp。所以一共有658张图片。我希望能够通过对图片进行操作的 .exe 文件运行这些图片中的每一个,然后将文件踢出到用户指定的文件中,它还具有一些阈值参数:下限、上限。所以对可执行文件的典型调用是:

./filter inputfile outputfile lower upper

Is there a way that I can loop this call over all the files just from the terminal or by creating some kind of bash script? My problem is similar to this: Execute a command over multiple files with a batch filebut this time I am working in a Linux command line terminal.

有没有一种方法可以让我仅从终端或通过创建某种 bash 脚本来循环调用所有文件?我的问题与此类似:使用批处理文件对多个文件执行命令,但这次我在 Linux 命令行终端中工作。

采纳答案by scottyaz

You can try something like this...

你可以试试这样的...

#! /bin/bash
for ((a=022; a <= 658 ; a++))
do
   printf "./filter filename%04d.bmp outputfile lower upper" $a | "sh"
done

回答by Gilles Quenot

You can test that AS-ISin your shell :

你可以AS-IS在你的 shell 中测试:

for i in *; do
    echo "$i" | tr '[:lower:]' '[:upper:]'
done

If you have a special path, change *by your path + a glob : Ex :

如果你有一个特殊的路径,改变*你的路径 + 一个 glob : Ex :

for i in /home/me/*.exe; do ...

See http://mywiki.wooledge.org/glob

http://mywiki.wooledge.org/glob

回答by Eric Fortis

This while prepend the name of the output images like filtered_filename0055.bmp

这在前面加上输出图像的名称,如 filtered_filename0055.bmp

for i in *; do
    ./filter $i filtered_$i lower upper
done

回答by Steve Buzonas

You may be interested in looking into bash scripting.

您可能有兴趣研究 bash 脚本。

You can execute commands in a for loop directly from the shell.

您可以直接从 shell 执行 for 循环中的命令。

A simple loop to generate the numbers you specifically mentioned. For example, from the shell:

一个简单的循环来生成你特别提到的数字。例如,从外壳:

user@machine $ for i in {22..680} ; do
> echo "filename${i}.bmp"
> done

This will give you a list from filename22.bmpto filename680.bmp. That simply handles the iteration of the range you had mentioned. This doesn't cover zero padding numbers. To do this you can use printf. The printfsyntax is printf format argument. We can use the $ivariable from our previous loop as the argument and apply the %Wdformat where W is the width. Prefixing the W placeholder will specify the character to use. Example:

这将为您提供从filename22.bmp到的列表filename680.bmp。这只是处理您提到的范围的迭代。这不包括零填充数字。为此,您可以使用printf. 该printf语法printf format argument。我们可以使用$i之前循环中的变量作为参数并应用%WdW 是宽度的格式。W 占位符的前缀将指定要使用的字符。例子:

user@machine $ for i in {22..680} ; do
> echo "filename$(printf '%04d' $i).bmp"
> done

In the above $()acts as a variable, executing commands to obtain the value opposed to a predefined value.

在上面$()充当变量,执行命令以获取与预定义值相反的值。

This should now give you the filenames you had specified. We can take that and apply it to the actual application:

这现在应该为您提供您指定的文件名。我们可以将其应用到实际应用中:

user@machine $ for i in {22..680} ; do
> ./filter "filename$(printf '%04d' $i).bmp" lower upper
> done

This can be rewritten to form one line:

这可以重写为一行:

user@machine $ for i in {22..680} ; do ./filter "filename$(printf '%04d' $i).bmp" lower upper ; done

One thing to note from the question, .exe files are generally compiled in COFFformat where linux expects an ELFformat executable.

从问题中要注意的一件事是,.exe 文件通常以COFFlinux 期望ELF格式可执行文件的格式编译。

回答by vishal

here is a simple example:

这是一个简单的例子:

for i in {1..100}; do echo "Hello Linux Terminal"; done

to append to a file:(>> is used to append, you can also use > to overwrite)

追加到文件:(>> 用于追加,您也可以使用 > 覆盖)

for i in {1..100}; do echo "Hello Linux Terminal" >> file.txt; done

回答by howardoer

You can leverage xargsfor iterating:

您可以利用xargs迭代:

ls | xargs -i ./filter {} {}_out lower upper

Note:

笔记:

  • {} corresponds to one line output from the pipe, here it's the inputfile name.
  • Output files wouldbe named with postfix '_out'.
  • {} 对应管道的一行输出,这里是输入文件名。
  • 输出文件将使用后缀“_out”命名。