0

I am running the following command:

OLDIFS=$IFS
IFS=$'\n'
for i in $(find $HOME/test -maxdepth 1 -type f); do
    if [ $? -eq 0 ]; then
        telegram-upload --to 12345 --directories recursive --large-files split --caption '{file_name}' $i &&
            rm $i
    fi
done
IFS=$OLDIFS

If the telegram upload command exits with a non zero code I intend to do the following:

rm somefile && wget someurl && rerun the whole command

How do I go about doing something like this?

Sachin
  • 1,217
  • 2
  • 11
  • 31
  • 1
    The exit status of a sequence of commands is the status of the last command. `IFS=$OLDIFS` can't exit with a non-zero code. – Barmar Jun 28 '22 at 04:31
  • 1
    Oh my bad i wasn't aware of that , i am concerned with the exit status of the telegram upload command .lemme edit the question – Sachin Jun 28 '22 at 04:33
  • 1
    What command's exit status are you trying to test with `if [ $? -eq 0 ]`? Nothing has been done yet except the `find` command. – Barmar Jun 28 '22 at 04:33
  • 3
    Then use `if ! telegram-upload ...` – Barmar Jun 28 '22 at 04:33

2 Answers2

0

It's straightforward: Do an endless loop, which you break out once you succeed:

for i in $(find $HOME/test -maxdepth 1 -type f)
do
  while true
  do
    if telegram-upload --to 12345 --directories recursive --large-files split --caption '{file_name}' $i
    then
      break
    else
      rm somefile && wget someurl
    fi
  done
done

UPDATE : For completeness, I added the loop over the files as well. Note that your approach of looping over the files will fail, if you have files where the name contains white space. However this is already a problem in your own approach and not part of the question, so I don't discuss this here.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • I don't think its interating through all the files here so the telegram upload command runs with all the files together as opposed to running 1 by 1 in my original command – Sachin Jun 28 '22 at 06:03
  • @Sachin: Ah, forgot that part. will update accordingly. – user1934428 Jun 28 '22 at 06:17
  • You should not iterate the results of `find` this way because it breaks on paths containing characters from the `$IFS` environment variables, which commonly includes space, tab and newline. See [this answer](https://stackoverflow.com/a/9612560/7939871) from [How to loop through file names returned by find?](https://stackoverflow.com/q/9612090/7939871) – Léa Gris Jun 28 '22 at 10:41
  • @LeaGris : Absolutely. That's why I explained the conditions in which this will fail. However, this part of the problem was not the topic of the question. Actually, the loop ove the files were irrelevant to the question, and I have only copied verbatim, because the OP asked for it in his comment. – user1934428 Jun 28 '22 at 10:52
0

I believe you can capture the exit code as follows:

exit_code=$(telegram-upload --to 12345 ...)

From here, you can use the variable $exit_code as a regular variable,
like if [ $exit_code -eq 0 ]; then).

Dominique
  • 16,450
  • 15
  • 56
  • 112