简单的 Unix 重定向问题:将文件中的行作为命令行参数传递给脚本
好吧,我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使
mycmds.txt
的内容可用作命令行参数,只需在调用myscript.sh
时内联内容即可:To make the content of
mycmds.txt
available as command line parameters, just inline the content when you callmyscript.sh
:这是因为
<
是输入重定向,并将该行作为标准输入发送到进程,与命令行参数完全无关。您可以执行以下操作:
$(xyz)
构造运行xyz
然后使用其标准输出来构造命令的该部分。例如:将尝试为您提供名为
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:
The
$(xyz)
construct runsxyz
then uses its standard output to construct that part of the command. For example: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 is4
. This is then substituted in to the outer command to give youls 4
and that is executed.您错过的微妙之处在于标准输入和位置参数不是同一件事。
标准输入,您可以使用“<”重定向,甚至可以使用“|”从另一个程序重定向是一个字节序列。脚本可以使用 read 命令读取 stdin。
位置参数编号为 1 到 N,保存每个参数的值。 shell 将它们称为 $1 到 ${42} (如果您给出了这么多)。
标准输入和位置参数是彼此独立的。您可以两者都拥有,或者都拥有,或者都不拥有,具体取决于您如何调用程序(以及该程序期望什么):
grep -E pattern
grep -E文件
wc < file
echo 这是五个位置参数
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):
grep -E pattern < file
wc < file
echo Here are five positional parameters
ls