0

I want to check for file in directory if there then push it to ssh server checing server connection if file not there then try 3 times with each 1min interval and in between if it comes ( on 2nd attend for example) then try again to connect ssh and push. else check for 3 attempts and exit

Please check my below code it is halting after 1st attempt ( during 2nd attempt I am making file available)

#!/bin/sh
echo "OK, start pushing the Userdetails to  COUPA now..."
cd /usr/App/ss/outbound/usrdtl/
n=0


      until [ $n -ge 3 ] || [ ! -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ]
      do 
      if [ -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ] ;  
      then 
      pushFiles()
      else
      n=$[$n+1]
      sleep 60
      echo " trying " $n "times " 
      fi
      done

pushFiles()
{
echo "File present Now try SSH connection"
while [ $? -eq 0 ];
do
    echo $(date);
     scpg3 -v /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv <sshHost>:/Incoming/Users/
     if [ $? -eq 0 ]; then
        echo "Successfull" 
        echo $(date);
        echo "Successfull" >> /usr/App/ss/UserApproverDetails.log
        exit 1;
        else
            echo $(date);
            echo "Failed" >> /usr/App/ss/UserApproverDetails.log
            echo "trying again to push file.."
            scpg3 -v /usr/App/sg/outbound/usrdtl/USERS_APPROVERS_*.csv <ssh Host>:/Incoming/Users/
            echo $(date);   
        exit 1;
    fi
done
}
Sanvi
  • 75
  • 2
  • 9
  • There are several things I don't understand about this code, it seems like you check `[ -f ${File} ] both in the `until` and then immediately again in an `if`. And then below, you wrap everything in a `while [ $? -eq 0 ]` but you have and if ` [ $? -eq 0 ] ` inside it. – JawguyChooser Jan 12 '18 at 06:02
  • yes I want to check multiple condition in loop so there the logic placing is missing. one is want to check file is there or not if there push it to ssh host else try 3 times if in between file came again push it to ssh host no need to try 3 times full. For ssh connection also same try 3 times if connection not available leave it if available in between 2 nd attempt then push file and exit no need to try 3rd time. – Sanvi Jan 12 '18 at 06:10
  • I will write you a much simpler starting point, standby. – JawguyChooser Jan 12 '18 at 06:13
  • See my suggestions below for a cleaned up starting point. You don't need to have an `if` check inside your `while` loop. You don't need to `echo $(date)` (the default behaviour is to print to stdout). You ought to use variable names to shorten long file paths for readability and to avoid bugs. You need to use proper indentation. – JawguyChooser Jan 12 '18 at 06:39
  • `[ ! -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ]` will always fail (with an error) if more than 1 `.csv` file is present. `[ -f "file" ]` can only check for a *single* file. It will aso be an error if no files are present. – David C. Rankin Jan 12 '18 at 06:41
  • @DavidC.Rankin right! That's why I suggested to actually use nullglob to test the glob in a subshell. Also, Sanvi, exit 0 traditionally indicates success. Any non-zero exit indicates an error code. – JawguyChooser Jan 12 '18 at 18:01

1 Answers1

0

I've tried to simplify this code for you. I hope it helps:

#!/bin/bash

outdir="/usr/App/ss/outbound/usrdtl"
logfile="/usr/App/ss/UserApproverDetails.log"
file_prefix="USERS_APPROVERS_"

function push_files() {
    echo "File present now try SSH connection"
    local attempts=1
    local retries=2
    date

    while [[ ${attempts} -lt ${retries} ]]; do
        if scp ${outdir}/${file_prefix}*.csv <sshHost>:/Incoming/Users/ ; then

            echo "Successful" | tee -a ${logfile}
            date
            exit 0
        else
            echo "Failed" >> ${logfile}
        fi
        attempts=$((attempts+1))
    do
    echo "scp failed twice" | tee -a ${logfile}
    exit 2
}

echo "OK, start pushing the Userdetails to  COUPA now..."
cd ${outdir}
attempts=1
retries=3
while [[ ${attempts} -lt ${retries} ]]; do
    echo "looking for files...attempt ${attempts}"
    if test -n "$(shopt -s nullglob; echo ${outdir}/${file_prefix}*.csv)"; then
        push_files()
    fi
    attempts=$((attempts+1))
    sleep 60
done
echo "Files were never found" | tee -a ${logfile}
exit 1

Look at this code and tell me how it's not doing what you're trying to do. The most complicated part here is the nullglob stuff, which is a handy trick to see if any file in a glob matches

Also, I generally used bashisms.

JawguyChooser
  • 1,816
  • 1
  • 18
  • 32
  • I didnot understand shopt -s nullglob .. instead can we add something in while loop logic to accommodate my requirement ? I dont want to try any new commands please – Sanvi Jan 12 '18 at 07:02
  • I think what you're trying to do is see if any files match that glob pattern, is that right? Setting nullglob in a subshell lets you test the glob expansion without changing the default glob behavior in the main shell. – JawguyChooser Jan 12 '18 at 07:26
  • ok working now instead of nullglob I added normal if !! if [ -f /usr/App/ss/outbound/usrdtl/USERS_APPROVERS_*.csv ] ; I know it looks normal code but since I understand this logic better so I can explain or change it in future so I added the same.. thanks – Sanvi Jan 12 '18 at 08:00
  • The reason I was trying something funny instead of just doing `-f` there is that the result of that glob expansion can be a list of files separated by spaces. I suppose that -f ends up evaluating the first of these files when there's more than one (it's not clear to me), that's why I wanted to write something to take into account the glob. – JawguyChooser Jan 12 '18 at 15:36