bash pass multiple arguments with spaces
Hi,
I have a c program that takes multiple arguments (filenames). You can pass in as many arguments as you want. You can run it like, ./myCprogram "My File1" "My File 2" Now I want to call that from a bash script. My bash script needs to read the arguments from command line and pass them to the c program. I can read the arguments fine but I can't pass them correctly because the arguments have spaces in them. Right now I am doing something like, myargs+=" \""$OPTARG"\"" echo $myargs #Echo prints "My File1" "My File 2" myCProgram $myargs This fails. Bash puts ` in spaces. Then I tried myCProgram "$myargs" This fails too as the whole $myargs is passed as single parameter to myCprogram. How can I fix this. Thanks, MSR |
Try passing the list as "$@"
|
THanks guboj. But that will not work for me. The example I gave was a simple one. In reality, my bash script needs to read multiple arguments and call multiple programs passing different arguments to each one. So I can't just pass $@.
|
If you have space in your args, use a different default delimiter, for example:
args.sh Code:
OFS=$IFS Code:
args=("$@") Code:
Argument 4 is e f |
I fail to see why that will not work.
You mean like in this case: Code:
#!/bin/bash Code:
#!/bin/bash Code:
./1.sh asdf ñklj poiu rqwer piupo "foo fight" Code:
"asdf" |
Sorry if I wasn't clear. I have no issues reading and echoing all the arguments in my script. But its passing them correctly to a different program thats giving me problems.
Inside the script, I need to call two different programs ./myProgramA "My Arg 1" "My arg4" "My arg 5" #All parameters with -a ./myProgramB "My Arg 2" "Myarg3" #All arguments with -b As you can see I can't just pass $@. I need to first parse all the arguments (which I can do correctly) and then disperse them seperately to appropriate program. . But no I need to myScript -a "My Arg 1" -b "My Arg 2" -b "Myarg3" -a "My arg4" -a "My arg 5" So I tried programAArgs=" " programBArgs=" " while getopts "a:b:" Option do case $option in a ) programAArgs=" \""$OPTARG"\"" ;; b ) programBArgs=" \""$OPTARG"\"" ;; esac done echo $programAArgs #"My Arg 1" "My arg4" "My arg 5" echo $programBArgs #"My Arg 2" "Myarg3" ./myProgramA $programAArgs #This fails. Bash puts ` at blanks ./myProgramA "$programAArgs" #This fails too as all of "My Arg 1" "My arg4" "My arg 5" is passed as one single argument to myProgramA not as 3 arguments Hope that makes it clear |
I see. I now understand what the problem is. Well, then using an alternative IFS at kenoshi said above is the easiest way to go. Sorry for the misunderstanding.
|
Thanks. I looked at kenoshi's solution. Its not clear to me how I can use it. I am new to scripting so excuse my ignorance. I am still trying to understand his solution.
|
The whole trick would be based on using any other arbitrary character as a field separator, kenoshi uses the colon ":". To specify the field separator you use the IFS variable. So, in the first script you parse everything and save the parameters on strings. Each string will contain an arbitrary number of parameters separated by the ":" (or whatever). Then you can send those strings to a second script, to a function, or wherever, just remember to set IFS before that, so this list of elements separated by : will be interpreted as a list of separated entities (arguments).
This is a quick example and I didn't even test it, but I think it should work at least for the most part, and it will illustrate a couple of example functions Code:
#!/bin/bash |
Awesome. Thanks kenoshi and i92guboj. I think I finally got it. Thanks for detailed explanation.
|
I'm jumping in a little late, but here goes...
If you change the IFS to “ (a double quotation mark), your script will work with a couple of modifications: Code:
programAArgs="" Code:
./myProgramA”My Arg 1"My arg4"My arg 5 |
All times are GMT -5. The time now is 06:08 AM. |