Validating parameters to a bash script
Posted
by nickf
on Stack Overflow
See other posts from Stack Overflow
or by nickf
Published on 2009-03-31T00:29:45Z
Indexed on
2010/03/26
15:03 UTC
Read the original article
Hit count: 185
I'm a total newbie to doing any bash scripting, but I came up with a basic one to help automate the process of removing a number of folders as they become unneeded.
#!/bin/bash
rm -rf ~/myfolder1/$1/anotherfolder
rm -rf ~/myfolder2/$1/yetanotherfolder
rm -rf ~/myfolder3/$1/thisisafolder
This is evoked like so:
./myscript.sh <{id-number}>
The problem is that if you forget to type in the id-number
(as I did just then), then it could potentially delete a lot of things that you really don't want deleted.
Is there a way you can add any form of validation to the command line parameters? In my case, it'd be good to check that a) there is one parameter, b) it's numerical, and c) that folder exists; before continuing with the script.
© Stack Overflow or respective owner