LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

[bash] "keep trying" command, even if exits with error, for at least 10 times

Posted 08-08-2014 at 10:40 PM by the dsc
Updated 08-08-2014 at 10:42 PM by the dsc
Tags bash

A script that tries to run a given arbitrary command N times, or until it exits with success. The commented parts are aborted attempts to make it figure whether the first parameter is a number instead of a command, and that would be used as the maximum number of attempts. Something went wrong, sometime I'll try to figure it out.

Quote:
#!/bin/bash
#input="$@"
# this program is stupid. It's declared to be in public domain by its author, who whishes to remain anonymous in order to spare him or herself of ridicule. Run at your own risk.
#echo $input | grep "^[[:digit:]]" && ( echo "then" ; command="$(echo $input | sed 's|^[[:digit:]]\ ||')" ; max=$(echo $@ | sed 's|\ .*||') ) || echo "else" ; export command="$input" ; max=10
#echo $command
max=10
#echo $max
attempts=0
false ; until [[ $? -eq 0 ]] || ((attempts>(max-1))) ; do attempts=$((attempts+1)) ; echo $attempts ; $@ ; done
Posted in Uncategorized
Views 899 Comments 3
« Prev     Main     Next »
Total Comments 3

Comments

  1. Old Comment
    Hey I had to give this one a shot on trying to get it to work for n tries, came up with a promising result, if interested.

    Heck, I posted it, if you'd like:

    http://www.linuxquestions.org/questi...og.php?b=36165
    Posted 08-09-2014 at 03:08 AM by goumba goumba is offline
    Updated 08-12-2014 at 10:57 PM by goumba
  2. Old Comment
    Thanks, I'll take a look at it!
    Posted 08-18-2014 at 02:09 PM by the dsc the dsc is offline
  3. Old Comment
    Just another not-so-different approach:

    Code:
    #!/bin/bash
    trycmd=""
    trystr=""
    num='^[0-9]+$'
    
    if [[ $1 =~ $num ]] ; then
    trycmd="$2"
    max="$1"
    else
    max=5
    trycmd="$1"
    fi
    
    trystr=$trycmd
    for ((n=0;n<max;n++)) ; do
    trystr="$trycmd || $trystr" ; done
    eval $trystr
    It's basically an automation of just copying the same command several times, separated by "||", which is also a crude way to do the same thing without the "until $? -eq 0" loop thing.

    By default it will try 5 times, but the first parameter may be a desired number of attempts.

    I don't know how well it will handle commands with quotes and such things. If there's more than one word in the command it needs to be between quotes. Like

    try 5 "wget -c www.host.com/file"
    Posted 04-03-2015 at 10:17 PM by the dsc the dsc is offline
 

  



All times are GMT -5. The time now is 12:18 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration