Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • It's a great answer, (and I appreciate the detailed info too) but it seems it didn't kept the "retry infinitely" part of the original snippet? I know i could use a while True but preferred to "retry infinitely until success" like the original snippet did if possible :) (eg: using the while ! method) Commented May 15, 2021 at 18:50
  • Also is there really no easy way to keep the quoting done on certain existing arguments? This is the main thing i struggled with, since adding a flag at the end using the original snippet in my post is easier (as you mentioned too) Commented May 15, 2021 at 18:51
  • @NordineLotfi, err, I'm not sure what you mean. The way to keep the arguments intact is to use "$@" or, for one or more args, but not the whole list, something like "$3" or "${@:2:3}". There are no quotes at this point. And yes, I switched to if, since I'm not sure what to do from the third try on. But you could do if ! "$@"; then while ! "$@" --otherarg; do sleep 1; done; fi to keep looping on the modified command line. Commented May 16, 2021 at 9:41
  • I meant to keep the specific quote that could be placed (as the example used in my post) on the command passed to the retry function. I know how to escape them (as mentioned and linked to a solution that do this in my post) but don't know how to keep them intact...my guess is that placing single quote around double quote would work but not sure how to do this Commented May 19, 2021 at 7:53
  • @NordineLotfi, if you call a script on the shell command line as retry.sh ls pattern "file name", then you get ls, pattern, and file name in $1, $2 and $3. "$@" expands to those three words, the same as if the script contained "$1" "$2" "$3" (except that "$@" doesn't require knowing the number of arguments). If you call the script as retry.sh ls pattern file\ name, you get the same. The quotes and escapes are removed when the command line is processed, so when you're in the script, you don't have any quotes to deal with, but individual positional parameters. Commented May 19, 2021 at 12:50