I want to retry a command for 5 times with an interval of 20 seconds. I want this command to be passed as a method parameter. How to do it? And once the function is written how to pass the value to the function?

I want my current code to be converted to a function which takes a set of parameters. How to write and call this function in my shell script?

My current code is like this :

  trialNumber=0
    until [ $trialNumber -ge 5]  
     do
        ssh $USERID@$HOST $SCRIPT_LOCATION/runme.sh   # This line is my command and it may very with number of parameters or command itself.
       [ $? -eq 0 ] && break
      trialNumber=$[$trialNumber+1]
      sleep 20
   done

( Above code is embedded at many places I want to move it into a function).

  • As a side note. The code need to be improved. Though you could wrap it in a function like TrytoSSH() { your_code_here } then call your function by TrytoSSH – Valentin Bajrami Aug 19 '13 at 11:13
  • Thanks val0x00ff, what about passing parameters and using them inside the method? For example can I call trytoSSH $USERID@$HOST $SCRIPT_PATH/runme.sh and use them in my function with $1, $2 , $3 ? And sometimes parameters to ssh might be different in number also so I want "ssh $USERID@$HOST $SCRIPT_LOCATION/runme.sh " or ssh -t $USERID@$HOST $SCRIPT_LOCATION/runme.sh " as single parameter and want to replace it directly with ssh. – java_enthu Aug 19 '13 at 11:27
  • You certainly can. You can use "$1" "$2" positional parameters to achieve this. The command will be like ssh -t "$1" "$2" where positional parameter "$1" is user@host and "$2" is your script. – Valentin Bajrami Aug 19 '13 at 11:30
  • @val0x00ff Is quoting the positional parameter really necessary? With spaces you are ****ed already before passing the parameters. Other side note to java_enthu: why not using a for loop? – Bernhard Aug 19 '13 at 11:41
  • @Bernhard Not really, it's more a habit. I always quote the vars even though not needed. – Valentin Bajrami Aug 19 '13 at 11:49
up vote 3 down vote accepted
retry() {
  trialNumber=$1 delay=$2; shift 2
  while [ "$trialNumber" -gt 0 ]; do
    "$@" && return
    ret=$?
    sleep "$delay"
    trialNumber=$(($trialNumber - 1))
  done
  return "$ret"
}

retry 5 20 ssh "$USERID@$HOST" "$SCRIPT_LOCATION/runme.sh"

Though the last sleep in case of failure is not necessary. May be better as:

retry() {
  trialNumber=$1 delay=$2; shift 2
  until "$@"; do
    ret=$?
    trialNumber=$(($trialNumber - 1))
    [ "$trialNumber" -gt 0 ] || return "$ret"
    sleep "$delay"
  done
}
  • Hi Stephane,looks good to me. Thanks a bunch. I put this code in a shell script and in the shell script I am trying to call this function but I get line:6 command:retry not found. (function definition of retry starts at line 6 in my script). – java_enthu Aug 19 '13 at 11:44
  • Thanks it worked. I did not know that method should be declared only at the top. :-). Merci mille fois. – java_enthu Aug 19 '13 at 11:57
  • Stefan : I've another scenario : SCRIPT_FILES =ssh $USER@HOST find $SCRIPTS -maxdepth 2 -type f for SCRIPT in $SCRIPT_FILES ..... Can I use the same retry function here also , I mean : SCRIPT_FILES =5 20 ssh $USER@HOST find $SCRIPTS -maxdepth 2 -type f ??? – java_enthu Aug 19 '13 at 16:33

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.