帮助处理 shell 脚本中的别名

发布于 2024-11-09 01:15:47 字数 913 浏览 0 评论 0原文

我有以下代码,旨在对某些输入运行 java 程序,并针对结果文件测试该输入以进行验证。

#!/bin/bash
java Program ../tests/test"$@".tst > test"$@".asm
spim -f test"$@".asm > temp
diff temp ../results/test"$@".out

上面代码的要点是:

  1. 在另一个目录中的测试文件上运行程序,并将输出通过管道传输到汇编文件中。
  2. 在该程序的输出上运行 MIPS 处理器,将其传输到名为 temp 的文件中。
  3. 对我生成的输出和一些预期输出运行 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:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-11-16 01:15:47

如果您提供多个命令行参数,那么显然 $@ 将扩展为多个参数的列表,这意味着您的所有命令都将是无意义的。

对于多个参数,您预计会发生什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文