Posts Tagged ‘ linux

Using getopts with required args in a posix shell

I recently wanted to add a verbose flag to a script. My script required some arguments, and I didn’t want to change the (very limited) already existing logic. Because programming in bash sucks enough that I am willing to spend 6 hours googling “getopts options with args” rather than re-arrange 3 lines of code.

This seems like such a common need that I figured it would be easily googlable, but based on the number of unanswered forum posts that I found, it isn’t.

It turns out that the advanced bash scripting guide does talk about this! Casually, and without any kind of callout. On page 203. As part of the discussion of internal commands.

It turns out that what I wanted was to use the standard getopts loop, followed by a call to shift:

OPTIND=1
while getopts "k:vh" o ; do # set $o to the next passed option
  case "$o" in
    v) # set "v" flag
       ;;
    k) # do something with "k" and $OPTARG, the arg to this opt
       ;;
    h) print "$usage"
       exit
       ;;
  esac
done
 
shift $(($OPTIND - 1))
# $1 is now the first non-option argument, $2 the second, etc

You see, getopts sets the $OPTIND POSIX variable to the index of the option that it should process next every time it’s called, and shift arg shifts arg arguments off the argument list.

Combining those two facts, and using theĀ $(( )) numeric context, lets you set a bunch of options and then act as though your script has only arguments in any POSIX-compliant shell.

I think. This is working for me in bash and zsh, and it’s part of the POSIX standard, so it should be true for you.

Technorati Tags: , ,

My first useful program, ever

2:00am, finals week, what would be a good use of my time? Oh, yeah, how about writing a little bash script to keep my external hard drive from constantly spinning down? Observe:

#!/bin/bash
 
while [ true ]
do
    touch /media/BigEx/.keephdactive
    sleep 4m
done

Wow, right?

Yeah, it’s been kind of bothering me for months, nothing on the internet anywhere had an easy way to do this. And then, after 5-ten minutes of googling in vain all of a sudden I thought to myself: “Hey man, you’re supposed to be a computer scientist, (tbd) can’t you do something about this?” And so, in less time than several failed google searches, I had this.

If you’re on linux and you want to get this running, copy & paste the following into your terminal. It will open up your text editor, inside of which you should change the “BigEx” to the name of your external hard drive. (What it shows up as in the menus and such.) These commands will just put it in your home folder, at the end there’s a command to move it out of there, if you don’t know how.

If you’re in GNOME (eg Ubuntu):
wget http://svn.quodlibetor.com/pubsh/bash/hdactive.sh && gedit hdactive.sh && chmod +rx hdactive.sh

If you’re in KDE (eg Kubuntu, Fedora 9):
wget http://svn.quodlibetor.com/pubsh/bash/hdactive.sh && kate hdactive.sh && chmod +rx hdactive.sh

The only difference between them is a different text editor. (The “&&”s are just ways to join multiple commands together in one line, I did it so that you won’t have to copy/paste 3 times just to get a little hack working :)

Last thing to do then is run it:

./hdactive.sh &

should do it. (The “&” just means “run this process in the background.”)

If you’re in GNOME and you want this file to run every time you start your system, first it’s probably a good idea to put it in your /usr/bin directory:

sudo mv hdactive.sh /usr/bin/

And then you’ll want to add it to your startup session, which is something I don’t know a command for, or how to do it in KDE, so menus time: “System -> Preferences -> Sessions” select “+ add” from the right side, for the name put “HDactive” (or whatever) for the command put “hdactive.sh” (no quotes) and for comment put “oh man quodlibetor’s friggin’ sweet.”

That last part is essential for the proper working of the code :)

OK, it’s now officially been an hour and fifteen minutes since I thought “damn I wish my hard drive would stay on.” On the one hand, that’s way too long, on the other hand, my hard drive has stayed on :)

p.s. I know that there are at least a couple things to do to make this work better (eg accept parameters instead of making people change the code by hand) this works for me, and it’s also damn near my first working bash script ever, so I’ll change it as I have time. I promise that the link will always work though, and if I change it enough that these instructions don’t apply any more I’ll put comments inside it so anybody trying to edit a newer version will see that something’s weird and what to do about it. You can consider it currently licensed under the WTFPL, so do wtf you want to with it :)

[edited a whole bunch of times to get some basic code formatting and syntax highlighting working, since this is my first post with actual code]

Technorati Tags: , , , ,

Seriously? Am i seriously posting about a penguin?

Yeah.

In case you don’t know, and since my last post was about linux, the ‘official,’ or at least as official as it gets, Linux mascot/logo is Tux, the penguin. Here is a page that provides a brief history of the guy, including the letter to the mailing list where Linus (pronounced leenus, i think) first suggested that a penguin be the mascot. It’s pretty funny, especially considering it’s written by the guy who basically wrote linux.

oh, and tux has his own gospel.

Technorati Tags:

Rhymes with “canoe”

GNU (pronounced with a hard G) is the little operating system that could, eventually turning into Linux. I think. Anyway, richard stallman would have everyone refer to the latter as GNU/Linux, an obviously unfeasable proposition.

GNU is an acronym for “GNU’s Not Unix,” it’s what’s known as a recursive acronym because as you can tell, the acronym is part of the name. So GNU can actually be further expanded to “GNU’s Not Unix Not Unix” and further “GNU’s Not Unix Not Unix Not Unix.” Eventually it starts to sound like a child whining.

Which, if you’ve ever read much about the Free Software Foundation, seems appropriate.

Technorati Tags: ,