从 bash 将输入发送到正在运行的 shell 脚本

发布于 2024-11-03 23:58:18 字数 827 浏览 2 评论 0原文

我正在为我的应用程序编写一个测试套件,并使用 bash 脚本来检查测试套件输出是否与预期输出匹配。以下是脚本的一部分:

for filename in test/*.bcs ;
do
    ./BCSC $filename > /dev/null
    NUMBER=`echo "$filename" | awk -F"[./]" '{print $2}'`
    gcc -g -m32 -mstackrealign runtime.c $filename.s -o test/e$NUMBER
    # run the file and diff against expected output
    echo "Running test file... "$filename
    test/e$NUMBER > test/e$NUMBER.out
    if [ $NUMBER = "4" ]
    then
        # it's trying to read the line
        # Pass some input to the file...
    fi
    diff test/e$NUMBER.out test/o$NUMBER.out
done

测试 #4 测试从 stdin 读取输入。我想测试脚本 #4,如果是的话,向它传递一组示例输入。

那样做

test/e4 < test/e4.in > test/e4.out

我刚刚意识到你可以像e4.in 具有示例输入的地方 。是否有另一种方法将输入传递给正在运行的脚本?

I'm writing a test suite for my app and using a bash script to check that the test suite output matches the expected output. Here is a section of the script:

for filename in test/*.bcs ;
do
    ./BCSC $filename > /dev/null
    NUMBER=`echo "$filename" | awk -F"[./]" '{print $2}'`
    gcc -g -m32 -mstackrealign runtime.c $filename.s -o test/e$NUMBER
    # run the file and diff against expected output
    echo "Running test file... "$filename
    test/e$NUMBER > test/e$NUMBER.out
    if [ $NUMBER = "4" ]
    then
        # it's trying to read the line
        # Pass some input to the file...
    fi
    diff test/e$NUMBER.out test/o$NUMBER.out
done

Test #4 tests reading input from stdin. I'd like to test for script #4, and if so pass it a set of sample inputs.

I just realized you could do it like

test/e4 < test/e4.in > test/e4.out

where e4.in has the sample inputs. Is there another way to pass input to a running script?

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

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

发布评论

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

评论(3

羁〃客ぐ 2024-11-10 23:58:18

如果您想直接在脚本中提供输入数据,请使用此处文档:

    test/e$NUMBER > test/e$NUMBER.out
    if [ $NUMBER = "4" ]; then
    then
        test/e$NUMBER > test/e$NUMBER.out <<END_DATA
test input goes here
you can supply as many lines of input as you want
END_DATA
    else
        test/e$NUMBER > test/e$NUMBER.out
    fi

有多种变体:如果您引用分隔符(即 <<'END_DATA'),则不会'不要在此处文档中执行诸如替换 $variable 之类的操作。如果您使用 <<-DELIMITER,它将从每行输入中删除前导制表符(以便您可以缩进输入以匹配周围的代码)。有关详细信息,请参阅 bash 手册页中的“此处文档”部分。

If you want to supply the input data directly in the script, use a here-document:

    test/e$NUMBER > test/e$NUMBER.out
    if [ $NUMBER = "4" ]; then
    then
        test/e$NUMBER > test/e$NUMBER.out <<END_DATA
test input goes here
you can supply as many lines of input as you want
END_DATA
    else
        test/e$NUMBER > test/e$NUMBER.out
    fi

There are several variants: if you quote the delimiter (i.e. <<'END_DATA'), it won't do things like replace $variable replacement in the here-document. If you use <<-DELIMITER, it'll remove leading tab characters from each line of input (so you can indent the input to match the surrounding code). See the "Here Documents" section in the bash man page for details.

杀お生予夺 2024-11-10 23:58:18

您提到的方式是发出命令/脚本时将文件重定向到标准输入的传统方法。

如果您详细说明您正在寻找的“其他方式”,也许会有所帮助,例如,为什么您甚至需要一种不同的方式来做到这一点?您是否需要执行此方法不允许执行的任何操作?

The way you mentioned is the conventional method to redirect a file into stdin when issuing a command/script.

Maybe it'll help if you'll elaborate on the "other way" you're looking for, as in, why do you even need a different way to do so? Is there anything you need to do which this method does not allow?

情绪少女 2024-11-10 23:58:18

你可以这样做:

cat test/e4.in | test/e4 > test/e4.out

You can do:

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