#!/bin/bash OUTFILE=somefile.txt ( ./runnable input << EOF 2 4 EOF ) > $OUTFILE
但后来我需要改变论据。 目录中有100个文件,每个文件有两个数字。 这两个数字需要反馈给上面的脚本。 100个文件有这样的数据:
2,4 9,2 4,2
所以我改变了脚本以下,但这似乎并没有工作。
#!/bin/bash OUTFILE=file3.txt ( while read line do ./runnable input << EOF #following gets first number before comma test=$(echo `expr index "$line" ,` ) echo ${line:0:($test-1)} line=$(echo ${line:$test}) #following gets second number after comma test=$(echo `expr index "$line" ,` ) echo ${line:0:($test-1)} line=$(echo ${line:$test}) EOF done < file_with_values ) > $OUTFILE
我能做什么错?
您不能在内联数据块(<< EOF)内编写脚本
尝试这样的事情:
OUTFILE=file3.txt while IFS=, read ab; do echo "first number: $a" echo "second number: $b" # you can call runnable here, for example if you want to give it one number at a time: echo "$a" | ./runnable input echo "$b" | ./runnable input # or something like this: ./runnable "$a" "$b" done >$OUTFILE
尝试通过从你读的行之外创build一个数组来简单地分割行,就像这样(其中your_line是从文件中读取的行)。
OIFS=${IFS} IFS=',' your_line=(${your_line}) IFS=${OIFS}
现在$ {your_line [0]}是第一个数字,$ {your_line [1]}是第二个数字。 你可以回显他们,添加他们,不pipe。 这可能会正常工作。
整个剧本将成为一些沿线的东西:
#!/bin/bash cat FILE_WITH_VALUES | while read your_line; do OIFS=${IFS} IFS=',' your_line=(${your_line}) IFS=${OIFS} echo ${your_line[0]} ${your_line[1]} | ./runnable_input >> OUTPUT_FILE done
缺点是这将启动100次(每行一次)的runnable_input。 在一个相当快的机器上,100个这样的小input运行并不重要,除非runnable_input取决于一次获得100行。
将你的100个文件预处理成1个数据文件,然后处理
#!/usr/bin/bash OUTFILE=xyzzy ( while read line do ./runable $line done <datafile )>$OUTFILE
试试这个脚本,
#!/bin/sh IFS=" " for i in $( sed 's|,| |g' file_with_values ); do ./runnable $i done > file3.txt