Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ksh - checking a string for leading # char

0 views
Skip to first unread message

ri...@dfwvx1.dallas.geoquest.slb.com

unread,
Jan 20, 1999, 3:00:00 AM1/20/99
to

Hi,

I'm working on a flexlm license checker to notify people when their
license is about to expire. I read in a list of license files and I
would like to be able to comment out a file with a # symbol in front
of the file name. Right now I'm doing it with a temp file and grep
but I would think there is a better way with string operations in
ksh. What I need to do is check for a leading # character but I'm
not sure how to do so.

Here is the current code I'd like to fix:

#!/bin/ksh
#
# Read license files and check dates for expiration
#
input=/system/license.files # file containing list of license files

exit_stat=0
node=`hostname`
echo "Subject: License Check on Node " $node

# strip out comment lines in license.files
# (bad design as drops any line with a # anywhere on it)

grep -v "#" $input > /tmp/lic_files

for file in $(< /tmp/lic_files)
do
echo ""
echo "Testing license file " $file

if ! nawk -f /system/licdate.awk $file
then
exit_stat=1
fi

done

rm /tmp/lic_files
exit $exit_stat

--------------
Rick Caldwell
ri...@slb.com


Michael P. Reilly

unread,
Jan 21, 1999, 3:00:00 AM1/21/99
to
ri...@dfwvx1.dallas.geoquest.slb.com wrote:

: Hi,

: done

: rm /tmp/lic_files
: exit $exit_stat

Use a case statement:
for file in $(< $input)
do
case $file in
''|'#'*) ;; # ignore blank lines and comments
*)


echo
echo Testing license file' '$file

nawk -f /system/licdate.awk $file >/dev/null ||
exit_stat=1
;;
esac
done

This lets you add conditions more easily later.

-Arcege


Stephane Du Pasquier

unread,
Jan 21, 1999, 3:00:00 AM1/21/99
to
ri...@dfwvx1.dallas.geoquest.slb.com wrote:

> grep -v "#" $input > /tmp/lic_files
>
> for file in $(< /tmp/lic_files)
> do

you can avoid the temp file:

for file in $(grep ...); do

however, this will fail for big inputs,
so you should rather say:

grep -v '^#' $input|while read file; do
stuff
done

you could also start grep in a co-process:
grep -v '^#' $input |&

and have the loop read from it:
while read -p file; do

I would stay with a grep or sed to filter the
lines; you can use a ksh test to reject every
unwanted line at the top of the loop (by calling a
continue) but it will be slower (provided
your input file is not ridiculously small).

S.

Sharon

unread,
Jan 25, 1999, 3:00:00 AM1/25/99
to
Hi Rick !

Your grep command should be :

grep -v ^# $input > /tmp/lic_files

The "^" is the character which, in regular expressions, stands for "The
line(s) beginning with what follows". By the way, the grep command doesn't
support all of the regular expresions syntax. It does however support the
"^" character.

Sharon Zehavi.


ri...@dfwvx1.dallas.geoquest.slb.com wrote:

> Hi,
>
> I'm working on a flexlm license checker to notify people when their
> license is about to expire. I read in a list of license files and I
> would like to be able to comment out a file with a # symbol in front
> of the file name. Right now I'm doing it with a temp file and grep
> but I would think there is a better way with string operations in
> ksh. What I need to do is check for a leading # character but I'm
> not sure how to do so.
>
> Here is the current code I'd like to fix:
>
> #!/bin/ksh
> #
> # Read license files and check dates for expiration
> #
> input=/system/license.files # file containing list of license files
>
> exit_stat=0
> node=`hostname`
> echo "Subject: License Check on Node " $node
>
> # strip out comment lines in license.files
> # (bad design as drops any line with a # anywhere on it)
>

> grep -v "#" $input > /tmp/lic_files
>
> for file in $(< /tmp/lic_files)
> do

> echo ""
> echo "Testing license file " $file
>
> if ! nawk -f /system/licdate.awk $file
> then
> exit_stat=1
> fi
>
>
> done
>
> rm /tmp/lic_files
> exit $exit_stat
>

> --------------
> Rick Caldwell
> ri...@slb.com


Cal Dunigan

unread,
Jan 25, 1999, 3:00:00 AM1/25/99
to
On Mon, 25 Jan 1999, Sharon wrote:
> Your grep command should be :
>
> grep -v ^# $input > /tmp/lic_files

Yep, but any time you have a shell special character (e.g. "^", "#"
etc.) in your command be sure to quote them.


grep -v "^#" $input > /tmp/lic_files

If you aren't sure, use quotes. If they are superflous, they will be
harmless, the converse is not true.

> The "^" is the character which, in regular expressions, stands for "The
> line(s) beginning with what follows". By the way, the grep command doesn't
> support all of the regular expresions syntax. It does however support the
> "^" character.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Cal Dunigan c...@goodnet.com There is something fundamentally
Consulting wrong with a world in which Ken
Modeling Thompson lives in obscurity and
Training Bill Gates is a famous billionaire.
//////////////////////////////////////////////////////////////////////


0 new messages