3

I am writing a shell script that takes in a time (in minutes) and opens an online egg timer set for that amount.

#!/bin/bash
#Opens http://e.ggtimer.com/TIMEmin where TIME = number of minutes
TIME=5
xdg-open http://e.ggtimer.com/"$TIME"min

When I run the above script it opens the web page http://e.ggtimer.com/5min I would like to set the time from the terminal. For example:

eggtimer 10

Would open http://e.ggtimer.com/10min

Thank you!

AntCas
  • 33

1 Answers1

5

If you use the following feature of Bash variables where they're used if they have a value otherwise a default value is used instead you can code up your example like so:

$ more etimer.bash 
#!/bin/bash
#Opens http://e.ggtimer.com/TIMEmin where TIME = number of minutes
TIME=${1:-5}
xdg-open http://e.ggtimer.com/"$TIME"min

This will use an argument that's passed into etimer.bash if one is provided, otherwise will use 5 minutes.

Examples

$ ./etimer.bash

Results in this URL: http://e.ggtimer.com/5min

$ ./etimer.bash 10

Results in this URL: http://e.ggtimer.com/10min

References

slm
  • 369,824