I have a whole bunch of really messy bash scripts to do a lot of things. These scripts control my cd ripping and encoding and organisation, and my video ripping. They help me backup and transfer data. They are simplistic and they are out of control.
For example, I have a pair of scripts called music2trap and trap2music. The first one will rsync my /music directory (where I have all my mp3s) with a mount point where my mp3 transfer external hard disk is attached. The second one is for work when I attach the external hard disk and sync it with the /music directory at work.
They work. But it sucks because I can't remember what things are, where the mount point is, and if the mount point changes (like the external mounts as /mnt/sde for various reasons) I have to make a temporary change, then forget to change it back, or I create a one-off copy of it, or anything else.
So I got sick of all this. I decided it was time to do the right thing by ALL my shell scripts. I started with this pair. I renamed them more sensibly. I don't know why they ended up with trap in the name. So now they are music2ext and ext2music.
Both use command line switches for over-rides.
- -h gives Usage, and the usage is useful.
- -d is dry run mode so I can see what will run without running it.
- -s /mnt/sdx lets me over-ride the default mount point.
And now I plan to do the same to all my scripts. Every time I have to use one of my shell scripts, I am going to take it apart and rebuild with these points:
- command line switches
- will have -h and usage
- will have a dry-run mode
- will allow over-rides of directories or mount points with a command line switch
- anything else that might change will have a command line switch
- comments, must have comments all throughmust have sensible names
I read about doing this a long time ago in a book about bash scripts, but it took me this long to understand why you do a decent job of even throwaway scripts. Because throwaway scripts turn into long-standing and often used scripts.