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

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

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


If you have seq available, you could do:

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

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

  gksu command && break

or even simpler (hat tip to drewbenn):

for attempt in {1..3} 
  gksu command && break
  • 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
    – user275214
    Commented Sep 12, 2016 at 17:29
  • @drewbenn thanks! I'm going to trying it out after the script has downloaded the installer file.
    – user275214
    Commented Sep 12, 2016 at 17:33
  • @user275214 does xampp-install.sh specify something other than bash (or ksh or zsh) ?
    – Jeff Schaller
    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.
    – user275214
    Commented Sep 13, 2016 at 16:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .