쉘 스크립트의 else 근처에 구문 오류가 있습니다.

쉘 스크립트의 else 근처에 구문 오류가 있습니다.

아래 코드를 시도 중입니다. 확인하려고 하므로 아래 언급된 디렉토리에 두 개의 파일이 있고 while 루프를 완전히 제거하고 싶습니다! 하지만 아직 파일을 받지 못했다면 계속 반복하고 기다려야 합니다. 하지만 아래 코드를 사용하면 다른 부분에서 오류가 발생합니다. 오류는 다음과 같습니다

syntax error near unexpected token else

문제를 해결하려면 어떻게 해야 하나요? 또 다른 질문은 Visual Studio Code에서 셸 스크립트의 형식을 지정하는 방법입니다. VSC에서는 이에 대한 확장을 찾을 수 없습니다.

day_of_month=1
export filesdir=/dir1/dir2/dir3
local count=0
numFilesReceived=0
while true; do
        files=$(find $filesdir -name '*.txt.gz' -type f -mmin -1)
        if [ "$day_of_month" == "1" ]; then
            if [ -f "$files" ]; then
                count=$((count + 1))
                break
                if [ "$numFilesReceived" == "$count" ]; then
                    echo "All $count data received!"
                    break 3
                fi
            fi
            else
                echo "No data received yet!" 
            fi
            fi
             else
            rm $files
        fi
       done

답변1

if/else의 균형이 맞지 않아 오류가 발생합니다. sh 스크립트의 if/else 구문은 다음과 같습니다.

if condition 
then
    do something
else
    do something else
fi

각각은 if하나로 닫혀야 합니다 fi. 다음으로 모든 파일을 단일 문자열 변수에 저장합니다.

files=$(find $filesdir -name '*.txt.gz' -type f -mmin -1)

이는 find명령이 다음과 같은 것을 반환하는 경우를 의미합니다.

$ find . -name '*.txt.gz' -type f
./file2.txt.gz
./file1.txt.gz

그러면 변수의 내용은 다음과 같습니다.

$ files=$(find . -name '*.txt.gz' -type f)
$ echo "$files"
./file2.txt.gz
./file1.txt.gz

그러나 해당 이름을 가진 파일이 존재하는지 확인합니다.

if [ -f "$files" ]; then

리터럴 이름을 가진 파일이 없기 때문에 이는 결코 사실이 아니며 ./file2.txt.gz\n./file1.txt.gz, 그렇게 했더라도 결과에 포함됩니다. 그럼에도 불구하고, 여러분은 그것이 파일이고 존재한다는 것을 이미 알고 있습니다. find명령이 수행하는 작업이기 때문에 이 테스트가 더욱 불필요해집니다.

또한 항상 사실이기 때문에 설정 day_of_month=1하고 사용할 필요가 없는 불필요한 변수가 많이 있습니다 . 및 if [ "$day_of_month" == "1" ]에도 마찬가지입니다 . 이 두 가지로 무엇을 하려는지 이해할 수 없으므로 0이 아닌 값으로 설정한 다음 파일 수가 해당 값과 일치하면 종료하려는 것 같습니다. 이것도 요점을 모르겠습니다 . 매월 1일이라면 파일을 삭제하고 싶은 것 같습니다. 그렇다면 현재 날짜도 확인하고 싶을 것입니다.numFilesReceivedcountcountday_of_month

당신이 원하는 것에 대한 최선의 추측의 실제 버전은 다음과 같습니다.

#!/bin/bash

filesdir=/dir1/dir2/dir3
expectedFiles=2
dayToDelete=1

## Get the current day of the month
day_of_month=$(date '+%d')

while true; do
  ## If this is not the 1st day of the month, just delete the files
  ## and exit
  if [ "$day_of_month" != "1" ]; then
    find "$filesdir" -name '*.txt.gz' -type f -mmin -1 -delete
    exit
  ## If this is the first day of the month
  else
    ## You don't need the file names, only their number,
    ## so just print a dot so you don't need to worry about
    ## whitespace in the names
    fileCount=$(find "$filesdir" -name '*.txt.gz' -type f -mmin -1 -printf '.\n' | wc -l)
    if [[ "$fileCount" == "$expectedFiles" ]]; then
      echo "All $expectedFiles files received!"
      exit 
    else
      echo "No data received yet!"
      ## Wait for 5 seconds. No point in spamming, in fact you
      ## probably want to sleep for longer.
      sleep 5
    fi
  fi
done

이것이 실제로 필요한 것은 아닐 것 같지만 이것이 출발점이 되기를 바랍니다. 도움이 더 필요한 경우 새로운 질문을 해주세요. 하지만 더 나은 도움을 드릴 수 있도록 스크립트가 수행해야 하는 작업에 대한 세부 정보를 제공해 주시기 바랍니다.

답변2

3개의 "if" 문과 5개의 "fi" 문이 있습니다.

그것은 다른 것이 아닙니다. 단지 다른 것입니다.

관련 정보