[wellylug] BASH scripting help required
Brian M Hoy
brian.hoy at pivot.co.nz
Fri May 7 11:58:51 NZST 2004
Jamie,
Something like this should do the trick. If you care about case, then
remove the tr command.
Cheers,
Brian
#!/bin/bash
for file in *
do
FIRSTCHAR=`echo $file | cut -c1 | tr /a-z/ /A-Z/`
if [ "$FIRSTCHAR" == "L" ] ; then
echo "Starts with L"
elif [ "$FIRSTCHAR" == "M" ] ; then
echo "Starts with M"
fi
done
On Fri, 2004-05-07 at 11:31, Jamie Dobbs wrote:
> True, Perl would be one way to do it, but not an option in this case.
>
> -----Original Message-----
> From: David Antliff [mailto:dave.antliff at paradise.net.nz]
> Sent: Friday, 7 May 2004 11:29 a.m.
> To: wellylug at lists.naos.co.nz
> Subject: Re: [wellylug] BASH scripting help required
>
>
> On Fri, 7 May 2004, Jamie Dobbs wrote:
>
> > I have a script that does some file manipulation then sends a file
> out of
> > the serial port.
> > Each file is reference as $file (via a for 'file in <filespec>'
> type setup).
> > What I need to do is to be able to test on the first character of
> the
> > filename and process something accordingly - ie. if the first
> character is
> > 'L' then do one thing, if it is 'P' do another etc. My skills at
> string
> > manipulation in Unix/BASH are terrible (at best) and I wonder if
> someone
> > caqn give me some pointers on how to do this.
>
> Sounds like the perfect excuse to learn some basic Perl :)
>
> One (of many) ways to do this is:
>
> if ($file =~ /^L/) {
> ...
> } elsif ($file =~ /^P/) {
> ...
> }
>
> Perl is *really* good at string manipulation.
>
> Cheers,
> David.
>
>
> --
> Wellington Linux Users Group Mailing List: wellylug at lists.naos.co.nz
> To Leave: http://lists.naos.co.nz/mailman/listinfo/wellylug
More information about the wellylug
mailing list