0

Here is a snip of the script:

ssh $(hostname | cut -f1 -d1)2 " echo 'Secondary Node ';
OH=$(env | grep ORACLE_HOME | cut -c13-55);
CN=$(env | grep CONTEXT_NAME | cut -c14-40);
cd $OH/appsutil/scripts/$CN;
./myscript.sh pass=****;

clear;
./myscript.sh pass=****;"

Error: Error -ksh: hostname1/appsutil/scripts/erpaaps1_apps1s/myscript.sh: not found

What happens is that script evaluates the variables on the first machine before it ever SSHs to the second. When this happens it goes into the wrong directory (because it got the value from the first server and not the one it SSH'd to).

I have also tried the script this way:

 ssh $(hostname | cut -f1 -d1)2 ' echo "Secondary Node ";
    OH=$(env | grep ORACLE_HOME | cut -c13-55);
    CN=$(env | grep CONTEXT_NAME | cut -c14-40);
    cd $OH/appsutil/scripts/$CN;
    ./myscript.sh pass=****;

    clear;
    ./myscript.sh pass=****;'

Error: -ksh: hostname1/appsutil/scripts/erpaaps1_apps1s/myscript.sh: not found

It still evaluates the variables from the first machine before ever running on the second.

If I take the script and try it on the remote machine locally it works as expected with the correct directories.

에이바바
  • 1,011
  • 11
  • 36
  • 60

1 Answers1

1

You'll need to put the part you want to run on the remote machine in single-quotes.

ssh $(hostname | cut -f1 -d1)2 ' echo "Secondary Node ";
OH=$(env | grep ORACLE_HOME | cut -c13-55);
CN=$(env | grep CONTEXT_NAME | cut -c14-40);
cd $OH/appsutil/scripts/$CN;
.myscript.sh pass=****;
clear;
./myscript.sh pass=****;'

This prevents the local shell from interpolating the variables before sending them to SSH.

Even better:

Put all those commands in a shell script on the remote machine, and then just execute that script.

ssh ssh $(hostname | cut -f1 -d1)2 /path/to/my/script.sh
Donovan
  • 15,917
  • 4
  • 22
  • 34
  • I've already tried it this way and it does not work, it still gets the values from the first server. I cannot write scripts on the other servers I need to SSH to. – 에이바바 Dec 16 '13 at 21:24
  • Make sure you replace the existing single-quotes with double-quotes. – Donovan Dec 16 '13 at 21:27
  • Donovan, I've already done this. I did replace the quotes within the echo command. – 에이바바 Dec 16 '13 at 21:27
  • Try my other suggestion then, put all that in a shell script on the remote machine, test to be sure it does what you want it to do, and then simply execute that remote script via SSH. – Donovan Dec 16 '13 at 21:28
  • The script works on the remote machine, it only does not work when it has to SSH from the first one within the script. – 에이바바 Dec 16 '13 at 21:29
  • So why can't you run the remote script via ssh? `ssh $(hostname | cut -f1 -d1)2 /path/to/my/script.sh` – Donovan Dec 16 '13 at 21:32
  • Because I have hundreds of servers to SSH to and I cannot write this file to each server and run it that way. I'm just trying to change to a directory where the scripts I need to run are. The name of this directory varies on each server and is in the env. The problem is that the first server always evaluates the commands and fills it in with its own directories first :( – 에이바바 Dec 16 '13 at 21:34
  • Please update your question with the exact error you get when you try to run this (with single-quotes) – Donovan Dec 16 '13 at 21:37
  • Error `-ksh: hostname1/appsutil/scripts/erpaaps1_apps1s/myscript.sh: not found` – 에이바바 Dec 16 '13 at 21:48
  • It works fine here. Try using additional escapes in front of any variable names `\$` – Donovan Dec 16 '13 at 21:49
  • Are you using it with KSH on a Unix machine? I can get it to work if I test it with Linux/Bash on my test box, but not with HP-UX(Unix)/KSH (which is where it needs to work). – 에이바바 Dec 16 '13 at 21:54
  • Unfortunately, I do not have access to an HP-UX system to test this. Honestly, if it were me, I would either take the time to create the scripts on the remote machines (push them out with scp) or, better yet, ask the DBA to fix his/her `myscript.sh` so that it lives in a common area like `/opt/bin` so you can call it without needing all the wrapper cruft. – Donovan Dec 16 '13 at 22:11