bash:使用双引号参数调用脚本
我有一个 bash 脚本,其参数用双引号引起来,它在给定边界内创建地图的形状文件,例如
$ export_map "0 0 100 100"
在脚本内,有两个 select
语句:
select ENCODING in UTF8 WIN1252 WIN1255 ISO-8859-8;
...
select NAV_SELECT in Included Excluded;
当然,这两个语句需要input 输入一个数字作为输入。这可以通过将数字(后跟换行符)通过管道传输到脚本来绕过。
为了节省时间,我希望有一个脚本可以为 ENCODING
(4 个选项)和 NAV_SELECT
(2 个选项)的每种组合创建 8 个地图。
我编写了另一个 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
**这有效(谢谢,Brian!),但我找不到从外部脚本外部传递数字参数 "0 0 100 100"
的方法。 **
基本上,我正在寻找一种方法,将双引号内的参数接受到包装器 bash 脚本,并将其(带有双引号)传递给内部脚本。
澄清:< /strong>
export_map
是主脚本,被 create_map
调用 8 次。
有什么想法吗?
谢谢,
亚当
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.
$ export_map "0 0 100 100"
Within the script, there are two select
statements:
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).
I have written another bash script, create_map
, to server as a wrapper:
#!/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. **
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.
CLARIFICATIONS:
export_map
is the main script, being called from create_map
8 times.
Any ideas?
Thanks,
Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题(我不确定;请参阅我的评论),你可能应该在你的
printf
中添加另一个\n
;默认情况下,printf
不会像echo
那样添加尾随换行符。这将确保我假设出现在export_map.sh
中的select
命令可以正确读取第二个值。另外,我认为您不需要添加
/bin/bash -c
和引号。除非我遗漏了某些内容,否则以下内容应该足够了:编辑感谢您的澄清。为了将参数从包装器脚本传递到内部脚本中,并将其保留为单个参数,您可以传入
"$1"
,其中引号表示您希望将其分组为一个参数,$1
是包装器脚本的第一个参数。如果您想将外部脚本中的所有参数传递到内部脚本中,并将每个参数保留为单个参数,则可以使用"$@"
代替。以下是
"$@"
工作原理的简单示例。首先,inner.bash
:outer.bash
:并调用它:
If I understand your problem correctly (which I'm not sure about; see my comment), you should probably add another
\n
to yourprintf
;printf
does not add a trailing newline by default the way thatecho
does. This will ensure that the second value will be read properly by theselect
command which I'm assuming appears inexport_map.sh
.Also, I don't think that you need to add the
/bin/bash -c
and quote marks. The following should be sufficient, unless I'm missing something:edit Thanks 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$1
is 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.Here's a quick example of how
"$@"
works. First,inner.bash
:outer.bash
:And invoking it: