Operating System - HP-UX
1827802 Members
2327 Online
109969 Solutions
New Discussion

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.
eric lipede_1
Regular Advisor

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

Hi
thks Dennis!!!

This problem is a pain but the best solution in this case is running it from 1 single local machine. Can we assume, for the sake of focus and clarity that there is good reason why each step is required. Ie...the use of sh -c, su etc.
Essentially, solutions should be robust and portable. In THIS case that is achieved with my question.

Dennis was correct in his observation. I am ensuring that commands run in the same shell, using common commands producing the same results and error types.

Again, this detracts from the actual question. Its isnt which is the best method for achieving the result....its how do I get the provided bit of "code" to work.

I think we are almost there though.....

Dennis ....I tried :

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

but got

# The copyright note can be read by typing: cat /etc/copyright
Not a terminal
stty: : Not a typewriter
for: Command not found.

...and as a reminder ...each element works fine when you log on remotely and perform the commands etc.

ps. I do appreciate suggestions on "other ways"...but in this case i really need the solution to this particular question ...thks in advance.

Points will be assigned regardless so feel free to join in ...
Earl_Crowder
Trusted Contributor

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

ssh -f otherhost 'su - otheruser -c "sh -c \"for i in 1 2 3 ; do echo this is \\\$i ; done\" " '

Works for me.
eric lipede_1
Regular Advisor

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

thanks - that worked !
eric lipede_1
Regular Advisor

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

Thks for all the help