Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

bash - how to read a file line by line and each line as an argument input to a .exe file and capture the output to another file in linux

I try to write a shell script that reads a .txt file line by line, and each line will as the input to my .exe file and I also want to capture the output of the .exe file and export it to another .txt file. my code likes that, but it doesn't work. when I try input manually like "./caculate.exe "1" the program also do not take 1 as an input, still ask me to input manually again.

#!/bin/bash
while IFS= read -r LINE; do
  ./caculate.exe "$LINE"
done < data.txt > f.txt
question from:https://stackoverflow.com/questions/66048688/how-to-read-a-file-line-by-line-and-each-line-as-an-argument-input-to-a-exe-fil

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If calculate.exe normally gets its input from standard input, you need to pipe the variable to it, not use an argument.

#!/bin/bash
while IFS= read -r LINE
do
    printf "%s
" "$line" | ./calculate.exe
done < data.txt > f.txt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...