0

I'm still new to shell scripting.I am having trouble running this 1 line of code with quotations in the argument in shell. Is there any other way to run this line of code? I am running this as a root user. I get the issue of the code not running when executed.

su - nfoadm -c ""hdbsql -CQajx -U DEFAULT "select entry_id, entry_type_name, utc_start_time, utc_end_time, state_name, message from M_BACKUP_CATALOG where entry_type_name = 'complete data backup' order by backup_id"""

basically i want to su to nfoadm and then execute the whole command of hdbsql.

Thereal jubster
  • 43
  • 1
  • 1
  • 6

2 Answers2

0

Try this:

su - nfoadm -c 'hdbsql -CQajx -U DEFAULT "select entry_id, entry_type_name, utc_start_time, utc_end_time, state_name, message from M_BACKUP_CATALOG where entry_type_name = \'complete data backup\' order by backup_id"'
Samarth
  • 627
  • 6
  • 13
  • 1
    Slightly broken -- the literal single quotes will exit the external syntactic ones. – Charles Duffy Mar 28 '17 at 17:23
  • Missed that. Edited. – Samarth Mar 28 '17 at 17:35
  • That doesn't actually work. Backslashes are literal inside single quotes, so they don't successfully escape other single quotes. – Charles Duffy Mar 28 '17 at 17:37
  • ...escaping a literal backslash inside a single-quoted string looks like: `printf '%s\n' 'this is a single quote: '"'"' <- there'`. Which is to say, you're *ending* the single-quoted string, opening a double-quoted string, putting in your single quote, and then switching back to single quotes. – Charles Duffy Mar 28 '17 at 17:39
  • I'm glad you did, though I described it so poorly. (ergh; should have been "escaping a literal single quote", of course). – Charles Duffy Mar 28 '17 at 17:45
  • @Charles Duffy i still seem to be confused about this quotations but correct me if im wrong but if its ' i need to protect is using double quotes? and with " i need to protect it with ' ? can you give me an simple example of using ' and " in one line im still confused haha – Thereal jubster Mar 28 '17 at 20:46
  • See http://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings – Charles Duffy Mar 28 '17 at 21:03
0

Best practice is to have the shell do the quoting for you, instead of trying to do it yourself.


In bash, the ideal way to do this is with printf -v varname '%q ' command with args..., which puts a string which will run your command into varname.

#!/bin/bash
cmd=( hdbsql -CQajx -U DEFAULT "select entry_id, entry_type_name, utc_start_time, utc_end_time, state_name, message from M_BACKUP_CATALOG where entry_type_name = 'complete data backup' order by backup_id" )
printf -v cmd_q '%q ' "${cmd[@]}"
su - nfoadm -c "$cmd"

If you don't have bash, printf can be used inside a command substitution; ksh93 will optimize this to avoid the usual fork costs that command substitutions entail.

#!/bin/ksh
cmd=( hdbsql -CQajx -U DEFAULT "select entry_id, entry_type_name, utc_start_time, utc_end_time, state_name, message from M_BACKUP_CATALOG where entry_type_name = 'complete data backup' order by backup_id" )
cmd_q=$(printf '%q ' "${cmd[@]}")
su - nfoadm -c "$cmd"

If you need to work a shell having no non-POSIX extensions, consider using sudo instead of su:

#!/bin/sh
sudo -u nfoadm hdbsql -CQajx -U DEFAULT "select entry_id, entry_type_name, utc_start_time, utc_end_time, state_name, message from M_BACKUP_CATALOG where entry_type_name = 'complete data backup' order by backup_id"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • i tried the method with bash. I executed the script and it seems only to detect the command hdbsql but not the whole script? – Thereal jubster Mar 28 '17 at 20:33
  • What "whole script"? There's only one line given in your question. If you mean it's not picking up the arguments, I'd suggest using `bash -x yourscript` to run it in a mode where it's printing every command for debugging -- that'll at least allow a better explanation of what's actually going on. – Charles Duffy Mar 28 '17 at 20:39
  • Also, note that the details matter -- be sure you're testing with the exact quoting, expansion forms, etc., given. Ideally, I'd like to see a [gist](https://gist.github.com/) with the exact code you're running as one file, and the logs of running it with `bash -x` as another (a single gist can have multiple files contained). – Charles Duffy Mar 28 '17 at 20:42