0

I have a script that I want it to receive a number of variable arguments and pass them to the execution of another script, but I'm only being able to pass the first argument set by the first call.

My script (nodetool_improved) looks like this:

sudo su cassandra -s /bin/bash -c "/usr/local/cassandra/bin/nodetool -u cassandra -pwf /opt/apps/cassandra/resources/security/jmxremote.password $@"

If I call it like:

$ nodetool_improved status

Then /usr/local/cassandra/bin/nodetool receives status, and the output looks OK. But if I call my script like:

$ nodetool_improved help status

Then the output instead of showing the help for the status option, just shows the help for all options, which is effectively the same as calling:

$ /usr/local/cassandra/bin/nodetool help

That means that help status is not being passed to /usr/local/cassandra/bin/nodetool. Only help.

How can I change my nodetool_improved script so that all arguments are passed to /usr/local/cassandra/bin/nodetool?

Some things I've tried and which don't work:


sudo su cassandra -s /bin/bash -c "/usr/local/cassandra/bin/nodetool -u cassandra -pwf /opt/apps/cassandra/resources/security/jmxremote.password -- '$@'"

Error:

status': -c: line 0: unexpected EOF while looking for matching `''
status': -c: line 1: syntax error: unexpected end of file

sudo su cassandra -s /bin/bash -c '/usr/local/cassandra/bin/nodetool -u cassandra -pwf /opt/apps/cassandra/resources/security/jmxremote.password "$@"' -- "$@"

Error:

Only the status gets picked up. The output is the same as if I called it like:

/usr/local/cassandra/bin/nodetool status
0

2 Answers 2

2

You can't use '$@' inside double quotes to get the same semantics as "$@". Instead I would pass the arguments outside of the quoted string.

sudo su cassandra -s /bin/bash -c '/usr/local/cassandra/bin/nodetool -u cassandra -pwf /opt/apps/cassandra/resources/security/jmxremote.password "$@"' _ "$@"

Inside the single quotes you have the literal string "$@" which then gets populated by Bash with the arguments to bash -c '...' which come after the string. (The -- is required because the first token after bash -c '...' is used to populate $0, not $@.)

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

5 Comments

Only the "status" argument gets picked up. See the edit on my post.
Then you have something else going on which drops it from $@ before this runs; or maybe nodetool only actually accepts a single argument? See demo: ideone.com/Qbo6GO
I'm not adding anything besides what I put in my post, it's literally copy+paste. nodetool accepts more than a single argument. See here: cassandra.apache.org/doc/latest/tools/nodetool/help.html
Not sure if it matters, but the cassandra user has nologin.
It's working now. You were close though! :) Check this comment: superuser.com/questions/1601809/…
0

You need to use "--" to stop the parameter interpretation (it works if nodetool accepts this using getopt). So, try:

sudo su cassandra -s /bin/bash -c "/usr/local/cassandra/bin/nodetool -u cassandra -pwf /opt/apps/cassandra/resources/security/jmxremote.password -- '$@'"

2 Comments

Doesn't work, see my post edit :( I get a "unexpected EOF while looking for matching `'"

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.