模拟用户输入以使用不同参数多次调用脚本

发布于 2024-09-08 19:17:06 字数 667 浏览 11 评论 0原文

我必须使用提供的脚本,该脚本在脚本运行时接受用户输入而不是参数。我无法解决这个问题。

脚本的一个例子是:

#!/bin/bash

echo "param one"
read one
doSomething

echo "param two"
read two
doSomething

echo "param three"
read three
doSomething

echo "param four"
read four
doSomething

echo "param five"
read five
doSomething

我想要一种能够调用此脚本并提供参数化输入的方法,例如:

./scriptNameWrapper.ksh 1 22 333 4444 55555

我尝试过谷歌搜索,要么我没有正确地提出问题,要么更有可能我可以'只见树木,不见森林。

我已经尝试过这个,但不起作用:

#!/bin/bash

./scriptName.ksh
<<$1
<<$2
<<$3
<<$4
<<$5

我显然不是 *nix 专家,但我确信我以前见过这样做,我只是找不到任何例子。这开始变得令人沮丧,任何帮助将不胜感激。

I have to use a provided script that takes user input while the script is running instead of parameters. I can't get around this.

An example of script would be:

#!/bin/bash

echo "param one"
read one
doSomething

echo "param two"
read two
doSomething

echo "param three"
read three
doSomething

echo "param four"
read four
doSomething

echo "param five"
read five
doSomething

I would like a way to be able to call this script and provide parameterized input, something like:

./scriptNameWrapper.ksh 1 22 333 4444 55555

I've tried googling, and either I'm not asking the question correctly, or more likely I can't see the wood for the trees.

I've tried this, which doesn't work:

#!/bin/bash

./scriptName.ksh
<<$1
<<$2
<<$3
<<$4
<<$5

I'm clearly not a *nix expert, but I'm sure I've seen this done before, I just can't find any examples out there. This is beginning to get frustrating, and any help would be greatly appreciated.

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

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

发布评论

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

评论(3

黎夕旧梦 2024-09-15 19:17:06

根据您的尝试,您可能看到的内容称为 here-document

它应该看起来像这样:

#!/bin/bash

./scriptName.ksh <<-END_PARAMS
    $1
    $2
    $3
    $4
    $5
END_PARAMS

What you've probably seen, based on your attempt, is called a here-document.

It should look like this:

#!/bin/bash

./scriptName.ksh <<-END_PARAMS
    $1
    $2
    $3
    $4
    $5
END_PARAMS
烏雲後面有陽光 2024-09-15 19:17:06

像这样调用您的脚本:

echo -e "Param1\nParam2\nParam3" | ./scriptName.ksh

每个参数之间的 \n 序列模拟按下的 Enter 键。

Call your script like this:

echo -e "Param1\nParam2\nParam3" | ./scriptName.ksh

The \n sequence between each parameter emulates the enter key being pressed.

一人独醉 2024-09-15 19:17:06

将参数放入文件中,每行一个,然后运行

./scriptName.ksh <filename

Put your paramaters in a file one per line then run

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