scp
10回やり直して失敗した場合は、エラーメッセージを印刷したいと思います。
以下は私のコードです。
#!/bin/bash
FILE=$1;
echo $FILE;
HOMEDIR="/home/ibro";
tries=0;
while (system "scp -P 3337 $FILE ibrahimince\@localhost:$HOMEDIR/Printed/")
do
last if $tries++ > 10;
sleep 3;
done
if [ $? -eq 0 ]; then
echo "SCP was successful"
else
echo " SCP failed"
fi
残念ながら、次のエラーが発生します。
npm-debug.log
./test.sh: line 8: system: command not found
以下は@roaimaの提案に基づく詳細な出力です。
$ shellcheck myscript
Line 3:
echo $FILE;
^-- SC2086: Double quote to prevent globbing and word splitting.
Did you mean: (apply this, apply all SC2086)
echo "$FILE";
Line 9:
last if $tries++ > 10;
^-- SC2210: This is a file redirection. Was it supposed to be a comparison or fd operation?
$
コードを編集するのに役立ちますか?
答え1
必要なパラメータを変更するか、ファイルの新しい変数を生成します。
#!/bin/bash
# Trap interrupts and exit instead of continuing the loop
trap "echo Exited!; exit;" SIGINT SIGTERM
MAX_RETRIES=10
i=0
# Set the initial return value to failure
false
while [ $? -ne 0 -a $i -lt $MAX_RETRIES ]
do
i=$(($i+1))
scp -P 3337 my_local_file.txt user@host:/remote_dir/
done
if [ $i -eq $MAX_RETRIES ]
then
echo "Hit maximum number of retries, ending."
fi
答え2
このretry
ツールはこれを行います。必要に応じて放棄するまで10回試しました。
基本的に、私たちは常に再試行します。
~$ retry --times 10 -- scp -P 3337 $FILE ibrahimince@localhost:$HOMEDIR/Printed/
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
~$
https://github.com/minfrin/retry
最新のDebian、Ubuntu、Nixですぐに使えます。
(免責事項、私はこのツールの原作者です)