I am trying to learn bashing. I am trying to run ./test testcase the file in test case has the arguments 9 11 22 13, 32 35 32 16 on the next line and so on My program takes 4 arguments. Right now if testcase has one line of arguments i.e 3 5 6 7 it works fine, but when there is more than 2 it can't run the program properly. I know I need a while loop to read each line of the file, but I am stuck here. If someone could help me it would be greatly appreciated. Thank you in advance. I've asked this question here before, I don't know if everyone is too busy or don't know how. Thanks again
your_path=../test/test
test_path=../../public/test
file_input="$1"
#while read -r line; do
#done < "$file_input"
# Read contents in the file
contents=$(< "$file_input")
# Display output of the test file
"$test_path" $contents > correctanswer 2>&1
# Display output of your file
"$your_path" $contents > youranswer 2>&1
diff the solutions
diff correctanswer youranswer > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "The two outputs were exactly the same "
else
echo "$divider"
echo "The two outputs were different "
diff youranswer correctanswer
echo "Do you wish to see the ouputs side-by-side?"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "LEFT: Your Output RIGHT: Solution Output"
sleep 1
vimdiff youranswer correctanswer; break;;
No ) exit;;
esac
done
fi
I think you can do it like this:
Modified so that the
read
command reads on file descriptor 3, leaving standard input available for theselect
, etc.I don't know what you're doing that makes it fail. I created two programs,
test1
andtest2
. Initially, they were identical:Then I changed
test2
so it contained:With this data in
input.file
:I ran the script
botched.sh
:The outputs were recognized as identical, as you'd expect. Then with the modified
test2
, I reran it:The
vimdiff
program did run and show the differences.Note the basic debugging technique — running with
bash -x
. It shows that the read is taking a single line of input with four numbers, and then those are passed to the surrogate programstest1
andtest2
; then it takes a second line, and passes those to the surrogates programs.Actual test code:
Tested with Bash 3.2.51 on Mac OS X 10.9.2.
If it doesn't work for you, then you need to set up surrogate test programs, similar to mine, and show what you get from running
bash -x
on your script. You should also identify the platform you're running on and the version of Bash you are running.I would normally use a construct similar to the following (applying to your example). Note that the only change your need to make is the
cat $file | (while read l; do
and thendone)
that closes the cycle. The body of the cycle is exactly your code with exception thatcontents
is replaced withl
(line read). Parenthesis around the cycle actually launch another instance of the shell where while is executed and itsstdin
is set to be fed fromcat
.