如何将bash变量重定向到可执行文件?
我有一个可执行文件,比如说它叫如何将bash变量重定向到可执行文件?,bash,redirect,Bash,Redirect,我有一个可执行文件,比如说它叫a.out。提示后需要两行输入-- 我可以将输入存储在一个文件(input.txt)中,并将其重定向到a.out,该文件如下所示-- 我可以调用a.out像-- 我正在写一个bash脚本,比如-- exec 5
a.out
。提示后需要两行输入--
我可以将输入存储在一个文件(input.txt)中,并将其重定向到a.out
,该文件如下所示--
我可以调用a.out像--
我正在写一个bash脚本,比如--
exec 5 阅读第1行时,HERESTRING
也可以工作(不需要子shell)。我正要添加一个here字符串,但后来决定只添加一个here文档,因为在here字符串中嵌入换行符需要另一种新语法($”…\n…。
)或者看起来几乎完全像here doc的东西。好的,我在here字符串上打了个洞,因为我意识到进程替换可能也值得一提。-t
将是我的首选选项。复制printf“$line1\n$line2\n”
OP中的内容是糟糕的jujuju——如果这两行中的任何一行都是格式字符串……这可能有助于更具体地描述您的最终尝试不成功的原因。(如果没有其他问题的话,从一般高质量问题的角度来看)使用printf'%s\n'$line1”“$line2'
;这样,如果line1
或line2
中的任何内容读取为格式字符串,就不会引入错误。
> ./a.out
> give me input-1: 0 0 10
> give me input-2: 10 10 5
> this is the output: 20 20 20
0 0 10
10 10 5
> ./a.out < input.txt
> give me input-1: 0 0 10 give me input-2: 10 10 5
> this is the output: 20 20 20
0 0 10
10 10 5
0 0 20
10 10 6
exec 5< input.txt
while read line1 <&5; do
read line2 <&5;
./a.out < `printf "$line1\n$line2"` ;
done
exec 5< input.txt
while read line1 <&5; do
read line2 <&5
printf "%s\n%s\n" "$line1" "$line2" | ./a.out
done
exec 5< input.txt
while read line1 <&5; do
read line2 <&5
./a.out < <(printf "%s\n%s\n" "$line1" "$line2")
done
while read line1; do
read line2
printf "%s\n%s\n" "$line1" "$line2" | ./a.out
done < input.txt
while read line1; do
read line2
./a.out <<EOF
$line1
$line2
EOF
done < input.txt
while read line1; do
read line2
# ./a.out <<< $'$line1\n$line2\n'
./a.out <<<"$line1
$line2"
done < input.txt
# read -t 0 doesn't consume any input; it just exits successfully if there
# is input available.
while read -t 0; do
./a.out
done < input.txt