0

My command is

X=`ssh ${OS_USER}@${OS_HOST} ". ~/.kshrc ;
   ulimit -c unlimited ;
   cd ${OS_PATH}/NEW_BL1_RC_RATE/bin/conf;
   echo "export RC_DB_INSTANCE=$7" >> RC_CONV_SET_VARS"`

$7 should be presented in double quotes. How it can be done?

  • Do you want the file to end up containing export RC_DB_INSTANCE="$7", or export RC_DB_INSTANCE="foo" where foo is the 7th argument to the script that you're running locally? – Gilles Jun 3 '16 at 21:10
1

You have multiple levels of nested double-quotes. that gets very tricky, very ugly, and very difficult to read and modify. e.g. `

ssh ${OS_USER}@${OS_HOST} \
    ". ~/.kshrc; ... ;
     echo \"export RC_DB_INSTANCE=\\\"\\\$7\\\"\" >> RC_CONV_SET_VARS".

(NOTE: i tested that multi-quoted echo with bash -c on my system, and it worked with that but I'm not even 100% sure that it will work in ssh. Multiple levels of quotes and backslashes are a PITA and should be avoided)

and then it gets even more complicated if RC_CONV_SET_VARS is not a literal filename but a variable name....further complicated depending on whether RC_CONV_SET_VARS is local to your system or to the remote system.

It's much easier to write a simple throwaway script, scp it to the remote host, and then run it there with ssh.

For example:

Save the following as myscript.ksh:

#! /usr/bin/ksh
. ~/.kshrc
ulimit -c unlimited
cd ${OS_PATH}/NEW_BL1_RC_RATE/bin/conf
echo 'export RC_DB_INSTANCE="$7"' >> "$RC_CONV_SET_VARS"

Then scp it to remote host and run it:

scp myscript.ksh "${OS_USER}@${OS_HOST}:/tmp/"
ssh "${OS_USER}@${OS_HOST}" 'ksh /tmp/myscript.ksh'

There are still quoting issues to be resolved, but they're pretty much the same as you'd have with quoting when writing a script you intend to run on the local system. Not ugly, complicated, and confusing multiple levels of backslash-escaping and quotes.

If this seems impractical because you'd have to type the password multiple times, then don't do that - set up key-based authentication and don't use password-based authentication with ssh.

BTW, you can pass whatever args you need on the ksh myscript.sh command line.

ssh "${OS_USER}@${OS_HOST}" 'ksh /tmp/myscript.ksh arg1 arg2 arg3'

or, if you need to pass the 7th arg of the local script to the remote script:

ssh "${OS_USER}@${OS_HOST}" "ksh /tmp/myscript.ksh \"$7\""

and change the last line of myscript.ksh above to something like:

echo "export RC_DB_INSTANCE=\"$1\"" >> "$RC_CONV_SET_VARS"
-1

You will have to "escape" the quotes:

echo "export RC_DB_INSTANCE=\"$7\""

will do the trick...

  • Tried it in the first place - it doesn't work. – Peck Jun 2 '16 at 8:07
  • Please clarify: The term "export .... =$7" shall be written to the file RC_CONV_SET_VARS on the remote host? Or do you try to intend something different? – gerhard d. Jun 2 '16 at 8:58
  • Correct - shall be written to the file RC_CONV_SET_VARS on the remote host – Peck Jun 2 '16 at 9:48
  • No, that sends " to the remote shell, which parses it, so that the file ends up containing export RC_DB_INSTANCE=foo if $7 is foo. – Gilles Jun 3 '16 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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