bash:使用双引号参数调用脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1994928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash: calling a scripts with double-quote argument
提问by Adam Matan
I have a bash scripts which an argument enclosed with double quotes, which creates a shape-file of map within the given boundries, e.g.
我有一个 bash 脚本,它的参数用双引号括起来,它在给定的边界内创建了一个地图形状文件,例如
$ export_map "0 0 100 100"
Within the script, there are two selectstatements:
在脚本中,有两个select语句:
select ENCODING in UTF8 WIN1252 WIN1255 ISO-8859-8;
...
select NAV_SELECT in Included Excluded;
Naturally, these two statements require the input to enter a number as an input. This can by bypassed by piping the numbers, followed by a newline, to the script.
自然而然,这两个语句需要输入一个数字作为输入。这可以通过将数字和换行符通过管道传输到脚本来绕过。
In order to save time, I would like to have a script that would create 8 maps - for each combination of ENCODING(4 options) and NAV_SELECT(2 options).
为了节省时间,我想要一个脚本来创建 8 个地图 - 对于ENCODING(4 个选项)和NAV_SELECT(2 个选项)的每个组合。
I have written another bash script, create_map, to server as a wrapper:
我已经编写了另一个 bash 脚本,create_map作为包装器到服务器:
#!/bin/bash
for nav in 1 2 3 4;
do
for enc in 1 2;
do
printf "$nav\n$enc\n" | /bin/bash -c "./export_map.sh \"0 0 100 100\""
done
done
**This works (thanks, Brian!), but I can't find a way to have the numeric argument "0 0 100 100"being passed from outside the outer script. **
**这有效(谢谢,布赖恩!),但我找不到"0 0 100 100"从外部脚本外部传递数字参数的方法。**
Basically, I'm looking for way to accept an argument within double quotes to a wrapper bash script, and pass it - with the double quotes - to an inner script.
基本上,我正在寻找将双引号内的参数接受到包装器 bash 脚本的方法,并将它(带双引号)传递给内部脚本。
CLARIFICATIONS:
说明:
export_mapis the main script, being called from create_map8 times.
export_map是主脚本,被调用create_map8 次。
Any ideas?
有任何想法吗?
Thanks,
谢谢,
Adam
亚当
回答by Brian Campbell
If I understand your problem correctly (which I'm not sure about; see my comment), you should probably add another \nto your printf; printfdoes not add a trailing newline by default the way that echodoes. This will ensure that the second value will be read properly by the selectcommand which I'm assuming appears in export_map.sh.
如果我正确理解您的问题(我不确定;请参阅我的评论),您可能应该\n在printf; printf默认情况下不会像那样添加尾随换行符echo。这将确保第二个值将被select我假设出现在export_map.sh.
printf "$nav\n$enc\n" | /bin/bash -c "./export_map.sh \"100 200 300 400\""
Also, I don't think that you need to add the /bin/bash -cand quote marks. The following should be sufficient, unless I'm missing something:
另外,我认为您不需要添加/bin/bash -c和引号。以下应该足够了,除非我遗漏了什么:
printf "$nav\n$enc\n" | ./export_map.sh "100 200 300 400"
editThanks for the clarification. In order to pass an argument from your wrapper script, into the inner script, keeping it as a single argument, you can pass in "$1", where the quotes indicate that you want to keep this grouped as one argument, and $1is the first parameter to your wrapper script. If you want to pass all parameters from your outer script in to your inner script, each being kept as a single parameter, you can use "$@"instead.
编辑感谢您的澄清。为了将包装脚本中的参数传递到内部脚本中,并将其作为单个参数,您可以传入"$1",其中引号表示您希望将此分组为一个参数,并且$1是您的第一个参数包装脚本。如果要将外部脚本中的所有参数传递到内部脚本中,每个参数都保留为单个参数,则可以"$@"改用。
#!/bin/bash
for nav in 1 2 3 4;
do
for enc in 1 2;
do
printf "$nav\n$enc\n" | ./export_map.sh ""
done
done
Here's a quick example of how "$@"works. First, inner.bash:
这是"$@"工作原理的快速示例。首先,inner.bash:
#!/bin/bash
for str in "$@"
do
echo $str
done
outer.bash:
outer.bash:
#!/bin/bash
./inner.bash "$@"
And invoking it:
并调用它:
$ ./outer.bash "foo bar" baz "quux zot"
foo bar
baz
quux zot

