awk, double variable, for loop and rsh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk, double variable, for loop and rsh
# 1  
Old 05-03-2011
awk, double variable, for loop and rsh

Hello folks,
I've a (perhaps) simple question.

In a text file I've :
Code:
server_name1: directory1
server_name2: directory2
server_name3: directory3
[...]

I want to make a loop that lets me connect and operate on every server:
Code:
rsh server_name1 "ls -l directory1"

I've tried with awk, but does not work (it doesn't export variables):
Code:
for n in `awk -F: {print $1}' text`
do 
   for m in `awk -F: {print $2}' text`
   do
      rsh $n "ls -l /$m"
   done
done

Any idea?

Thanks all
gb

Last edited by pludi; 05-03-2011 at 06:09 AM..
# 2  
Old 05-03-2011
mmm... that double loop is gonna call ls on each server for each directory; You probably want something like this:
Code:
while read s d ; do 
     rsh ${s%:} "ls -l $d"
done < text

The ${s%:} will get rid of the trailing colon.
# 3  
Old 05-03-2011
thanks,
but this way it reads only first entry, not all.

It executes only:
rsh server_name1 "ls -l dir1"
# 4  
Old 05-03-2011
Strange... do those directories on servers exist? Does the remote connection happen at all? Try rsh -v to switch on verbose mode.
# 5  
Old 05-03-2011
it does not work at all now, cause it put cmd in bg before the read has been done... :S

Code:
> jobs
[3] +  Running                 while read n m ; do;remsh ${m%:} "ls -d /$n" &;done < text
[2] -  Running                 while read n m ; do;remsh ${m%:} "ls -d /$n" &;done < text
[1]    Running                 while read n m ; do;remsh ${m%:} "ls -d /$n" &;done < text

---------- Post updated at 11:55 AM ---------- Previous update was at 11:52 AM ----------

Quote:
Originally Posted by mirni
Strange... do those directories on servers exist? Does the remote connection happen at all? Try rsh -v to switch on verbose mode.
yes, they exists, and I've to use remsh, so no verbose mode possible.

---------- Post updated at 12:10 PM ---------- Previous update was at 11:55 AM ----------

arf... probably I've mistaped something the first time I execute my script, caus it works (not fine, cause it search everytime on everyserver everyline...)

so, this works (not fine):
Code:
for n in `awk -F: '{print $1}' text`; do
for m in `awk -F: '{print $2}' text`; do
remsh $n "ls -d /$m"
done
done

I prefer this one (works fine):
Code:
for n in `cat text`; do
ser = `echo $n | awk -F: '{print $1}'`
dir  = `echo $n | awk -F: '{print $2}'`
remsh $ser "ls -d /$dir"
done

thank you mirni (and I don't understand why your while doesn't work)
# 6  
Old 05-03-2011
You got this mixed:
Quote:
while read n m ; do;remsh ${m%:} "ls -d /$n" &;done < text
From your sample input, server was first, so the correct would be
Code:
while read n m ; do;remsh ${n%:} "ls -d /$m" ; done < text

# 7  
Old 05-03-2011
You can also try something like
Code:
awk -F": " ' { print "remsh "$1" \"ls -l /"$2"\"" | "sh" } '

Regards
Peasant.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

2. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

3. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

4. Shell Programming and Scripting

Cannot pass rsh and awk command into a variable

Hello, I'm having some issues getting a home dir from a remote server passed to a variable. Here is what I have so far: rsh server "(ls -ld /home*/user | awk '{print \$9}')" /home3/userThat works fine and brings back what I need. But when I try to add it to a variable it goes all... (3 Replies)
Discussion started by: elcounto
3 Replies

5. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

6. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

7. Shell Programming and Scripting

using awk in a for loop (getting ksh variable)

Hi, I've tried searching the forums for a case similar to mine but was unsuccessful. This is my first time to use awk so any help would be really appreciated :) I have one file containing data for with the first value in each row being a State Name. I would need to create a separate file... (1 Reply)
Discussion started by: karver
1 Replies

8. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies

9. Shell Programming and Scripting

rsh breaking me out of loop

Hey all I have two scripts, one script containing the guts of my code. The other simply loops through a list, calling the other script on each iteration. Problem is when I add the line `/usr/bin/rsh -l root $HOSTNAME ""` to my main script, the loop never seems to exectute any more... (1 Reply)
Discussion started by: mark007
1 Replies

10. Shell Programming and Scripting

rsh and for loop

hi I wanted to use the for loop structure in tandem with rsh command and the result to be redirected into a local .lst file within a shell script . Tried the following but does not help :confused: . rsh ABCD "cd /bosp/local/home/linus/;for i in `ls -ltr | grep ^- | awk {'print $9'}` do... (4 Replies)
Discussion started by: newbee2005
4 Replies
Login or Register to Ask a Question