1

I wish to retry scp 10 times and print an error message if it failed.

Below is my code:

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

Unfortunately, I get the below error:

npm-debug.log
./test.sh: line 8: system: command not found

Below Is detailed output upon @roaima's suggestion

$ 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?

$

Can you please help correct the code?

6
  • What shell did you write this for? Where is your initial first line #! marker to identify the interpreter? Please paste it into shellcheck.net to find obvious errors Commented Jun 27, 2021 at 8:07
  • I have updated the original post.
    – Ashar
    Commented Jun 27, 2021 at 8:15
  • To my knowledge, bash doesn't have a system command, and none is needed: Just put the command directly after while (no parenthesis), but don't forget to negate the condition (commands return 0 on "good" outcomes).
    – dirkt
    Commented Jun 27, 2021 at 8:24
  • Please paste your code into shellcheck.net to find obvious errors. Fix those, or ask about them here Commented Jun 27, 2021 at 8:26
  • @roaima @dirkt i updated the original post with shellcheck.net debug output.
    – Ashar
    Commented Jun 27, 2021 at 9:12

2 Answers 2

1

Change whatever parameters you want or make a new variable for the file.

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

The retry tool will do this. As requested, we try 10 times before giving up.

By default, we retry forever.

~$ 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

Available out the box in recent Debian, Ubuntu, and Nix.

(Disclaimer, I am the original author of this tool)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .