Re: bash scripts conditionals and substrings

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: der.hans
Date:  
To: plug-discuss
Subject: Re: bash scripts conditionals and substrings
Am 28. Feb, 2005 schwätzte Craig White so:

> banging my head against the wall (despite what Craig Brooksby says, I am
> not the smart one)


We're all smart. We're all dumb. Hopefully we aren't both on the same
subject ;-).

> simple script...


Some suggestions. I learned shell syntax from people who'd been coding for
eons before I learned about computers. I didn't necessarily learn well, so
use my advice with caution :).

Pay attention to what Jeremy said :).

> #!/bin/sh
>


Good, always leave the second line blank.

> infile="/tmp/outfile.txt"
> outfile="/tmp/default_user.php"
>
> > $outfile


Even though you've hard-coded the location of $outfile in the code you
should check that it's OK to wipe the file before doing so.

>
> for i in `cat $infile`; \
> do \


for i in `cat $infile`
do

Backslashes for line continuing isn't universal, so avoid it.

>    if ${i:6:5} = "START"
>       then printf $i >> $outfile


if [ 'START' = ${i:6:5} ] ; then
    echo $i >> $outfile


Use brackets on the test. As Jeremy said, see man page for test(1).

Put the fixed string on the left to avoid false positives due to
assignment rather than comparison.

If the string is constant use single quotes for control statements. That's
something you should do all the time, but it's more important for control
statements. You want to be certain of what you're checking. Also, if it's
part of a loop you're calling it multiple times.

You don't need printf for that, so use the simpler ( and lighter ) echo.
Not really important, but I've seldom needed to reach for printf in shell
scripts.

>    else
>       printf "if (!function_exists('_prefs_hook_$i')) { \n\n" >>
> $outfile

>
>    fi
> done

>
> # head -n 4 /tmp/outfile.txt
> **** START OF SECTION - horde - ****
> default_identity
> identities
> identityselect
>
> #### gives you the idea of what I'm working on...
>
> What I am trying to do is see if line has "***** START ...
> and if so, write it to the outfile
> otherwise, write the other (else)


Try while rather than for.

$ cat fred
**** START OF SECTION - horde - ****
default_identity
identities
identityselect

while read line
do
    echo "($line)"
    echo ${line:5:5}
    if [ 'START' = ${line:5:5} ] ; then
        echo got start
    else
        echo no start
    fi
done < fred


That script with the above input file produces the following output.

(**** START OF SECTION - horde - ****)
START
got start
(default_identity)
lt_id
no start
(identities)
ities
no start
(identityselect)
ityse
no start


I didn't know about the substring syntax, so I learned something with this
as well :).

ciao,

der.hans
-- 
#  https://www.LuftHans.com/    http://www.AZOTO.org/
#  Schließlich verteidigt Amerika Freiheit. Und Freiheit beginnt mit dem Wort.
#    -- Gunter Grass
---------------------------------------------------
PLUG-discuss mailing list - 
To subscribe, unsubscribe, or to change  you mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss