?
Solved

unix script with rsh in loop

Posted on 2003-03-17
10
Medium Priority
?
764 Views
Last Modified: 2013-12-26
I have the following script running on AIX version 4.3.  The while loop only reads the first record of the input file and then stops.  If I remove the rsh command, the loop will process both records.  How can I get the loop to read both records and rsh to the remote server and check for their existence?

#!/bin/ksh
echo "/prod/junk1" >filelist.txt
echo "/prod/junk" >>filelist.txt
INPUT=filelist.txt
while read filenames; do
   if [ `rsh server1 "ls $filenames 2>/dev/null "  | wc -l ` != 0 ]
     then
       echo file exists
     else
       echo file does not exist
   fi
done < $INPUT
0
Comment
Question by:gwj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8154913


while read filenames; do
  if [ `rsh server1 ls $filenames "|" wc -l ` != 0 ]
    then
      echo file exists
    else
      echo file does not exist
  fi
0
 

Author Comment

by:gwj
ID: 8158742
The loop still only processes the first record and then stops.
0
 
LVL 9

Accepted Solution

by:
HamdyHassan earned 600 total points
ID: 8159151
try the following and let me know the output

while read filenames; do
  xx=`rsh server1 ls $filenames "|" wc -l `
  echo Checking $filenames returns $xx
  if [ $xx != 0 ]
  then
     echo file exists
  else
     echo file does not exist
  fi
done < $INPUT
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gwj
ID: 8159197
It processed 1 record and gave the following message:

ls: 0653-341 The file /prod/junk does not exist.
Checking /prod/junk returns 0
file does not exist

I believe that somehow the rsh command is setting some return code that is causing the while loop to think it has processed all of the records.
0
 

Author Comment

by:gwj
ID: 8159871
I found out that rsh eats up stdin resulting in a null read.  To solve this problem, I used rsh server01 -n to ignore stdin.
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8159882

yes because we use wc -l , that means counting number of lines - which in this case is THREE

(1) ls: 0653-341 The file /prod/junk does not exist.
(2) Checking /prod/junk returns 0
(3) file does not exist

then you are checking if no_lines is not zero , which in out case will be correct

let's change

if [ $xx != 1 ]
 then
    echo file does not exist
 else
    echo file exists
 fi


question, when the script failed with these three messages, did it continue and print "file exists" or it just stopped

0
 

Author Comment

by:gwj
ID: 8159883
I gave you the points for assisting.
0
 

Author Comment

by:gwj
ID: 8160041
Here is the script I ran with your latest suggestion:

cinlid01:/prod/staging$ more junk5.sh
#!/bin/ksh
echo "/prod/junk" >filelist.txt
echo "/prod/junk1" >>filelist.txt
INPUT=filelist.txt
while read filenames; do
 xx=`rsh server1 ls $filenames "|" wc -l `
 echo Checking $filenames returns $xx
 if [ $xx != 1 ]
 then
    echo file does not exist
 else
    echo file exists
 fi
done < $INPUT


It returns:

ls: 0653-341 The file /prod/junk does not exist.
Checking /prod/junk returns 0
file does not exist

and then the scripts stops.  If I take the same script as above and add the -n option, I get:

ls: 0653-341 The file /prod/junk does not exist.
Checking /prod/junk returns 0
file does not exist
Checking /prod/junk1 returns 1
file exists

which is what I was expecting.

0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8160112
that means you solved, let me where you put this -n option
0
 

Author Comment

by:gwj
ID: 8160196
xx=`rsh server1 -n ls $filenames "|" wc -l `
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question