Operating System - HP-UX
1748251 Members
3832 Online
108760 Solutions
New Discussion юеВ

Re: ssh , su , sh re-visited with "for" loop

 
SOLVED
Go to solution
eric lipede_1
Regular Advisor

ssh , su , sh re-visited with "for" loop

Hi
does anyone have any ideas on how to write (as root):

ssh -f hpuxbox su - adiffuser -c sh -c "for i in 1\;do echo this is \$1\;done"

correctly as I currently get :
"for: Command not found."

Thks in advance for the answers and for all those that are tempted to say ..why dont you try using another method ie remote scripts ..thks for that but I really need the answer to the queston I have specifically asked. Cheers.
Points will be assigned.
13 REPLIES 13
vishnu.khandare
Respected Contributor

Re: ssh , su , sh re-visited with "for" loop

Hi Eric,

Which command ur exactly try to run??
That command is not getting to shell thats y ur getting this error command not found.
If ur running from non root user, u need to ad d that command to users shell.

Hope this solves ur query.

Regards
Vishnu Khandare
You should deserve before U desire!!!!
eric lipede_1
Regular Advisor
Solution

Re: ssh , su , sh re-visited with "for" loop

Hi
not really as you didnt read/ understand the question right...but thks for your attempt anyway

Ill try to elaborate.....

I am logged in as root.
I wish to ssh to hpuxbox.
I wish to su - to adiffuser.
I wish change / switch shell from the default shell of adiffuser (csh) to sh.
I wish to run a for loop once im in that shell.
......all in one line.

The command i provided encapsulates what i want to do and what commands I wish to run.

fyi -> if i ssh , then su, then sh, then via the cmd line run "for....." ..it works .

thks in advance.
James R. Ferguson
Acclaimed Contributor

Re: ssh , su , sh re-visited with "for" loop

Hi Eric:

You might try (untested):

# ssh -f hpuxbox su - adiffuser -c "{ for i in 1;do echo this is $i;done; }"

You might also reference the dialog that began in your initial thread:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1454190

Regards!

...JRF...
eric lipede_1
Regular Advisor

Re: ssh , su , sh re-visited with "for" loop

Hey James

mmmm tried but no peanuts, i also tried different combos of adding "\" and no joy :-(
eric lipede_1
Regular Advisor

Re: ssh , su , sh re-visited with "for" loop

....also tried:

ssh -f hpuxbox su - adiffuser -c sh -c "{for i in 1\;do echo this is \$1\;done}"

..and no
James R. Ferguson
Acclaimed Contributor

Re: ssh , su , sh re-visited with "for" loop

Hi Eric:

You don't offer what "didn't work" --- syntax error?

Make sure that you have whitespace surrounding the curly braces. Use single quotes, too:

These work in a local environment:

# sh -c '{ for i in 1 2 3;do echo this is $i;done; }'

# su - adiffuser -c '{ for i in 1 2 3; do echo this is $i;done; }'

Regards!

...JRF...
eric lipede_1
Regular Advisor

Re: ssh , su , sh re-visited with "for" loop

James,
Ive listed the errors..
ssh -f ahpuxbox su - adiffuser -c sh -c '{ for i in 1\;do echo this is \$i\;done }'
ssh -f ahpuxbox su - adiffuser sh -c '{ for i in 1;do echo this is $i;done }'
sh: Syntax error at line 1 : `do' is not expected.

ssh -f ahpuxbox su - adiffuser sh -c '{ for i in 1\;do echo this is $i\;done }'
sh: No such file or directory

ssh -f ahpuxbox su - adiffuser -c sh -c '{ for i in 1\;do echo this is $i\;done }'
The copyright note can be read by typing: cat /etc/copyright
Not a terminal
stty: : Not a typewriter
{: Command not found.

ssh -f ahpuxbox su - adiffuser -c sh -c "{ for i in 1\;do echo this is \$i\;done }"
The copyright note can be read by typing: cat /etc/copyright
Not a terminal
stty: : Not a typewriter
{: Command not found.

As you know the issue is associated with the ssh use and passing the commands when they get to the remote node. I hope the errors throw more light on the issue.

Running each part of the command separately works fine....just a problem when you put all of them together.

Thks
James R. Ferguson
Acclaimed Contributor

Re: ssh , su , sh re-visited with "for" loop

Hi (again) Eric:

I don't see why you need/want to impose another shell (... 'sh -c' ...)

Instead of:

# ssh -f hpuxbox su - adiffuser -c sh -c { for i in 1 2 3; do echo this is $i;done; }'

...try:

# ssh -f hpuxbox su - adiffuser -c '{ for i in 1 2 3; do echo this is $i;done; }'

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: ssh , su , sh re-visited with "for" loop

>ssh -f hpuxbox su - adiffuser -c sh -c "for i in 1\;do echo this is \$1\;done"

I would suggest you first need to quote the stuff after the first -c:
ssh -f hpuxbox su - adiffuser -c 'sh -c "for i in 1; do echo \"this is \$1\"; done"'

>JRF: I don't see why you need/want to impose another shell (... 'sh -c' ...)

Eric made it quite clear. That user is using the scummy C shell and he didn't want to be caught dead using it. ;-)

It seems that copying a real shell script over to the machine as suggested in the previous thread looks more and more the easiest solution.