KSH script on HP-UX runs SSH with commands. Script evaluates environmental variables before running on server

1k views Asked by At

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

There are 1 answers

12
Donovan On

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