Scp

最多重試 10 次 scp 命令直到成功

  • June 27, 2021

如果失敗,我希望重試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?

$

你能幫忙更正程式碼嗎?

更改您想要的任何參數或為文件創建一個新變數。

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

引用自:https://unix.stackexchange.com/questions/655993