While loop not reading all files if ssh fails


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop not reading all files if ssh fails
# 1  
Old 08-14-2013
While loop not reading all files if ssh fails

The below while loop is in ksh on a SunOs server: SPARC-Enterprise 5.10
The ksh version is: Version M-11/16/88i

The intention of the below while loop is to read through a list of file names in files.txt and
delete each file from a server, one at a time. The delete works, the problem is that if a file
does not exist the loop exits instead of issuing the read for the next file. The first file name
in files.txt does not exist, purposely to allow testing the routine for file not present.

Interestingly if the ssh cmd is removed and replaced with the execution of a non-existent
script the loop does not exit, it reads all files in files.txt. Why does the loop exit when ssh
tries to delete a file not on the server? I have tried various versions of trap with no success.
There is no break or exit in the script, which contains only the while loop.
Thanks, Lee
Code:
#!/bin/ksh
USAGE=”usage: test.ksh”

Code:
while read InputRecord
do
  echo ‘Start’   This displays only once
 
  ssh  [this cmd issues a rm of a file from files.txt, this works] > $LOG/file.delete.log   The return code is “1” for file not found.
 
  <A rtn which greps file.delete.log for file not found and if so logs this fact.  This works>
 
  echo ‘** got here’  This simply tells me that after the first error control gets here and then 
  exits the loop.
done < $LOG/files.txt


Last edited by Scott; 08-14-2013 at 04:26 PM.. Reason: Removed formatting
# 2  
Old 08-14-2013
why not paste the actual code? perhaps your pseudo-code is missing something important, like "set -e"
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 08-14-2013
Here is the code.
Code:
!/bin/ksh
#set -xv
USAGE="usage: test2.ksh"
 
# Set the environment variables
. /bill/adc/env.vars
. /bill/common/host/set.host.bat
 
# Set env vars
AdcIP=`grep ADCIPadr $DAY_DIR/ADC.Web.Server.cfg | awk '{ print $2}'`
DataDir=`grep ADCdataDir $DAY_DIR/ADC.Web.Server.cfg | awk '{ print $2}'`
ADCuid=`grep ADCuid $DAY_DIR/ADC.Web.Server.cfg | awk '{ print $2}'`
 
#--------------------------------------------------------------------------------------------------#
# DELETE the billing files from the web server                                                   #
#--------------------------------------------------------------------------------------------------#
 
echo `date` '    Start of delete routine' >> $AUD_DIR/app.log
 
while read InputRecord
do
   echo `date` '      Deleting file..............................: ' $InputRecord >> $AUD_DIR/app.log
 
   ssh -i $SSH_DIR/<ssh key> -b $SrcIP $ADCuid@$AdcIP "rm $DataDir/$InputRecord" > $LOG_DAY/delete.adc.bill.files.log 2>&1
 
   echo 'RC: ' $?
 
   echo 'IR: ' $InputRecord
done < $LOG_DAY/bill.files.pulled.lst


Last edited by Scott; 08-14-2013 at 05:33 PM.. Reason: Removed formatting. Please do not paste code from rich-text editors
# 4  
Old 08-20-2013
BUMP - Interested in knowing the answer to this
# 5  
Old 08-20-2013
Through a LOT of trial and error and researching on the net I came across the below while loop
syntax just a little while ago, the first example below. I changed my while loop to match and it
works yet I do not understand why it works. The echo of $? after each ssh “rm” failure is 1 as
it should be because the files do not exit. I am testing the error condition for when a file does not
exist. I would like to understand the reason the syntax works. The previous while loop after the
first file rm failure had control go back to the read and then the script stopped; I used “set -xv”.
It was as if the “rm” failure triggered an EOF.

Code:
while IFS= read -r f <&3;
do
     #do something with "$f"
     rm -f "$f"
done 3< delete.txt
 
while IFS= read -r FileName <&3;
do
       echo `date` '      Deleting file..............................: ' $FileName >> $AUD_DIR/app.log
 
       ssh -i $SSH_DIR/m5kdom6.adc.webserver.pri -b $SrcIP $ADCuid@$AdcIP "rm $DataDir/$FileName" > $LOG_DAY/delete.adc.bill.files.log 2>&1
 
       echo 'RC: ' $?
