»Q« writes: > On Sun, 19 Aug 2007 07:10:30 +0200 > > Bo Ørsted Andresen <[EMAIL PROTECTED]> wrote: > > On Sunday 19 August 2007 04:00:45 »Q« wrote: > > > It works as it is, but I'm interested in learning about any newbie > > > traps into which I might be falling or about any better practices I > > > should use. This is my first attempt at printing a usage message or > > > parsing arguments. > > > > > > The script is at <http://remarqs.net/misc/mids2urls.sh.txt>. Feel > > > free to reply to me off-list (or just to flame me) if you think > > > this kind of non-Gentoo-specific stuff has no place here. > > > > It's hardly the best place to ask this kind of question but > > anyway.. :p
Right :) But a I like bash-scripting.... I also think I should start learning python, but I need my scripts to work on old Solaris installations, where I do not like to install perl, python and such. ANd probably I am not allowed to so so anyway. That's why I chose bash for them. Well, I was surprised how much I could accomplish with that. The size of my application is now > 250k and still growing. > Yeah. Thanks for having a look at it anyway. Is there a list or group > that welcomes this kind of stuff? I looked but couldn't find one. There is comp.unix.shell. And read the Advanced Bash Scripting Guide: <http://tldp.org/LDP/abs/html/> > > The most noticeable thing which is wrong with this script is the > > quoting inside the brackets [ ]. Since you use bash you should just > > use [[ ]] instead (which makes most of the quoting in them > > unneeded..). Also things like [[ $foo == "" ]] should rather be [[ -n > > $foo ]] but that's a minor. I always use [[ $foo ]] only. Also, instead of if [ ! -f "$1" ] I like if ! (( $# )) # $# is the number of arguments better. BTW, shift $(($OPTIND - 1)) can also be written as shift $((OPTIND-1)) - you do not need the $ inside (( )) contructions. They are quite powerful, I did not expect bash to do C-like things like this: for (( i=0; i < 10*foo; i++ )) > > Finally the number of calls to grep, sed and tr clearly shows that > > you could improve your knowledge about sed.. ;) > > Yeah, very true. I also tend to avoid grep and sed, at least if I need performance. Exampes: if echo "$var" | grep "bla" can be written as: if [[ $var == *bla* ]] I replace simple if-then statemens by &&: if [ $QUIET == "no" ] then echo -e "$URLLIST" fi --> [[ $QUIET == "no" ]] && echo -e "$URLLIST" Some more things: THIS_SCRIPT=$(basename $0) --> THIS_SCRIPT=${0##*/} (strip everything from $0 until the last / from the left). Okay, there is basename and dirname, but they are external comands. If I like to use them, I define them like this: basename() { local basename=${1##*/} echo "${basename%${2:-}}" } dirname() { echo "${1%/*}" } Have a look at all these {} constructions, they are very powerful. In longer scripts, I define all variables I use, and with set -u I make bash issue errors if it encounters undefined ones. This helps in case I make a type (using $fooo instead of $foo), which otherwise would just give an empty string. One last thing is that I so not like the ; at the end of lines. I put the thens, dos and such into the next, under their corrsponding ifs and whiles. Looks better to me. Same goes for the starting { in functions. BTW, after && . || or |, a \ is not needed if the statement continues in the next line. Also, foo="$( .... )" can always be written without the "". Have fun with bash, Wonko -- [EMAIL PROTECTED] mailing list

