I've written a shell script, called keepAbcdRunning, whose job it is to check whether command / process 'abcd' is running and if it isn't, to start it.

keepAbcdRunning has the form:

if pgrep -x $1 > /dev/null
then echo "$1 is running"
else echo "calling $2"
exit 0

As I've written it, keepAbcdRunning needs two input arguments, $1 is the command / process name to check and $2 is the command needed to start abcd.

abcd itself is a script which executes an application that takes a Json formatted string as input. If I wanted to call abcd directly from a terminal, I'd call:

./abcd.sh '{\"type\":\"path\",\"key\":[]}'

However, I seem to be unable to pass the character sequence in such a way that abcd.sh is called correctly from within keepAbcdRunning....

  • 2
    ./abcd.sh '{\"type\":\"path\",\"key\":[]}`` why?!?!? Use ./abcd.sh -key=value -key=value` or even ./abcd.sh type=value&key=value&path=value – Rinzwind Apr 20 '17 at 11:21
  • Did you ever run echo $1 within your script to see what $1 is actually assigned? – Thomas Apr 20 '17 at 11:40
  • @Thomas - yep, and I tried tons of combinations (for $2), but think finally it's cracked.... – indexfingers Apr 20 '17 at 12:41
  • Why are you escaping the double quotes. Also, what @Rinzwind said: It'll be much, much better to just have the standard --option=value form. See: Best way to parse command line args in Bash (StackOverflow) – UniversallyUniqueID Apr 20 '17 at 16:58
  • I must confess that as a complete beginner, @Rinzwind's comment went straight over my head... to further show my naivety, ultimately, the whole thing calls a matlab executable which, at least presently, can't parse arguments of the form --option=value. Would the idea be to pass the arguments in this form to Bash (keepAbcdRunning.sh) and then have that reformat them into json, ready to be passed to matlab (abcd)? – indexfingers Apr 20 '17 at 17:24

This does it I think: "./abcd.sh '{\\\"type\\\":\\\"path\\\"\,\\\"key\\\":[]}'"

Basically needed

\\ -> \

\" -> "

\, -> ,

Finally, whole thing needed to be in double quotes so I could have single quotes in the sequence.... https://stackoverflow.com/questions/35063332/insert-single-quote-with-sed

Edit: This was my first time doing pretty much anything with scripting / linux / unix / ubuntu so take my advice with a pinch of salt, but I subsequently found that writing the json string to a file and then passing reference to that file to be so much simpler than trying to pass the string itself

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.