?
Solved

unix script with rsh in loop

Posted on 2003-03-17
10
Medium Priority
?
782 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
  • 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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 

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

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

601 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