So, I'm trying to retry failing or command-line with errors, BUT with different flags each time it retry...
I already know how to retry command on failure:
while ! "$@"
do
:
sleep 1
done
As an example, I'll use the function above on a failing command on purpose to illustrate:
retry ls test # here this will usually fail and thus retry infinitely
I'm aware this:
- Retry infinitely (until the retried command succeed or return an exit code that is interpreted by the shell as successful)
- Doesn't take into consideration any kind of errors/exit code other than failure whatsover
And all of the above is the wanted/expected effect.
Only thing I'm trying to add to that is support for additional flag when "retrying" N command (which I'll need to either supply as argument to the function or hardcode in the function depending on cases...)
Here what I tried:
bash
while ! "$(echo "$@" | sed "s/^pattern/pattern --otherflag/g")"
do
:
sleep 1
done
Usage:
retry ls pattern
Where the new command will become ls pattern --otherflag
(which will obviously fail, but this is beside the point)
2nd Method:
while ! "${@/pattern/pattern --otherflag}"
do
:
sleep 1
done
Same usage as above...
Here it will give the same result as above.
Now this seems to work on first glance, but if the supplied/failing command contain any kind of quotes, which usually come unquoted, the output/retried command will then have the quotes removed/ignored...
So following above two method/example, a command such as ls pattern "file"
will become ls pattern --otherflag file
where file
doesn't have any quotes (same happen with single quotes).
To fix this I tried this answer's quote
function but didn't manage to get satisfying result.
Furthermore, it also seems like it doesn't wait/use the added flag only on failure (eg: when the retry function kicks in) but instead run it before it even fail, with the additional flags.
I don't want to have to use a if
block/condition if possible and prefer to stay as close as possible to the above snippet. Prefer bash
or/and sh
alternative. Any feedback/answer is welcome.
while True
but preferred to "retry infinitely until success" like the original snippet did if possible :) (eg: using thewhile !
method) – Nordine Lotfi May 15 '21 at 18:50"$@"
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 toif
, since I'm not sure what to do from the third try on. But you could doif ! "$@"; then while ! "$@" --otherarg; do sleep 1; done; fi
to keep looping on the modified command line. – ilkkachu May 16 '21 at 09:41quote
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 – Nordine Lotfi May 19 '21 at 07:53retry.sh ls pattern "file name"
, then you getls
,pattern
, andfile 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 asretry.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. – ilkkachu May 19 '21 at 12:50file
or"file"
and it makes no difference. In any case, if you want to add a new command-line argument, there's no need to touch the existing words you have in the positional parameters, in$@
, just add a new one. – ilkkachu May 19 '21 at 12:54