Need help on for loop inside ssh


 
Thread Tools Search this Thread
Operating Systems AIX Need help on for loop inside ssh
# 1  
Old 07-11-2014
Need help on for loop inside ssh

Hi,

I am having a file like,

Code:
#cat file
Jun 19 13:08
Jun 19 13:08
Jun 19 13:08
Jun 19 13:14

when I run the below comamnd locally it will work fine,

Code:
IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done

And the output is,

Code:
HI Jun 19 13:08
HI Jun 19 13:08
HI Jun 19 13:08
HI Jun 19 13:14

But when i run the same command inside ssh,

Code:
ssh root@IPaddress "IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done"

I am getting an error saying,

Code:
sh: syntax error at line 2: `Jun' unexpected

TIA

Moderator's Comments:
Mod Comment edit by bakunin: something tells me you haven't read our rules carefully enough, especially the part about using CODE-tags. I suggest you make good on this NOW!

Last edited by bakunin; 07-11-2014 at 06:18 AM..
# 2  
Old 07-11-2014
Quote:
Originally Posted by sumanthupar
when I run the below comamnd locally it will work fine,

Code:
IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done

But when i run the same command inside ssh,

Code:
ssh root@IPaddress "IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done"

Think it over: when you do a "ssh machine command", then "command" will be executed on the remote host, not your local one. If "command" is something like "for x in $(cat file) ...", then you are referring to a remote file, not a local one!

if you want to process a file locally and start ssh-sessions remotely using the content of the file you have to do the loop locally. The following sketch shows how:

Code:
while read LINE ; do
     ssh user@host "do_something_with $LINE"
done < /path/to/some/file

A second problem is your use of double quotes. You see, a shell maintains a simple switch "inside/outside a double quote" and once it encounters such a character this switch is flipped. Therefore double quotes cannot be nested. You will have to escape the inner pair to make your command work:

Code:
ssh root@IPaddress "IFS=$'\n'; for i in $(cat file) ;do echo \"HI $i\" ; done"

(which will - as explained above - still not work, but for a different reason)

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

3. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

4. Shell Programming and Scripting

variables inside an ssh session

Hello all, I would like to declare and use variables inside an ssh session. I have the feeling that it's not possible. Here is the situtation simpified: #:/bin/sh test="salut" echo $test ssh hudson@10.41.21.99 <<EOF export testssh="salut" echo testssh=$testssh ... (4 Replies)
Discussion started by: Lotfus
4 Replies

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

7. Shell Programming and Scripting

ssh inside a for loop

Hi, The requirement is to ssh to unix servers and oracle databases, to perform some monitoring activity. I'm using shell script to perfom this. I pass the server details and database to a variable ... SERVERS="SERVER1 SERVER2 SERVER3" DATABASE="DB1 DB2 DB3" for i in $SERVERS do ssh... (2 Replies)
Discussion started by: senthil3d
2 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question