在程序c中输入多个文件的Shell脚本



我有一个程序,它等待'get',然后等待多个文件输入(在同一行或一个接一个)。然后使用'quit'结束输入。我的文件位于"test_files/"在另一个目录中。我想要的是脚本从我的文件夹输入每个文件到我的程序。

现在我有:

{ echo "get"; echo "file1.txt file2.txt ... fileN.txt quit"; } | ./program

但是我想要的是这样的:

echo "get" | ./program
for N in {files in folder}
do
echo "fileN.txt" | ./program
done
echo "quit" | ./program

应该可以:

#!/bin/bash
for i in `ls file*.txt`; do
echo $(basename $i) | ./program
done

首先您需要确保程序是可执行的然后,在每次执行中等待文件被处理,这可以用等美元!(虽然我想我可以让它等待./program执行结束)你也可以给一个暂停,比如半秒slee0 0.5

# before
chmod +x program
program get
for N in $(ls files*)
do
./program file$N
wait $!
# or
#sleep 0.5
done
./program quit
echo "END"

如果您需要运行您的程序,然后为它提供get,这是一个文件名列表,以quit结尾,您需要从shell脚本生成所有这些:

#!/bin/sh
folder=/some/folder/in/your/filesystem
# all the programs executed inside the parenthesis run in a
# subshell, so the whole output of this subshell is
# effectively redirected to the pipe to the program.
# the attempts shown in your attempt and the proposed solutions
# just execute the program several times and pass to it only one
# of the commands, not all in sequence.
( echo get    
# for all files in folder
for file in "$folder"/*
do 
echo "$file"
done
echo quit ) | ./program

如果您需要简单地连接文件并将它们提供给程序的stdin,只需使用:

cat *.txt | ./program
# or
cat file1.txt file2.txt ... fileN.txt | ./program

相关内容

  • 没有找到相关文章

最新更新