Hi all,
I am writing a script which parses /* to find out the commented portion of the code.
I am using grep "/\*" file.c
Following is the code snippet

for i in `grep "/\*" file.c`; do
.
.
.
done

but this is not giving the lines greped from file.c.
However if I run this command from the shell i.e.
grep "/\*" file.c I get the correct answer, I think the problem is due to ` .
Can some body help me on this

TIA
tan...

Recommended Answers

All 9 Replies

Try grep ///*

That too is not working.

Actually in the same script file if I do only
grep "/\*" file.c
it works fine
But when I put it under the single quote ` ` the behavior changes

for i in `grep "/\\*" file.c`; do

You need to escape the escape character when doing this inside of the expression quotes.

Edit: Which, I believe, is what ithelp meant, but he mistakenly used /// instead of /\\.

Thanks for reply
but that thing is still not working for me.
Here is the code
for i in `grep "/\\*" file `; do echo ">>>$i ";do
echo "$i"
done

Contents of file is simple
Line 1
LIne 2
/*
//
LIne 3

The output(mentioned below) is strange listing of / directory
/bin
/boot
/dev
/etc
/home
/initrd
/lib
/lost+found
/media
/misc
/mnt
/opt
/proc
/root
/sbin
/selinux
/srv
/sys
/tmp
/usr
/var

regards
tan...

It is because of file name expansion in the shell.

Just for fun try it this way

for i in `grep "/\\*" file`; do
  set -f i=$i
  echo "$i"
done

for i in `grep "/\\*" file`; do
set -f i=$i
echo "$i"
done

is working on the shell prompt but not from the shell script. It is still giving the same output

Ok, unfortunately, the only thing left to do, that I can think of, is to do the script this way:

#!/bin/sh -f
for i in `grep "/\\*" file`; do
  echo "$i"
done

And start the entire script without file name generation. That means, however, that file name expansion/generation does not work at all in the script, which may be wholly unenjoyable, but at least this part works.

Thanx "#!/bin/sh -f" works
Also I found one more way to make it work
. ./script.sh
that is running the script from the same shell.
I wonder if both are the same method or different one.

Different. Although I don't really know why sourcing the script, rather than simply running it, would make a difference in the way set -f acts, but, oh well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.