成功するまでscpコマンドを最大10回再試行してください。

成功するまでscpコマンドを最大10回再試行してください。

scp10回やり直して失敗した場合は、エラーメッセージを印刷したいと思います。

以下は私のコードです。

#!/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

おすすめ記事