SSH full shell script in 1 line.

I'd like to ssh a piece of shell script code into a single ssh command.

As you all know, you could ssh something like this:

ssh user@host "ls -la"

and receive the output of that command being run on that machine. So, let's say I scripted something on the fly via command line like this:

%prompt%>for i in 1 2 3 4 5
> do
> echo "i = $i\n"
> done
i = 1

i = 2

i = 3

i = 4

i = 5

%prompt%>

Is there a way to get this bundled into 1 command via an ssh? If I bring up this quick script via the command history, I get:

for i in 1 2 3 4 5^Jdo^Jecho "i = $i\n"^Jdone

The "^J" won't work. I tried. Is there some character I can use in it's place? I tried \n and \r, but no dice. Just so we're clear, I'm trying to do something like:

ssh user@host "for i in 1 2 3 4 5^Jdo^Jecho "i = $i\n"^Jdone"

Thanks in advance for your responses.

hello,

You just have to replace ^J by ;

Fatiha

I tried that and it still didn't work. Any other suggestions?

1)

Prompt> mkdir test & cp /home/testfile1.txt /test/testfile1.txt

In the above example the CP command will be executed only if the MKDir command was successful

2)

Prompt> mkdir test ; cp /home/testfile1.txt /test/testfile1.txt

In the above example the CP command will be executed REGARDSLESS if the MKDir command was successful or not.

the second one is executed. i have tried that one.
but when trying the same like for i in 1 2 3;do;echo $i;done; is not working.
i don't know why it is not working.

please anyone send the the solution for the above.

Since the command is double-quoted $i is being expanded locally (to empty string).

[user@host1: /tmp] ssh user@host2 "for i in 1 2 3 4 5; do; echo $i; done"





[user@host1: /tmp] ssh user@host2 "for i in 1 2 3 4 5; do; echo \$i; done"
1
2
3
4
5
[user@host1: /tmp] ssh user@host2 'for i in 1 2 3 4 5; do; echo $i; done'
1
2
3
4
5

i got the solution........

just remove ; after do keyword

i have tried the following

for i in 1 2 3; do echo $i; done;
1
2
3

it works with bash.
Not sure if this will work with Korn and C shells though.

Success! You guys are awesome. Thanks.