1

I want to automate an installation and I need to run the downloaded installer with gksu. I have tried:

attempt=0
  until
  gksu command; do
    attempt=$((attempt + 1))
    if [ "$attempt" -gt 3 ]; then
      exit 1
    fi
  done

but it doesn't exit until it reaches the third attempt. It doesn't matter if gksu has exited with exit code 0 or with a non zero exit code. What I would like to be happen is:
while gksu command's exit code is not 0 and attempt number is not 3 repeat gksu command. If exit code is not 0 but attempt number is 3, exit the whole script. If exit code is 0 leave cycle and continue processing the script.
How could I do that?

1 Answer 1

2

If you have seq available, you could do:

for attempt in $(seq 1 3)
do
  gksu command && break
done

If seq is not available, but you have (and want to use) bash:

for((attempt=1;attempt<=3;attempt++))
do 
  gksu command && break
done

or even simpler (hat tip to drewbenn):

for attempt in {1..3} 
do
  gksu command && break
done
4
  • for((attempt=1;attempt<=3;attempt++)) do gksu xampp-linux-x64-5.6.21-0-installer.run && break done 51: ../xampp-install.sh: Syntax error: Bad for loop variable Commented Sep 12, 2016 at 17:29
  • @drewbenn thanks! I'm going to trying it out after the script has downloaded the installer file. Commented Sep 12, 2016 at 17:33
  • @user275214 does xampp-install.sh specify something other than bash (or ksh or zsh) ? Commented Sep 12, 2016 at 17:46
  • No, it doesn't. I only created this script because i wanted to learn something other than PHP SQL or C# (these are what they teach in school). I didn't even think about publishing it, not to mention about rewriting it to other shells. Commented Sep 13, 2016 at 16:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.