0

I'm new to shell scripting and i'm trying to dynamically form the query based on several conditions. here is the code snippet

if [[ $(id -u) -eq 0 ]]
then
  SSH_COMMAND="${cmd} > ${LOG_OUTPUT_FULL_PATH}/kubectlnodes.log"
else
  SSH_COMMAND="'echo paass | sudo -S sh -c '${cmd} > ${LOG_OUTPUT_FULL_PATH}/kubectlnodes.log''"
fi
#echo "executing ${SSH_COMMAND}"
$(${SSH_COMMAND})

my command is written to SSH_COMMAND variable correctly but when executing the command, it execute the echo first and my entire command is failing. I tried the following format to execute the full command in SSH_COMMAND variable ${SSH_COMMAND} and $(${SSH_COMMAND}) but both are partially execute and failing the command.

Appreciate any inputs to this

Cyrus
  • 84,225
  • 14
  • 89
  • 153
user3817206
  • 315
  • 4
  • 14
  • See [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050): *I'm trying to put a command in a variable, but the complex cases always fail!* – Charles Duffy Oct 25 '21 at 20:34
  • To be very clear, by the way, the approaches described in the above-referenced FAQ are far better than several in answers to the linked duplicate question. Using `eval`, in particular, is a source of major security problems; see [BashFAQ #48](http://mywiki.wooledge.org/BashFAQ/048): *Eval command and security issues* – Charles Duffy Oct 25 '21 at 20:36
  • ...in this specific case where you just want to select one of two possible compound commands, put a function definition in each side of your `if` both defining the same function, and you get whichever definition the conditional indicates under that function's name. – Charles Duffy Oct 25 '21 at 20:37
  • 1
    (btw, on a side note, using all-caps variable names for variables you define yourself is hazardous -- all-caps names are used for variables with meaning to the shell and OS-provided tools, so you risk overwriting variables meaningful to the operating by mistake when using that namespace yourself; see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, reserving names with at least one lower-case character for application use -- in this context, your script is an application). – Charles Duffy Oct 25 '21 at 20:40

0 Answers0