简体   繁体   中英

Help with aliases in shell scripts

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:

  1. Run Program on a test file in another directory, and pipe the output into an assembly file.
  2. Run a MIPS processor on that program's output, piping that into a file called temp.
  3. 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 anymore.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM