0

how can I pass a parameter to a shell script that is a longer string with spaces and "-"? Here is what I currently have:

/usr/test.sh -g filesystem -q -p "-p \"/usr/\" -s \"some intern string\" -q"  

The test.sh script recognizes two -p, two -q and some other things, that are not right. But the value behind the first -p should be one large string: -p "/usr/" -s "some intern string" -q

What is my mistake?

Thanks for any help,
greetings,
Chriss

3
  • This really smells like an XY problem. What are you doing with "${flags[parameter]}" ? Once you concatenate a string with spaces into another string with spaces, there's no reliable way to extract the original string. Commented Oct 22, 2015 at 17:58
  • Also, you're adding extra quotes into the values here: wasteParameter+=("\"$arg\"") Commented Oct 22, 2015 at 17:59
  • @glennjackman I am afraid, but the script does not run to this point. It interrupts at the point if [ $? != 0 ] ; then with the error, that it found some not allowed parameter. So there must be a problem with getopt I guess. Commented Oct 23, 2015 at 9:04

1 Answer 1

1

One way of seeing what is being passed is running printf "<<%s>>\n" yourcommand, which will print each individual argument on a line surrounded by <<..>> for easy disambiguation:

$ printf "<<%s>>\n" /usr/test.sh -g filesystem -q -p "-p \"/usr/\" -s \"some intern string\" -q"
<</usr/test.sh>>
<<-g>>
<<filesystem>>
<<-q>>
<<-p>>
<<-p "/usr/" -s "some intern string" -q>>

As you can see, the argument to -p is indeed the string -p "/usr/" -s "some intern string" -q.

If test.sh doesn't work, it's a bug in test.sh, not in this invocation.

Sign up to request clarification or add additional context in comments.

3 Comments

I added the important part out of my test.sh file and I really hope, you can explain my what I did wrong here. and sorry for stealing your time :(
@christopher2007 That's a separate question, but what you pasted from test.sh is fine and if you put printf "<<%s>>\n" "${flags[parameter]}" at the end you get the expected value of <<-p "/usr/" -s "some intern string" -q>>
I am afraid, but the script does not run to this point. It interrupts at the point if [ $? != 0 ] ; then with the error, that it found some not allowed parameter. But as you mentioned, with printf "<<%s>>\n" /usr/test.sh -g filesystem -q -p "-p \"/usr/\" -s \"some intern string\" -q" the correct parts are displayed. Is there something wrong with my getopt?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.