[Clug-tech] Bash Scripting

Shawn sgrover at open2space.com
Tue Nov 21 02:32:28 PST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Never fails.. shortly after posting this message, I figured it out.

I used "grep -c mystring myfile" to get a count our how many matches are
found.  So the code looks something like this:

position=`grep -c $targettext $MYFILE`
if [ $position -eq 0 ]
then
  #... add text to the file ...
fi

I suspect that this is just one way of many.  Is there a better method?

Shawn


Shawn wrote:
> Hi all.
> 
> I'm writing a bash script to automate some tasks.  One of the tasks is
> to add a block of text to a file.  The catch is that I only want this
> block of text added if it is not already there (otherwise there'd be
> multiple entries...)
> 
> I know I can use grep to find the text in question, but I'm not sure how
> to use this in a bash script to equate to "this text exists".
> 
> So, in pseudo code, I'm after this:
> 
> targettext="some value"
> if ($targettext not in $MYFILE)
> 	echo $targettext >> $MYFILE
> 
> How would I do this in bash?
> 
> Thanks for any tips.
> 
> Shawn

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFYtW72B6Swl9qN24RAv2EAKDep1Vjr5B3s1hANzQSjZnmgN5nFACg1Yy9
HeeCT3CoNDqQqJEvy9VR8eg=
=vyXP
-----END PGP SIGNATURE-----




More information about the clug-tech mailing list