User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 455,976 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,797 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 988 | Replies: 9 | Solved
Reply
Join Date: May 2006
Location: India
Posts: 5
Reputation: tanalam is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tanalam tanalam is offline Offline
Newbie Poster

parsiring asterisk

  #1  
Nov 27th, 2007
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...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,012
Reputation: ithelp will become famous soon enough ithelp will become famous soon enough 
Rep Power: 6
Solved Threads: 68
ithelp ithelp is offline Offline
Veteran Poster

Re: parsiring asterisk

  #2  
Nov 27th, 2007
Try grep ///*
Reply With Quote  
Join Date: May 2006
Location: India
Posts: 5
Reputation: tanalam is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tanalam tanalam is offline Offline
Newbie Poster

Re: parsiring asterisk

  #3  
Nov 27th, 2007
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
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: parsiring asterisk

  #4  
Nov 27th, 2007
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 /\\.
Last edited by masijade : Nov 27th, 2007 at 6:27 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: May 2006
Location: India
Posts: 5
Reputation: tanalam is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tanalam tanalam is offline Offline
Newbie Poster

Re: parsiring asterisk

  #5  
Nov 28th, 2007
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...
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: parsiring asterisk

  #6  
Nov 28th, 2007
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
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: May 2006
Location: India
Posts: 5
Reputation: tanalam is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tanalam tanalam is offline Offline
Newbie Poster

Re: parsiring asterisk

  #7  
Nov 28th, 2007
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
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: parsiring asterisk

  #8  
Nov 28th, 2007
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: May 2006
Location: India
Posts: 5
Reputation: tanalam is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tanalam tanalam is offline Offline
Newbie Poster

Re: parsiring asterisk

  #9  
Nov 28th, 2007
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.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: parsiring asterisk

  #10  
Nov 28th, 2007
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 9:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC