Re: ssh , su , sh re-visited with "for" loop (322 Views)
Reply
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 1 of 14 (322 Views)
Accepted Solution

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.
Please use plain text.
Respected Contributor
vishnu.khandare
Posts: 194
Registered: ‎09-12-2010
Message 2 of 14 (322 Views)

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!!!!
Please use plain text.
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 3 of 14 (322 Views)

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.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 14 (322 Views)

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...
Please use plain text.
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 5 of 14 (322 Views)

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 :-(
Please use plain text.
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 6 of 14 (322 Views)

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 14 (322 Views)

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...
Please use plain text.
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 8 of 14 (322 Views)

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 14 (322 Views)

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...
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,757
Registered: ‎03-06-2006
Message 10 of 14 (322 Views)

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.
Please use plain text.
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 11 of 14 (322 Views)

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 ...
Please use plain text.
Trusted Contributor
Earl_Crowder
Posts: 76
Registered: ‎07-17-2001
Message 12 of 14 (322 Views)

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.
Please use plain text.
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 13 of 14 (322 Views)

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

thanks - that worked !
Please use plain text.
Regular Advisor
eric lipede_1
Posts: 85
Registered: ‎04-09-2010
Message 14 of 14 (322 Views)

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

Thks for all the help
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation