0

This is all a bit messy in my head, so I shall do my best to explain!

I have the following script:

#!/usr/bin/env bash

# make, then run
make --quiet

execute_and_echo() { echo "\$ $@" ; "$@" ; }

# add sudo if we need to - and pass all arguments to the target
if [[ $EUID -ne 0 ]]; then
    execute_and_echo sudo ./utility "$@"
else
    execute_and_echo ./utility "$@"
fi

The intended purpose is to build the utility using make, then run it (appending sudo if we're not root) while passing all of the arguments that are passed to the script on to the utility. This mostly works, but not with some command line arguments.

One argument that utility accepts is (in a case of extreme recursion) another shell command to run from within the utility. Believe me - this makes sense for my application. The form of that argument might be:

utility -s "echo banana"

This works fine when I run it directly from the command line. However, if I instead run

./run -s "echo banana"

the quotes are stripped out and I get undesired behaviour. Does anyone know how to tweak this to get a better behaved script?

stefandz
  • 60
  • 11
  • 5
    I think the situation is well explained here http://stackoverflow.com/questions/1668649/how-to-keep-quotes-in-args – fedterzi Jun 15 '16 at 12:47
  • @fedterzi actually I tried all of the options there but none quite work – stefandz Jun 15 '16 at 13:32
  • with `bash -c "$@"` I get the expected behavior if I do `./test.sh "echo test"` (just prints test), is it possible that your utility doesn't read stdin properly? Could you share the utility or at least just the way it handles stdin? – fedterzi Jun 15 '16 at 13:40
  • 1
    There's always a chance that the utility is the issue, but if I pass the argument normally at the bash prompt, that works. Happy to share the utility, but it's complex and hardware specific, so I shall try and write a minimal example from the more complex code and share that :) – stefandz Jun 15 '16 at 13:43
  • OK - this is strange. I seem to now no longer be seeing the undesired behaviour - writing the minimal example made the issue disappear, and then going back to the original code shows no issues. Not at all sure what changed. The quotes are still being stripped, but I can deal with that cosmetically. – stefandz Jun 15 '16 at 14:14
  • you can run your scripts with `bash -xv` to debug it – Felipe Lema Jun 15 '16 at 16:38

0 Answers0