bash:使用双引号参数调用脚本

发布于 2024-08-16 19:55:54 字数 1062 浏览 1 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

一枫情书 2024-08-23 19:55:54

如果我正确理解你的问题(我不确定;请参阅我的评论),你可能应该在你的 printf 中添加另一个 \n ;默认情况下,printf 不会像 echo 那样添加尾随换行符。这将确保我假设出现在 export_map.sh 中的 select 命令可以正确读取第二个值。

printf "$nav\n$enc\n" | /bin/bash -c "./export_map.sh \"100 200 300 400\""

另外,我认为您不需要添加 /bin/bash -c 和引号。除非我遗漏了某些内容,否则以下内容应该足够了:

printf "$nav\n$enc\n" | ./export_map.sh "100 200 300 400"

编辑感谢您的澄清。为了将参数从包装器脚本传递到内部脚本中,并将其保留为单个参数,您可以传入 "$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 "$1"
    done
done

以下是 "$@" 工作原理的简单示例。首先,inner.bash

#!/bin/bash

for str in "$@"
do
    echo $str
done

outer.bash

#!/bin/bash

./inner.bash "$@"

并调用它:

$ ./outer.bash "foo bar" baz "quux zot"
foo bar
baz
quux zot

If I understand your problem correctly (which I'm not sure about; see my comment), you should probably add another \n to your printf; printf does not add a trailing newline by default the way that echo does. This will ensure that the second value will be read properly by the select command which I'm assuming appears in 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 -c and quote marks. The following should be sufficient, unless I'm missing something:

printf "$nav\n$enc\n" | ./export_map.sh "100 200 300 400"

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.

#!/bin/bash

for nav in 1 2 3 4;
do
    for enc in 1 2;
    do
        printf "$nav\n$enc\n" | ./export_map.sh "$1"
    done
done

Here's a quick example of how "$@" works. First, inner.bash:

#!/bin/bash

for str in "$@"
do
    echo $str
done

outer.bash:

#!/bin/bash

./inner.bash "$@"

And invoking it:

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