庆典:调用带有双引号参数的脚本 [英] bash: calling a scripts with double-quote argument

查看:100
本文介绍了庆典:调用带有双引号参数的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个参数封闭双引号一个bash脚本,给定的边界值范围内创建地图的形状文件,例如

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.

为了节省时间,我想有一个脚本,将创造8的地图 - 对编码(4个选项)和 NAV_SELECT (2个选项)。

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).

我已经写了另外一个bash脚本, create_map ,以服务器作为包装:

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

*的这工作(感谢,布莱恩!),但我不能找到一种方法有数值参数0 0 100 100正在从外部脚本之外过去了。 *

*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. *

基本上,我正在寻找方法来接受一个包装bash脚本双引号中的参数,并把它传递 - 用双引号 - 到内部脚本

说明:

export_map 是主脚本,正在从 create_map 所谓的8倍。

export_map is the main script, being called from create_map 8 times.

任何想法?

谢谢,

亚当

推荐答案

如果我正确地理解你的问题(这我不知道,见我的意见),你应该添加其他 \\ n 你的的printf ; 的printf 默认不添加尾随的换行符的方式,回声一样。这将确保第二个值将被正确读取选择哪些我假设在 export_map.sh 命令>。

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\""

另外,我不认为你需要添加-c /斌/ bash和引号。下面应该足够了,除非我失去了一些东西:

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"

修改感谢您的澄清。为了从您的包装脚本传递参数,进入内部脚本,保持它作为一个参数,你可以通过$ 1,这里的引号表明您想保持这种归纳为一个参数,而 $ 1 是你的包装脚本的第一个参数。如果你想传递到你内心的脚本从外部脚本的所有参数,每个被保留作为一个参数,你可以使用$ @代替。

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

下面是一个如何$ @和一个简单的例子。首先, inner.bash

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 "$@"

和调用它:

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

这篇关于庆典:调用带有双引号参数的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