Operating System - HP-UX
1830936 Members
1924 Online
110017 Solutions
New Discussion

Re: working with $(...) and echo ${...} in combination with remsh ??

 
SOLVED
Go to solution

working with $(...) and echo ${...} in combination with remsh ??

Pop quiz:

$ date
Wed Feb 26 12:24:18 MET 2003

$ echo date
date

$ echo `date`
- or -
$ echo $(date)
Wed Feb 26 12:24:18 MET 2003

$ a=1
$ echo ${a}
1

If I am logged in on host A
$ remsh B "echo `hostname`"
$ remsh B "echo $(hostname)"
A

Hmmmm... I would have expected to see "B" here.

Also:
If I am logged in on host A
$ a=1
$ remsh B "a=2 ; echo ${a}"
1

Hmmmm... I would have expected to see "2" here.

Note: exporting is irrelevant in combination with remsh (only with xterms and such).

How am I supposed to port a local script to run remotely ? I can not hold any temporary data. This behaviour limits my possiblilities severely. I do not know / have any means of finding out what the proper approach is.

Please assist.
17 REPLIES 17
Robin Wakefield
Honored Contributor
Solution

Re: working with $(...) and echo ${...} in combination with remsh ??

Hi Harald,

You need single quotes to prevent shell interpolation of the $.

rgds, Robin
Christian Gebhardt
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

Hi

try

$ remsh B 'hostname'
B

Chris
Balaji N
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

hi
how about
remsh B 'echo `hostname`'

-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.

Re: working with $(...) and echo ${...} in combination with remsh ??

This is indeed a step forward, but the script I'm actually using now runs without launching any xterm whatsoever. No errors too though...

==============================================
#!/bin/sh -p
unset ENV

LOGINSRV=######
PACKAGE=#######
DISPLAY=#######:0.0

# Extract a list of users based on cluster package name
USERLIST=$(ypcat -k auto.user | grep $PACKAGE |awk '{print $1}' 2>/dev/null)

# Launch an xterm
remsh $LOGINSRV 'for USER in $USERLIST
do xterm -display $DISPLAY -e 'sleep 3' &
done'

# If it works, I'll modify the xterm to execute for each user.
==========================================




Balaji N
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

#!/bin/sh -p
unset ENV

LOGINSRV=######
PACKAGE=#######
DISPLAY=#######:0.0

# Extract a list of users based on cluster package name
USERLIST=$(ypcat -k auto.user | grep $PACKAGE |awk '{print $1}' 2>/dev/null)

# Launch an xterm
remsh $LOGINSRV "for USER in $USERLIST;
do; xterm -display $DISPLAY -e 'sleep 3' &;
done"

does this work?
-b-
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.

Re: working with $(...) and echo ${...} in combination with remsh ??

To simplify, please try this:

-Suppose I'm user X on host A

$ remsh B 'for a in X X
> do xterm -title hello -display A:0.0 -e 'sleep 3' &
> done'

This works...

$ USERLIST="X X"
$ remsh B 'for USER in $USERLIST
> do xterm -title hello -display A:0.0 -e 'sleep 3' &
> done'

This doesn't...

Solution:

$ remsh B 'USERLIST="X X" ; for USER in $USERLIST
> do xterm -title hello -display A:0.0 -e 'sleep 3' &
> done'

Case closed, I guess...

What I've learned:
* Use ' instead of " when using remsh to isolate arguments.
* Passing variables through remsh is not possible.

Thank you for your help.
Adam J Markiewicz
Trusted Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

Hi

How about:

$ USERLIST="X X"
$ remsh B 'for USER in ' $USERLIST '
> do xterm -title hello -display A:0.0 -e 'sleep 3' &
> done'

Good luck

Adam
I do everything perfectly, except from my mistakes
Christian Gebhardt
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

Hi Harald

your last comment is not correct:

* Passing variables through remsh is not possible.

$ remsh B 'echo '$USERLIST''

works

It's all a question of quoting.

Chris
Christian Gebhardt
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

Here is an article about "parsing the command line".

http://users.evitech.fi/~atrost/Unix/shellfunc.html

It is very important in which order the shell interprets the characters and how you can affect this with quotations.

Chris

Ps: Do you know something about the pointing system in this forum ??

Re: working with $(...) and echo ${...} in combination with remsh ??

Have just assigned points to you all...

Am having difficulties using ' quotes more than once:

#!/bin/sh -p
unset ENV

LOGINSRV=####

remsh $LOGINSRV 'PACKAGE=#####
DISPLAY=#####:0.0
USERLIST="$(ypcat -k auto.user | grep $PACKAGE |awk '{print $1}')"
for USER in $USERLIST
do xterm -title hoi -display $DISPLAY -e 'sleep 3' &
done'

The ' quotes near the awk command are interfering with the remsh ones...
Guess I just don't know how ' quotes are interpreted by the shell...

Will do some homework.


