Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


How to have local shell variables in a ksh script seen on remove server in SSH block?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
How to have local shell variables in a ksh script seen on remove server in SSH block?

I have googled this and found many solutions, but none of them are working for me. I am in a korn shell, most others reference bsh, maybe that is the issue? Anyway, all I am trying to do is use a variable I have declared in my main script in a remote shell I am running through ssh.

So I have a script with code similar to this:

Code:
filepath=/xxx/yyy/zzz/
filename=xyz

ssh uid@hostname << 'ENDSSH'
.
.
if [[ -e ${filepath}${filename} ]]; then
....
....
fi

ENDSSH

But filepath and filename are blank in the remote shell as they are declared locally. How do I get those values seen in the SSH block?
# 2  
Quote:
Originally Posted by DJR
I have googled this and found many solutions, but none of them are working for me. I am in a korn shell, most others reference bsh, maybe that is the issue? Anyway, all I am trying to do is use a variable I have declared in my main script in a remote shell I am running through ssh.

So I have a script with code similar to this:

Code:
filepath=/xxx/yyy/zzz/
filename=xyz

ssh uid@hostname << 'ENDSSH'
.
.
if [[ -e ${filepath}${filename} ]]; then
....
....
fi

ENDSSH

But filepath and filename are blank in the remote shell as they are declared locally. How do I get those values seen in the SSH block?
either:
Code:
filepath=/xxx/yyy/zzz/
filename=xyz

ssh uid@hostname << ENDSSH
.
.
if [[ -e \${filepath}\${filename} ]]; then
....
....
fi

ENDSSH

or

Code:
filepath=/xxx/yyy/zzz/
filename=xyz

ssh -q uid@hostname " \
. \
. \
myRemoteVar='foo' \
echo here's my remoteVar->[\${myRemoteVar}] \
if [[ -e ${filepath}/${filename} ]]; then
.... \
.... \
fi " < /dev/null

# the \${vars} are variables to be evaluated on the REMOTE host


Last edited by vgersh99; 3 Weeks Ago at 11:24 AM.. Reason: fixed double-quoting in the 2nd suggestion
# 3  
Thanks but these don't work. Escaping the variables with a backslash does nothing - tried in double quotes also as in \"$var" and either way they are still blank in the SSH block. If I just say
Code:
fn=\$filename 
or
fn=\"$filename"

and then echo $fn right afterwards either way it is blank.

I'm not really sure what you are trying to show in the second one. Yes I can declare a variable within the SSH part and use it there, but that doesn't help me. I need to be able to reference the variables I declared before I accessed the remote server.

--- Post updated at 09:18 AM ---

I've been playing around with it some more and here is something I've found that really stumps me. As I said, escaping it with the backslash does nothing. However if I use double quotes ONLY, it does, sort of:

If I say within the SSH block,
Code:
echo fp = $\filepath

It returns nothing: fp =

However if use doublequotes:
echo fp = "$filepath"

This works, it displays fp = /xxx/yyy/zzz/

However, it still won't assign it to a local variable or work in an expression. So if I try this:
Code:
echo fp = "$filepath"
fp="$filepath"
echo fp = $fp

I get

Code:
fp = /xxx/yyy/zzz/
fp =

Why is it that i can evaluate it in the echo statement but it won't assign it to the remote variable?

Last edited by RavinderSingh13; 3 Weeks Ago at 02:13 PM..
# 4  
DJR,
this is not what I suggested and not how to assign/use/display local vs remote vars - please reread my original post.
Code:
filepath=/xxx/yyy/zzz/
filename=xyz

ssh -q uid@hostname " \
. \
. \
myRemoteVar='foo' \
echo here's my remoteVar->[\${myRemoteVar}] \
if [[ -e ${filepath}/${filename} ]]; then
.... \
.... \
fi " < /dev/null

# the \${vars} are variables to be evaluated on the REMOTE host

# 5  
I think that code in unquoted here documents is problematic: too many levels of quoting.
In zsh you can chain two (or more) redirections
Code:
#!/bin/zsh
filepath=/xxx/yyy/zzz/
filename=xyz

ssh uid@hostname << EOT1 << 'ENDSSH'
filepath=$filepath
filename=$filename
EOT1
.
.
if [[ -e ${filepath}${filename} ]]; then
....
....
fi

ENDSSH

The first part EOT1 is unquoted and assigns the local variables to the remote variables.
Instead of directly feeding the login shell (where ssh might complain that a tty is missing) it is more correct to run an explicit /bin/sh (or /bin/bash)
Code:
ssh uid@hostname /bin/sh -s << EOT1 << 'ENDSSH'

Having this, with any standard shell you can pass the local variables as parameters
Code:
filepath=/xxx/yyy/zzz/
filename=xyz

ssh uid@hostname /bin/sh -s $filepath $filename << 'ENDSSH'
filepath=$1
filename=$2
.
.
if [[ -e ${filepath}${filename} ]]; then
....
....
fi

ENDSSH

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 6  
Thank you for that MadeinGermany. The first way didn't work (maybe because I'm using korn shell) but the second one using /bin/sh -s $filepath $filename << 'ENDSSH' did the trick.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Except script to run a local shell script on remote server using root access
Aditya Avanth
local script: cat > first.sh cd /tmp echo $PWD echo `whoami` cd /tmp/123 tar -cvf 789.tar 456 sleep 10 except script: cat > first #!/usr/bin/expect set ip 10.5.15.20 set user "xyz123" set password "123456" set script first.sh spawn sh -c "ssh $user@$ip bash < $script"... Shell Programming and Scripting
1
Shell Programming and Scripting
Using a find command in ssh but using local variables?
Orionizer
I have a script like this (Yes, I know the DAY6 number isn't right - I'm just testing at this point): DAY0=`date -I` DAY1=`date -I -d "1 day ago"` DAY6=`date -I -d "2 days ago"` if then ssh root@synology1 nohup rm -rf "/volume1/Fileserver/$DAY6" fi I've tested the line to remove the...... Shell Programming and Scripting
5
Shell Programming and Scripting
SQL block in a Shell Script connecting to a local and remote DB
daveu7
Hi All, In a Shell scriipt with a SQL block I want to issue a query against a local DB and a remote DB on a remote server. The shell script is running locally. This is how I connect to the local server. But I want the query to reference remote table in the join. Question can I specify a...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers
ksh script with Interactive ssh on remote server
Jeevanm
HI Unix Gurus, I an stuck in an interesting issue, where I am trying to execute a script on remote server after ssh. The script on remote server is interactive,. Whenever it is called it hangs where it expects input from terminal and I have to terminate it. I have searched through fourm...... Shell Programming and Scripting
12
Shell Programming and Scripting
shell script, reading and resetting local variables
b888c
Hello, I have a problem with trying to run a shell script that reads in user input, validates, and sets to a 'default' value if the input is not valid. I cannot get the portion of resetting to a default value to work. These lines are skipped, and the value of x is still whatever the user...... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers

Featured Tech Videos