I had the following bash script which was working fine.
#!/bin/bash
OUTFILE=somefile.txt
(
./runnable input << EOF
2
4
EOF
) > $OUTFILE
but later i had the need of changing arguments. there are 100 files in a directory, each file has two numbers. these two numbers need to be fed to the above script. 100 files have data in them like this:
2,4
9,2
4,2
so i changed the script to the following but this does not seem to work.
#!/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
what can i be doing wrong?
$()
so you don't need backticks at all. But you don't need to useexpr
or the line it's in or the following two lines. To get the part before the comma:echo "${line%,*}"
and the part after the comma:echo "${line#*,}"
but as Balázs Pozsár said, you can't have commands inside a here doc.