스크립트를 수동으로 실행하는 경우 Y를 입력하여 삭제합니다.

스크립트를 수동으로 실행하는 경우 Y를 입력하여 삭제합니다.

이것은 2부분으로 구성된 질문입니다.

시나리오: 스크립트가 cronjob에 있습니다. 폴더가 없으면 시스템에서 이메일을 보내고 폴더를 사용할 수 없음을 알리는 티켓을 엽니다. 수동으로 로그인하여 이전 폴더 atm을 삭제해야 합니다.

수동으로 스크립트를 실행하고 "Y"를 눌러 이전 폴더를 삭제하거나, 로그인한 상태에서 계속하려면 "Enter"를 눌러 스크립트를 수동으로 실행할 수 있기를 바랍니다.

이것이 내가 지금까지 가지고 있는 것입니다...

   #-- 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

다음과 같은 것이 필요합니다.

#!/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

위의 스크립트는 POSIX호환 가능하며 사용됩니다 . 또는 모드 shellcheck에서 실행 중인지 확인하고 그에 따라 작동할 수 있습니다. 나는 그것을 , 및 으로 테스트 했습니다 .interactivenon interactivecronbashdashBusybox ashFreeBSD

관련 정보