[gentoo-user] Re: [OT] bash script advice/criticism wanted

2007-08-19 Thread »Q«
Bo Ørsted Andresen [EMAIL PROTECTED] wrote:

 Also things like [[ $foo ==  ]] should rather be [[ -n $foo ]] but
 that's a minor.

[[ -z $foo ]] seems to work much better.  ;)

--
[EMAIL PROTECTED] mailing list



Re: [gentoo-user] Re: [OT] bash script advice/criticism wanted

2007-08-19 Thread Alex Schuster
»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



[gentoo-user] Re: [OT] bash script advice/criticism wanted

2007-08-19 Thread »Q«
On Sun, 19 Aug 2007 10:43:18 +0200
Alex Schuster [EMAIL PROTECTED] wrote:

 There is comp.unix.shell.

Thanks.  I'll go there from now on.

 And read the Advanced Bash Scripting Guide: 
 http://tldp.org/LDP/abs/html/

I had been reading the ABS doc, but parts of it made more sense after
the input from you and Bo.  I've digested most of it, and the script
looks a lot different.  Bo will be happy that sed is only called once
(and no grep or tr), and you should be somewhat more happy with my
conditionals and some other things.

The updated version is at that same URL,
http://remarqs.net/misc/mids2urls.sh.txt.

-- 
[EMAIL PROTECTED] mailing list



[gentoo-user] Re: [OT] bash script advice/criticism wanted

2007-08-18 Thread »Q«
[EMAIL PROTECTED] wrote:

 On Sat, Aug 18, 2007 at 09:00:45PM -0500, ?Q? wrote:
 
  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.
 
 I took a quick look and about the only advice I'd give is ... use
 perl! or python or ruby, or whatever language rocks your boat.
 
 I learned perl because a small project I did involved text processing
 and it got uglier and uglier the more I tried to trick awk, sed, grep,
 sort, and all the usual suspects into doing the job.  I finally
 decided to learn perl and did that and got the job done in half the
 time I had spent trying the usual suspects.  Since then, I have gone
 straight to perl for any script which has to do anything as fancy as
 get options or parse text.

I'm ok with grep and sed s, but not much more.  I guess I'll (try to)
learn perl if I have any serious text processing to do.  But for now,
I'm going to be stubborn and stick with bash.

 Bash makes scripting more powerful than the old bourne shell, but it
 doesn't make it any easier.
 
 I know it's not the advice you wanted :-)

Heh, I can't always get what I want 

-- 
[EMAIL PROTECTED] mailing list



[gentoo-user] Re: [OT] bash script advice/criticism wanted

2007-08-18 Thread »Q«
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

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.

 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.

Thanks much.  And thanks for the link.

 Finally the number of calls to grep, sed and tr clearly shows that
 you could improve your knowledge about sed.. ;)

Yeah, very true.

 http://wooledge.org/mywiki/BashFAQ#head-b1c292ce2f4fdfa6a7b5389da1892d65b6f37cda

--
[EMAIL PROTECTED] mailing list