0

Usually !! repeats the last given command. But it doesn’t seem to work in a bash script. Is there a way to end a script and repeat it with the exact same options and arguments given the last time?

Additional info: In a part of my script it is checked if a server is running. If it isn't, it is started by etherwake. This is the part, where I want the script to be ended and restarted, until the server is finally up. Ignoring the problem that this easily can lead to a endless loop which might threaten system/network stability.

Is there an easier/safer way? There doesn't seem to be a GOTO LINE command or something similar.

1
  • I am not sure where in the process the options and arguments get in the picture? – Jacob Vlijm Aug 26 '14 at 10:28
2

Just make an infinite while loop:

#!/bin/sh

while [ TRUE ]; do

check_server_status 10.0..
etherwake ff:ff..

done

1

How about

while server-is-not-running; do
    try-and-start-server
    sleep seconds-to-wait
done

where

  • server-is-not-running is your current test to see if the server is running, i.e. a shell function or an external command returning non-zero if the server is running (alternatively, ! server-is-running if that function or command returns zero if the server is running).
  • try-and-start-server is the command to start the server, which again can be a shell function or external command
  • seconds-to-wait is a sensible number of seconds to wait for successful start

To limit the number of attempts, replace the while with a for:

for i in {1..number-of-attempts}; do
    try-and-start-server
    sleep seconds-to-wait
    server-is-not-running || break
done
2
  • I think this will do the trick quite elegantly. Thanks! – Markus Aug 26 '14 at 13:22
  • Great to hear @Markus, however please adhere to Askubuntu policy: no Thanks! comments, instead mark the answer as such. – zwets Aug 27 '14 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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