Linux Bash中的一行命令

问题描述 投票:0回答:3

我正在尝试编写一个基本的单行Linux Bash命令,它将所有数字介于1 - 1000之间,作为exe程序的输入。

exe程序看起来像这样:

please insert 1:   1(wanted input)
please insert 2:   2(wanted input)
.
.
.
.
please insert 1000:  1000(wanted input)
success!

所以我试过写这个linux bash命令:

for((i=1;i<=1000;i+=1)); do echo "$i"|./the_exe_file; done

但问题是我的命令在for的每个迭代上打开exe文件...这意味着只有第一个输入(1)是正确的。并且,由于某种原因,给exe文件的输入似乎不太好。我能做什么?我的错误在哪里?

提前致谢。

linux bash command exe
3个回答
1
投票

您要求在每次循环迭代中打开exe。它你只需要打开一次,把它带出循环:

for((i=1;i<=1000;i+=1)); do echo "$i"; done | ./the_exe_file

1
投票

同样,您可能会发现使用专为此设计的工具更具可读性。

seq 1 1000 | ./the_exe_file

1
投票

尝试

printf '%s\n' {1..1000} | ./the_exe_file

0
投票

在bash中:

$ for f in {1..1000}; do echo $f; done 

去测试:

$ for f in {1..1000}; do echo $f; done  | uniq | wc -l
1000
© www.soinside.com 2019 - 2024. All rights reserved.