Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question
5  
The first command line argument is $1 so TIME=$1 or just put $1 in your URL. –  Joseph R. May 2 at 0:07
1  
@JosephR. Even if it seems too simple to you: Make that an answer so that it can be accepted and the question get marked done. –  Hauke Laging May 2 at 0:18
    
@HaukeLaging Seems slm already took care of that :) –  Joseph R. May 2 at 0:20
    
I swear I didn't even see your comment, I was already thinking of my ${VAR:-defaultval} post when I wrote it, just saw the commentary now. –  slm May 2 at 0:22
1  
@slm No worries. My previous comment was sincere, not passive aggressive :) –  Joseph R. May 2 at 0:29

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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