Setting variable from within SSH session

Setting variable from within SSH session

am 25.09.2007 11:08:03 von timothy.hill

Hi all,

I am attempting to write a KSH script to do the following:

> Log onto a remote system using SSH
> Execute some commands on said system
> Set some variables on remote system
> Run some more commands
> Exit
> Done

My problem is that I am unable to set any variables, no matter how I
go about doing things. The method I started with is:

#!/bin/ksh

ssh xxxx@xxxxxx << EOF
FILE=$(ls -rt * | tail -1)

echo FILE: $FILE

cat $FILE | grep >
EOF

The above is extremely simplified, but in essence, is what I want to
do. What it seems to do is that the $() opens a new shell on the
machine I run the script on and not the remote system.

I have also tried redirecting the output of the ls to a file and then
trying to read that into a variable, but that just doesn't seem to
work either.

Any help would be most gratefully received.

Cheers

Tim

Re: Setting variable from within SSH session

am 25.09.2007 13:29:04 von Vakayil Thobias

Prince Al wrote:
> Hi all,
>
> I am attempting to write a KSH script to do the following:
>
>
>>Log onto a remote system using SSH
>>Execute some commands on said system
>>Set some variables on remote system
>>Run some more commands
>>Exit
>>Done
>
>
> My problem is that I am unable to set any variables, no matter how I
> go about doing things. The method I started with is:
>
> #!/bin/ksh
>
> ssh xxxx@xxxxxx << EOF
> FILE=$(ls -rt * | tail -1)
>
> echo FILE: $FILE
>
> cat $FILE | grep >
> EOF
>
> The above is extremely simplified, but in essence, is what I want to
> do. What it seems to do is that the $() opens a new shell on the
> machine I run the script on and not the remote system.
>
> I have also tried redirecting the output of the ls to a file and then
> trying to read that into a variable, but that just doesn't seem to
> work either.
>
> Any help would be most gratefully received.
>
> Cheers
>
> Tim
>

Please create a script in the remote machine with the following commands
and named it as remote_file.ksh

#!/bin/ksh
FILE=$(ls -rt * | tail -1)
echo FILE: $FILE
cat $FILE | grep >


After that run the ssh script like this :

#!/bin/ksh

ssh xxxx@xxxxxx << EOF
../remote_file.ksh
EOF

Re: Setting variable from within SSH session

am 25.09.2007 17:02:06 von Bill Marcum

Prince Al wrote:
>
> Hi all,
>
> I am attempting to write a KSH script to do the following:
>
>> Log onto a remote system using SSH
>> Execute some commands on said system
>> Set some variables on remote system
>> Run some more commands
>> Exit
>> Done
>
> My problem is that I am unable to set any variables, no matter how I
> go about doing things. The method I started with is:
>
> #!/bin/ksh
>
> ssh xxxx@xxxxxx << EOF
> FILE=$(ls -rt * | tail -1)
>
> echo FILE: $FILE
>
> cat $FILE | grep >
> EOF
>
> The above is extremely simplified, but in essence, is what I want to
> do. What it seems to do is that the $() opens a new shell on the
> machine I run the script on and not the remote system.
>
> I have also tried redirecting the output of the ls to a file and then
> trying to read that into a variable, but that just doesn't seem to
> work either.
>
> Any help would be most gratefully received.
>
> Cheers
>
> Tim
>
Variables in a here document are expanded by the parent shell unless the
end marker (or part of it) is quoted.
ssh xxx@xxxxxx<<'EOF'

Re: Setting variable from within SSH session

am 25.09.2007 21:53:13 von E U

Hi, I am trying to do almost same thing, except I want the output to a
local variable for comparison sake.

l_var1=xx

l_var2= `
ssh xxxx@xxxxxx << EOF
FILE=$(ls -rt * | tail -1)
echo FILE: $FILE

cat $FILE | grep grep_string | wc -l
EOF
`
status=$?

if [[ $status > 0 ]] then
echo problem running on remote server.
exit 99
fi

if [[ $l_var1 = $l_var2 ]] then
echo $l_var1 matches with $l_var2
fi

Any idea what could go wrong here (syntex error don't count)?