스크립트에서 올바른 종료 코드를 가져올 수 없습니다.

스크립트에서 올바른 종료 코드를 가져올 수 없습니다.

SFTP를 통해 원격 서버에 연결하고 거기에서 일부 파일을 가져오는 스크립트가 있습니다. 내 스크립트는 다음과 같습니다

/usr/bin/sftp [email protected] <<EOF
lcd /dir1/dir2/dir3
cd /rsdir1/rsdir2/rsdir3
get file_pattern`date -d "last month" +%m%Y`.csv
EOF
rc=$?
        if [[ $rc != 0 ]]
           then
        echo "Error occured getting file and the script abended with error code $rc" `date "+%Y-%m-%d-%H.%M.%S"`
            exit 1
    else
    echo "Successfully transferred the file" `date "+%Y-%m-%d-%H.%M.%S"`
        fi

그러나 스크립트가 해당 패턴의 파일을 찾을 수 없는 경우에도기타스크립트의 일부를 작성하고 화면에 출력을 제공합니다.

Connecting to remote.server.com...
sftp> lcd /dir1/dir2/dir3
sftp> cd /rsdir1/rsdir2/rsdir3
sftp> get file_pattern032014.csv
Couldn't stat remote file: No such file or directory
File "/rsdir1/rsdir2/rsdir3/file_pattern032014.csv" not found.
Successfully transferred the file YYYY-MM-DD-24HH.MI.SS 

내가 여기서 뭘 잘못하고 있는지에 대한 제안이 있습니까?

답변1

올바른 반환 코드를 얻었고 sftp 세션이 올바르게 실행되었으므로 반환 코드는 0입니다. 이것을
사용해야 합니다 scp. 복사가 실패하면 0을 반환하지 않습니다.

다음을 수행할 수 있습니다.

file=file_pattern`date -d "last month" +%m%Y`.csv 
[email protected]:/rsdir1/rsdir2/rsdir3/$file
local=/rsdir1/rsdir2/rsdir3/$file

if scp -q $remote $local
then
    echo "Successfully transferred the file" `date "+%Y-%m-%d-%H.%M.%S"`
else
    echo "Error occured getting file and the script abended with error code $?" `date "+%Y-%m-%d-%H.%M.%S"`
    exit 1
fi

편집: 복사 대상을 파일 이름으로 변경했습니다. 디렉터리에 복사했는데 해당 디렉터리가 없으면 해당 디렉터리 이름으로 파일을 만듭니다.

답변2

다음을 시도해 보십시오:

/usr/bin/sftp -b - [email protected] <<EOF ...

"-b -"는 명령줄에서 읽는 동안 sftp를 배치 모드로 전환합니다. sftp 명령 중 하나가 실패하면 배치 모드는 (내 시스템에서) 0이 아닌 종료 코드로 sftp를 종료합니다.

관련 정보