파일 수정 날짜를 확인하는 데 사용된 스크립트의 구문 오류

파일 수정 날짜를 확인하는 데 사용된 스크립트의 구문 오류

그래서 실행 시 파일을 수정하거나 메시지를 에코할 수 있는 스크립트를 셸에서 만들고 싶습니다.

이것이 내가 쓴 것입니다:

#!/bin/bash

current=$(date +%s)
last_modified='stat -c "Y" $/home/userr/textfile'

if
[ $((current-last_modified)) -gt 120 ]; then
        touch /home/userr/textfile;
else
        echo "File was modified less than 2 minutes ago";
fi

ShellCheck에서는 모든 것이 괜찮다고 하는데 실행하려고 하면 다음과 같이 표시됩니다. stat -c "Y" $/home/userr/textfile: syntax error: invalid arithmetic operator (error token is ""Y" $/home/userr/textfile")

내가 어디로 잘못 가고 있는지 아시나요? 미리 감사드립니다!

답변1

현재 문제는 다음 줄일 수 있습니다.

last_modified='stat -c "Y" $/home/userr/textfile'

문자열이 stat -C "Y" ...변수에 할당됩니다. 아마도 당신이 원하는 것은 명령의 출력을 할당하는 것인데, stat -c %Y /home/userr/textfile다음과 같이 작성할 수 있습니다:

last_modified="$(stat -c %Y /home/userr/textfile)"

오류가 감지되지 않는 일이 없도록 빠른 실패 동작이 활성화된 #!/bin/sh -e모든 쉘 스크립트(여기서는 bash가 필요하지 않습니다 :))를 권장합니다 .-e

관련 정보