Bash: call scripts with double quote argument

I have bash scripts in which the argument is enclosed in double quotes, which creates a map form file within the given borders, e.g.

$ export_map "0 0 100 100" 

There are two select statements inside the script:

 select ENCODING in UTF8 WIN1252 WIN1255 ISO-8859-8; ... select NAV_SELECT in Included Excluded; 

Naturally, these two operators require entering a number input as input. This can be circumvented by skipping numbers and then a new line in the script.

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 wrote another bash script, create_map , for the server as a shell:

 #!/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 pass the numeric argument "0 0 100 100" from the outside to an external script. **

Basically, I’m looking for a way to take an argument in double quotes to the bash script wrapper and pass it - with double quotes - to the internal script.

EXPLANATIONS:

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

Any ideas?

Thanks,

Adam

0
source share
1 answer

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, as echo does. This ensures that the second value is correctly read by the select command, which I assume will appear in export_map.sh .

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

Also, I don't think you need to add the /bin/bash -c and quote tags. The following is enough: if I am missing something:

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

edit Thanks for the clarification. To pass an argument from your script shell to an internal script, saving it as a single argument, you can pass "$1" where the quotation marks indicate that you want to keep this group as a single argument, and $1 is the first parameter in your script wrapper. If you want to transfer all parameters from an external script to your internal script, each of which is stored as one 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 is a brief example of how " $@ " works. First, inner.bash :

 #!/bin/bash for str in " $@ " do echo $str done 

outer.bash :

 #!/bin/bash ./inner.bash " $@ " 

And calling him:

 $ ./outer.bash "foo bar" baz "quux zot" foo bar baz quux zot 
+6
source

Source: https://habr.com/ru/post/893136/


All Articles