done 3< $LOG_DAY/bill.files.pulled.lst

# 6  
Old 08-20-2013
When you redirect into your while loop with <, you are redirecting stdin from that file.

ssh, an interactive application, tries to use stdin as your keyboard, eating all your lines.

Use ssh -n to avoid this problem.
These 2 Users Gave Thanks to Corona688 For This Post:
# 7  
Old 08-20-2013
I have successfully run the script in the original form with the -n parameter. I have also read more about it on the net. I appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ssh fails from one server only with expecting SSH2_MSG_KEXDH_REPLY message

I have two linux servers viz 12.7.44.18 and 12.7.45.18 I wish to ssh from both these server to a destination AiX server 12.7.33.18 The ssh works from 12.7.44.18 -> 12.7.33.18 but fails from 12.7.45.18 -> 12.7.33.18 The openssl version on both linux source 12.7.44.18 and 12.7.45.18 is the... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

Ssh fails for one of two users on the same server.

I have two users on dest_host server viz wlsadm & wasadm. From src_host server with root user I m able to passwordless login to wlsadm@dest_host however, the same fails for wasadm@dest_host Below is the debug for both first the working ssh and then the non-working ssh. Works: ... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. Shell Programming and Scripting

Ssh fails due to argument position.

I have a constraint to follow organization policy. So i do not have much liberty. ssh -i /opt/nonprod user1@hostone -t bash works while ssh -i /opt/nonprod -t bash user1@hostone fails How can I get this to work when I am enforced to put -t bash before the user@hostname ? Will share debug... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

Windows exe file fails when triggered from ssh

Hi, I am triggering a windows exe file using the below command. ssh user@remoteserver command.exe -option1:xx /option2:yy This command is working fine from windows command prompt. When I am triggering the same command from ssh I get the error message cant load Any ideas to deal with... (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

5. Ubuntu

Passwordless ssh authentication fails

Unable to set ssh passwordless authentication I am unable to ssh with passwordless authentication from Windows client onto UBuntu server. The ssh version on UBuntu is OpenSSH_5.8p1 Debian-7ubuntu1, OpenSSL 1.0.0e , while SSH on Windows Client is OpenSSH_5.1p1, OpenSSL 0.9.8k. I turned on ssh... (5 Replies)
Discussion started by: tkota
5 Replies

6. Shell Programming and Scripting

Need Help with expect when ssh fails

I'm trying to write a script using expect. I'd like the script to execute several commands when the ssh succeeds and i want it to exit if the ssh fails. Does this require to define a time out for the ssh command so that if the prompt is back before this defined time the next commands are executed??... (2 Replies)
Discussion started by: Hossam_Nox
2 Replies

7. Shell Programming and Scripting

until loop fails.

i have a script like below, i am trying to print "...." until a background process that was executed before is running,, the below does work and teh loop keeps on printing "..", can someone help where i am wrong. /tmp/test/abc.sh & until do echo "..\c" done abc.sh is the scipt... (8 Replies)
Discussion started by: yesmani
8 Replies

8. Shell Programming and Scripting

unzip command fails in ssh

I'm trying to run a set of commands on a remote machine using ssh in a shell script. One of the commands is unzip. But when the execution reaches this command, the script fails with an error that unzip is not found. Below is the code and the error snippet. sourceDir=$1 ; filename=$3 ; destDir=$2... (4 Replies)
Discussion started by: farahzaiba
4 Replies

9. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

10. Shell Programming and Scripting

reading from 2 files through while loop

hi i have two files cat input.txt 123456| 43256 456482|5893242 cat data.txt xv 123456 abcd dsk sd 123456 afsfn dd df 43256 asdf ff ss 456482 aa sf 5893242 ff ff aa 5893242 aa aa i need to read inputs from input.txt and find data for data.txt. then i need to print them as a... (2 Replies)
Discussion started by: windows
2 Replies
Login or Register to Ask a Question