I have the following code, which is intended to run a java program on some input, and test that input against a results file for verification.
#!/bin/bash
java Program ../tests/test"$@".tst > test"$@".asm
spim -f test"$@".asm > temp
diff temp ../results/test"$@".out
The gist of the above code is to:
- Run Program on a test file in another directory, and pipe the output into an assembly file.
- Run a MIPS processor on that program's output, piping that into a file called temp.
- Run diff on the output I generated and some expected output.
I made this shell script to help me automate checking of my homework assignment for class. I didn't feel like manually checking things an开发者_如何学Goymore.
I must be doing something wrong, as although this program works with one argument, it fails with more than one. The output I get if I use the $@ is:
./test.sh: line 2: test"$@".asm: ambiguous redirect
Cannot open file: `test0'
EDIT:
Ah, I figured it out. This code fixed the problem:
#!/bin/bash
for arg in $@
do
java Parser ../tests/test"$arg".tst > test"$arg".asm
spim -f test"$arg".asm > temp
diff temp ../results/test"$arg".out
done
It turns out that bash must have interpreted a different cmd arg for each time I was invoking $@.
enter code here
If you provide multiple command-line arguments, then clearly $@
will expand to a list of multiple arguments, which means that all your commands will be nonsense.
What do you expect to happen for multiple arguments?
精彩评论