Using getopts to pass parameters in bash scripts
Submitted by diveli on Thu, 07/24/2008 - 15:23I always wondered how scripts passed variables beginning with a '-', i.e 'foo.sh -q', or 'tar -zxfv' and so on.
I knew that '$1' in bash takes the first extra variable sent at the shell, but that can be any sort of word, not specifically a '-' parameter. Does that make sense?
Anyway, it turns out you can use this neat getopts function like so, with a case statement. This way you can also output a 'usage' mini-manual when a variable that is unrecognised, is passed:
usage() { cat << EOF usage: $0 -option Use this script to rsync this to that :)
- diveli's blog
- Add new comment
- Read more
- 289 reads
no users on the server, says a 'user' :)
Submitted by diveli on Wed, 06/04/2008 - 11:11It's sad when this sort of thing makes me laugh:
11:09 < diveli> uptime
11:09 < poilu> 11:09:20 up 3 days, 16:18, 0 users, load average: 0.05, 0.08, 0.03
Magic invisible poilu! Hooray for my trusty irc bot :)
- diveli's blog
- Add new comment
- 100 reads
Statistics Report
Submitted by diveli on Mon, 05/26/2008 - 09:55This is my Client web/database usage/bandwidth statistics script as mentioned in the Think Tank.
- 221 reads