帮助处理 shell 脚本中的别名
我有以下代码,旨在对某些输入运行 java 程序,并针对结果文件测试该输入以进行验证。
#!/bin/bash
java Program ../tests/test"$@".tst > test"$@".asm
spim -f test"$@".asm > temp
diff temp ../results/test"$@".out
上面代码的要点是:
- 在另一个目录中的测试文件上运行程序,并将输出通过管道传输到汇编文件中。
- 在该程序的输出上运行 MIPS 处理器,将其传输到名为 temp 的文件中。
- 对我生成的输出和一些预期输出运行 diff。
我制作了这个 shell 脚本来帮助我自动检查我的课堂作业。我不想再手动检查事情了。
我肯定做错了什么,因为虽然这个程序只处理一个参数,但处理多个参数时就会失败。如果我使用 $@,我得到的输出是:
./test.sh: line 2: test"$@".asm: ambiguous redirect
Cannot open file: `test0'
编辑:
啊,我想出来了。这段代码解决了这个问题:
#!/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
事实证明,每次我调用 $@ 时,bash 都必须解释不同的 cmd arg。
enter code here
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您提供多个命令行参数,那么显然
$@
将扩展为多个参数的列表,这意味着您的所有命令都将是无意义的。对于多个参数,您预计会发生什么?
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?