1

This is a 2 part question.

Scenario: This script is on a cronjob. If a folder does not exist, the system sends us an email that opens a ticket which notifies us that the folder is not available. We manually have to log in and remove the preceding folder atm.

I would like for us to be able to execute the script manually and remove the preceding folder by pressing "Y" or to continue by pressing the "Enter" key, while we are logged in and execute the script manually.

This is what I have so far ...

   #-- check to see if cache folder exists
   { echo "Checking to see if ...";
   echo "${wDir}/${client%/}/.ftp-vendor-scripts/cache exists ... "; echo ""; } >> "$log"

   if [ ! -d "${wDir}"/"${client%/}"/.ftp-vendor-scripts/cache ]; then
      echo "Directory - ${wDir}/${client%/}/.ftp-vendor-scripts/cache DOES NOT exists - Failed ..." >> "$log";

      if [ ******** this script is being executed manually ******* ]; then
         echo "Would you like to delete the ${wDir}/${client%/}/.ftp-vendor-scripts folder?"
         echo "Press \"Y\" to delete the ${wDir}/${client%/}/.ftp-vendor-scripts."
         echo "Press \"Enter\" to continue without deleting the .ftp-vendor-scripts folder."
      else
         echo "Directory - ${wDir}/${client%/}/.ftp-vendor-scripts/cache DOES NOT exists - Failed ..." | mail -s "${wDir}/${client%/}/.ftp-vendor-scripts/ca$
      fi

   else
      echo "Directory - ${wDir}/${client%/}/.ftp-vendor-scripts/cache exists - Success ..." >> "$log";
   fi
1

You need something like this:

#!/usr/bin/env sh

if [ -t 1 ]
then
    interactive=1
else
    interactive=0
fi


if [ "$interactive" -eq 1 ]
then
    printf "interactive\n"

    while true
    do
    printf "Rm directory? "
    read -r reply
    if [ "$reply" = "y" ]
    then
            printf "directory will be removed\n"
            break
    elif [ "$reply" = "n" ]
    then
            printf "directory will not be removed\n"
            break
    else
            printf "Uknown reply - it must be either y or n\n"
    fi
    done

else
    printf "non interactive\n"
fi

The above script is POSIX-compliant and checked for errors with shellcheck. It will check if it runs in interactive or non interactive mode, possibly via cron and will act accordingly. I've tested it with bash, dash, Busybox ash and FreeBSD.

  • Can you adjust your script and place if [ -t 1 ]; then interactive="true"; fi at the top of the script so I can use it throughout the script? I think this is going to do us just fine. – needtoknow Apr 2 '18 at 19:22
  • I modified the script. – Arkadiusz Drabczyk Apr 2 '18 at 19:25
  • Hats off to you mate. I appreciatew it. – needtoknow Apr 2 '18 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.