简单的 Unix 重定向问题:将文件中的行作为命令行参数传递给脚本

发布于 2024-11-30 13:40:23 字数 638 浏览 1 评论 0原文

好吧,我的 Unix 脚本技能显然很生疏。我想要做的就是拥有一个包含 4 个参数的文件,我希望将它们传递给脚本,就像它们来自命令行一样。但奇怪的是这样做:

./myscript.sh < mycmds.txt

似乎没有按我预期的方式工作。 myscript.sh 的内容是:

cat >> out.txt <<EOF
$1 $2 $3 $4
EOF

因此,如果我从命令行运行 myscript.sh,如下所示: ./myscript.sh test1 test2 test3 test4 一切正常,我看到 test1 test2 test3 test4 显示在 out.txt 文件中。但是,如果我将 test1 test2 test3 test4 作为一行放在名为 mycmds.txt 的文件中,然后运行 ​​./mysript.sh ./mysript.sh < mycmds.txt 我只是在 out.txt 文件中得到一个空行。

那么我在这里做错了什么?将参数存储在文件中并将它们传递给脚本以便将它们视为来自命令行的正确方法是什么?

Ok, my Unix scripting skills are obviously really rusty. All I want to do is have a file with 4 arguments that I want passed to a script as if they came from the command line. But strangely doing this:

./myscript.sh < mycmds.txt

Doesn't seem to be working the way I expect. Contents of myscript.sh are:

cat >> out.txt <<EOF
$1 $2 $3 $4
EOF

So if I run myscript.sh from the command line like this: ./myscript.sh test1 test2 test3 test4 everything works great and I see test1 test2 test3 test4 show up in the out.txt file. But if I put test1 test2 test3 test4 as a line in a file called mycmds.txt and then run ./mysript.sh < mycmds.txt I just get an empty line in out.txt file.

So what am I doing wrong here? What is the proper way to store arguments in a file and pass them to a script so that they will be treated just as if they came from the command line?

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

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

发布评论

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

评论(3

绮筵 2024-12-07 13:40:23

要使 mycmds.txt 的内容可用作命令行参数,只需在调用 myscript.sh 时内联内容即可:

./myscript.sh $(< mycmds.txt)

To make the content of mycmds.txt available as command line parameters, just inline the content when you call myscript.sh:

./myscript.sh $(< mycmds.txt)
雨落星ぅ辰 2024-12-07 13:40:23

这是因为 < 是输入重定向,并将该行作为标准输入发送到进程,与命令行参数完全无关。

您可以执行以下操作:

./myscript.sh $(cat mycmds.txt)

$(xyz) 构造运行 xyz 然后使用其标准输出来构造命令的该部分。例如:

ls $(expr 1 + 3)

将尝试为您提供名为 4 的文件的目录列表,如下所示。

首先执行命令expr 1 + 3,输出为4。然后将其替换为外部命令,为您提供 ls 4 并执行。

That's because < is input redirection and sends that line in as standard input to the process, nothing at all to do with command line arguments.

You can do something like:

./myscript.sh $(cat mycmds.txt)

The $(xyz) construct runs xyz then uses its standard output to construct that part of the command. For example:

ls $(expr 1 + 3)

will attempt to give you a directoy listing for the file called 4, as follows.

First the command expr 1 + 3 is executed and the output is 4. This is then substituted in to the outer command to give you ls 4 and that is executed.

﹏雨一样淡蓝的深情 2024-12-07 13:40:23

您错过的微妙之处在于标准输入和位置参数不是同一件事。

标准输入,您可以使用“<”重定向,甚至可以使用“|”从另一个程序重定向是一个字节序列。脚本可以使用 read 命令读取 stdin。

位置参数编号为 1 到 N,保存每个参数的值。 shell 将它们称为 $1 到 ${42} (如果您给出了这么多)。

标准输入和位置参数是彼此独立的。您可以两者都拥有,或者都拥有,或者都不拥有,具体取决于您如何调用程序(以及该程序期望什么):

  1. 两者:grep -E pattern grep -E文件
  2. 只是标准输入:wc < file
  3. 只是位置参数:echo 这是五个位置参数
  4. 两者都不是:ls

The subtlety you miss is that the standard input and positional parameters are not the same thing.

Standard input, which you can redirect with the '<', or even from another program with '|' is a sequence of bytes. A script can read stdin with, well, the read command.

Positional parameters are numbered 1 to N and hold the value of each argument. The shell refers to them as $1 to ${42} (if you gave that many).

Standard input and positional parameters are indepent of one another. You can have both, either, or none, depending how you call a program (and what that program expects):

  1. Both: grep -E pattern < file
  2. Just stdin: wc < file
  3. Just positional paramters: echo Here are five positional parameters
  4. Neither: ls
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文