Christian Gebhardt
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

Hi

to prevent you working at home try this with your awk:

old: awk '{print $1}'
new: awk '\'{print \$1}\''

Chris

Even if it appears somewhat importunate but maybe you are not logged in, but there are no points assigned.
Jorrit Visschers
New Member

Re: working with $(...) and echo ${...} in combination with remsh ??

>Hi Harald
>
>your last comment is not correct:
>
>* Passing variables through remsh is not >possible.
>
>$ remsh B 'echo '$USERLIST''
>
>works
>
>It's all a question of quoting.
>
>Chris

I think Harald means actual variable exportation from the local host to the remote host, not expanding the contents of the local variable and sending that result to the remote command as an argument.

For example;
$ remsh B 'echo '$USERLIST'; printenv'

will print out indeed the content, but the printenv command shows that the variable is NOT set on the remote host.

Then again, I might be wrong :-)




Rebmal
New Member

Re: working with $(...) and echo ${...} in combination with remsh ??

Try the next commandlines and notice the difference in output:

USERLIST="1 2"
echo ${USERLIST}

remsh xxxx -n "for i in ${USERLIST} ; do echo ${i} ; cd /user/${i}; echo $(hostname); echo ${USERLIST}; pwd ; hostname; done"

remsh xxxx -n "USERLIST="A B" ; for i in ${USERLIST} ; do echo ${i} ; cd /user/${i} ; echo $(hostname) ; pwd ; \ hostname; done"

remsh xxxx -n "USERLIST=\"A B\" ; for i in \${USERLIST} ; do echo \${i} ; \ cd /user/\${i} ; echo \$(hostname) ; echo \${USERLIST}; pwd ; hostname; done"

So you can prevent the shell parsing your variables on your current system before executing the command on the remote system.
Rebmal
New Member

Re: working with $(...) and echo ${...} in combination with remsh ??

Retry.

Just checked my reply on this pages and noticed a few to much backslashes in my reply. I guess due to mistakes while typing. I'll try to correct them, because the \ does the trick.

USERLIST="C D"
echo ${USERLIST}

remsh xxxx -n "for i in ${USERLIST} ; do echo ${i} ; cd /home/${i}; echo $(hostname); echo ${USERLIST}; pwd ; hostname; done"

remsh xxxx -n "USERLIST='A B' ; for i in ${USERLIST} ; do echo ${i} ; cd /home/${i} ; echo $(hostname) ; pwd ; hostname; done"

remsh xxxx -n "USERLIST=\"A B\" ; for i in \${USERLIST} ; do echo \${i} ; cd /home/\${i} ; echo \$(hostname) ; echo \${USERLIST}; pwd ; hostname; done"

For this example i assume the users homedirectory's are in /home and exist on the remote server.

Some explanation:
Putting a \ before a special character prevents it being interpreted on the current host, but send it 'as it is' to the remote host. This way your can pass variables to another system, even if you need qouting '" on the other side.
Chris Vail
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

I do a lot of this kind of thing, actually--write a script locally and execute it remotely and capture the data back on the local system. This is easier than you make it out to be:
rcp path/script remhost:/path/script
remsh remhost:"/path/script">>localpath/localfile.

Else, you have to do the arcane quoting mechanisms you and the others mention:
remsh somehost:"for I in `ls -l path`;do;echo $I;done".

This is way too much to do on a command line. I enjoy command line scripting, but I'm not insane about it.....


Chris

Re: working with $(...) and echo ${...} in combination with remsh ??

Thank you all for your help.

Here's the *final* version of the script I wrote.

What it does ?
It connects to a number of servers and starts a number of "su - &". Due to circumstances (MC/Serviguard rpc.lockd /NFS issues) users are faced with a delay before the prompt is returned (once) when logging in. This script will login for each user to go through the symptoms first and 'take the heat' so users will not notice anything.

#!/bin/sh -p
unset ENV

COMMAND="echo \$(hostname) \$(bdf . | grep lvol) ; exit"

for LOGINSRV in A B C D
do
for PACKAGE in $(ypcat auto.user| grep ... | awk -F: '{print $1}' | sort -u)
do
# A remote shell is invoked via remsh
remsh $LOGINSRV -n "for USER in "$(ypcat -k auto.user | grep $PACKAGE | awk '{print $1}')"
do
/usr/bin/timeout -t 100 su - \$USER -c '$COMMAND 2>/dev/null' 2>/dev/null &
done" &
done
done

I still don't know exactly what single quotes (') do, - or better - what the difference is with using backslashes (\)....
Christian Gebhardt
Honored Contributor

Re: working with $(...) and echo ${...} in combination with remsh ??

Hi Harald

Nice to hear that your script is working.

I'm working with UNIX for about ten years and from my experience the usage of quotes (single or double) and backslashes (where and how many) is

40% knowledge
30% experience
30% try and error

Have a nice day and a beautiful weekend

Chris