SSH full shell script in 1 line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SSH full shell script in 1 line.
# 1  
Old 12-16-2011
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:

Code:
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:

Code:
%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:

Code:
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:

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

Thanks in advance for your responses.
# 2  
Old 12-16-2011
hello,

You just have to replace ^J by ;

Fatiha
# 3  
Old 12-19-2011
I tried that and it still didn't work. Any other suggestions?
# 4  
Old 12-19-2011
1)
Code:
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)
Code:
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.

Last edited by zxmaus; 12-19-2011 at 10:39 AM.. Reason: added code tags
# 5  
Old 12-19-2011
Since the command is double-quoted $i is being expanded locally (to empty string).
Code:
[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

# 6  
Old 12-19-2011
MySQL

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

just remove ; after do keyword

i have tried the following

Code:
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.

Last edited by zxmaus; 12-19-2011 at 10:37 AM.. Reason: added code tags
# 7  
Old 12-20-2011
Success! You guys are awesome. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with shell script - filter txt file full of ips

Hello again gentlemen. I would like to make a shell script to 'optimize' a plain text full of IPs. Let's suppose to have this text file: 1.192.63.253-1.192.63.253 1.0.234.46/32 1.1.128.0/17 1.116.0.0/14 1.177.1.157-1.177.1.157 1.23.22.19 1.192.61.0-1.192.61.99 8.6.6.6 I want to... (2 Replies)
Discussion started by: accolito
2 Replies

2. UNIX for Dummies Questions & Answers

SSH on a Shell Script

Hello, I'm sure you'd have received a lot of questions like this, but I couldn't find anything relevant to my problem in the first search. Sorry in advance if this is a repeated question. I'm trying to do an SSH inside a shell script and expect the shell script to connect to a remote server... (6 Replies)
Discussion started by: mathbalaji
6 Replies

3. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

4. Shell Programming and Scripting

How to get full sql table data using shell script

i have a Oracle table like col1 col2 ---- ----- a 1 b 2 c 3 when i write a script for it , it gives me all the data in one column. Please give me the solution which gives the exact result like we see in sql editors. for a in `echo " set feedback off; set pagesize 40;... (1 Reply)
Discussion started by: ss135r
1 Replies

5. UNIX for Advanced & Expert Users

SSH using shell script terminates the script execution

Hello, I am writing a shell script in which i do ssh to remote server and count the number of files there and then exit. After the exit the shell script terminates which i believe is expected behavior. Can some one suggest me a way where even after the exit the script execution resumes. ... (2 Replies)
Discussion started by: manaankit
2 Replies

6. UNIX for Advanced & Expert Users

SSH key works from CMD line not script

OK , .. This is an odd one. I have a new server and I need to have a tunnel open to it. I have this exact process running on a few others but this new one I just got is not allowing the script to connect. I set up my users account and ssh keys from the server that will host the tunneling i... (6 Replies)
Discussion started by: jeffsandman0035
6 Replies

7. Shell Programming and Scripting

Executing remote application using ssh without full reference to its location

Hello again :) , My script has an ssh command to run a script on a remote machine. The script has commands such as sqlplus and unzip. However, the return I get in my own terminal says it can't find sqlplus and unzip. the ssh command is: ssh user@host "cd ScriptDir; ./Script.sh" and the... (6 Replies)
Discussion started by: jangozo
6 Replies

8. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

9. Shell Programming and Scripting

ssh in shell script

Hi all, I am trying to write a script in shell which can connect me to a remote server and run some commands my problem is i cant pass the password barrier.I read a lot of articles where bloggers have suggested not to use telnet and to use something more secure like ssh.I tried ssh but as soon... (8 Replies)
Discussion started by: pistachio
8 Replies

10. Shell Programming and Scripting

ssh using shell script

I need to ssh a server using shell script and run certain commands there. Please let me know how to do it . Thanks! (4 Replies)
Discussion started by: asth
4 Replies
Login or Register to Ask a Question