LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2012, 04:04 PM   #1
pfpietro
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Rep: Reputation: Disabled
[Quick question]Problem with execl and GREP


Guys, I have the following code

Code:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

void read2();
main(int argc, char** argv)
{
int pid,status;
	pid=fork();
	if ( pid == 0 )
	{
		read2(argv[1], argv[2]);
		exit(0);	
	}
	else
	wait(&status);
}

void read2(char * file, char * stringa)
{
	printf("%s, %s\n",file, stringa);
	execl("/usr/bin/grep", "grep", "-c", stringa, file, (char *)0);
		
}
Why I don't see any outputs on the screen when I run my program ?
usying any .txt file I should get on stdout ( screen ) the number of lines that "stringa" shows up. But I don't.

What am I doing wrong ?
 
Old 08-31-2012, 05:09 PM   #2
billyoc
LQ Newbie
 
Registered: Oct 2010
Posts: 23

Rep: Reputation: 23
Quote:
Originally Posted by pfpietro View Post
Guys, I have the following code

Code:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

void read2();
main(int argc, char** argv)
{
int pid,status;
	pid=fork();
	if ( pid == 0 )
	{
		read2(argv[1], argv[2]);
		exit(0);	
	}
	else
	wait(&status);
}

void read2(char * file, char * stringa)
{
	printf("%s, %s\n",file, stringa);
	execl("/usr/bin/grep", "grep", "-c", stringa, file, (char *)0);
		
}
Why I don't see any outputs on the screen when I run my program ?
usying any .txt file I should get on stdout ( screen ) the number of lines that "stringa" shows up. But I don't.

What am I doing wrong ?
Wow, it works fine here. I ran it against its own source file:
./test1 test1.c read2
and got:
test1.c, read2
3
 
Old 08-31-2012, 05:59 PM   #3
pfpietro
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
... I don't know what to do...
I've tryied everything

Last edited by pfpietro; 08-31-2012 at 06:27 PM.
 
Old 08-31-2012, 06:46 PM   #4
pfpietro
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Ok, the perror says "No such file or directory"

I don't understand because when I launch the terminal and use "ls" to list all the files, of course I see the file I'm calling the program with. However even if I change the file name for another one or either the file source I still get the same error.

grep works like that, right ? GREP -C PATTERN FILE

so my command line is execl("usr/bin/grep", "grep", "-c", stringa, file, NULL); its supposed to execute GREP -C STRING FILE
but I still get the error.

does anyone can explain me why I'm so blind that I can't see where the problem is ?
 
Old 09-02-2012, 10:21 AM   #5
pfpietro
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
anyone ?
 
Old 09-02-2012, 02:40 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Did you tried to run
Code:
$ which grep
/bin/grep
??
 
Old 09-03-2012, 01:10 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Well, I meant that /usr/bin/grep is a wrong path to grep binary. It should probably be /bin/grep.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with fork and execl rgscott Programming 1 07-12-2012 07:31 AM
execl problem jodie Linux - Newbie 1 04-27-2008 02:36 PM
execl question kev000 Programming 3 05-09-2007 08:17 AM
quick grep question provkitir Linux - Software 4 11-25-2004 09:32 PM
Quick grep question player_2 Linux - Software 2 10-10-2003 06:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration