スクリプトから正しい終了コードを取得できません。

スクリプトから正しい終了コードを取得できません。

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

編集:コピー先をファイル名に変更しました。ディレクトリにコピーし、そのディレクトリがない場合は、そのディレクトリ名でファイルを作成します。

おすすめ記事