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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-02-2004, 10:05 AM
|
#1
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Rep:
|
execlp command syntax help
I'm forking a process to run a backup command. I've been trying to figure out how to use one of the "exec" commands to do the equivalent of the code below:
Code:
tar czf /tmp/2004Sep01.tar.gz `find /data/local/2004/Sep/01 -daystart -cmin +1420 -cmin -2290`
. . . but I'm having trouble figuring this out.
To start out, I've tried to figure out how to just do the find command part, but this attempt doesn't work:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
int main () {
g_print("Executing command\n");
execlp("find", "find", "/data/local/2004/Sep/01", "-daystart", "-cmin", "+1000", "-cmin", "+2000", ">/tmp/find.text", NULL);
g_print("End of command\n");
return EXIT_SUCCESS;
}
Could you please help me figure out how to properly use one of the "exec" commands to do what I want?
Possibly running a tar/find command isn't what I should be doing? Is there a "C" way to do it?
--Tony
Last edited by tonyfreeman; 09-02-2004 at 11:21 AM.
|
|
|
09-02-2004, 01:25 PM
|
#2
|
Member
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190
Rep:
|
man 3 exec
One thing I noticed is that the first arguement of execlp() is the filename of the file to be executed, maybe use the full pathname (/usr/bin/find). And I don't believe you need the "find" twice, just once will do.
Last edited by KneeLess; 09-02-2004 at 01:28 PM.
|
|
|
09-02-2004, 01:39 PM
|
#3
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Original Poster
Rep:
|
OK ... I just figured out that if I removed the ">/tmp/find.text" part .... the find command works.
Code:
execlp("find", "find", "/data/2004/Sep/01", "-daystart", "-cmin", "+1000", "-cmin", "+2000", NULL);
I wonder why it didn't like sending the output to the text file?
Anyway ... Now I need to adjust things in order to make the tar command run off of the find command.
Has anyone done this sort of thing before? Any ideas or examples I can use to do it?
Thanks for reading ;-)
--Tony
|
|
|
09-02-2004, 01:41 PM
|
#4
|
Member
Registered: May 2003
Distribution: Debian GNU/Linux 3.0 Sid, OpenBSD 3.5
Posts: 190
Rep:
|
Quote:
I wonder why it didn't like sending the output to the text file?
|
As far as I know, execlp() doesn't use bash, which I should've seen, and the last part is bash syntax.
|
|
|
09-02-2004, 08:54 PM
|
#5
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
Quote:
One thing I noticed is that the first arguement of execlp() is the filename of the file to be executed, maybe use the full pathname (/usr/bin/find). And I don't believe you need the "find" twice, just once will do.
|
You do need to have find twice, once for the program to execute and the second for the the zero argument (i.e. the name of the program).
And you don't need to use the full pathname if the command is in your path. The p family does it for you.
If you need to redirect the output then use dup2 to change the stdout to that of a file descriptor of a file.
Last edited by mirradric; 09-02-2004 at 08:58 PM.
|
|
|
09-07-2004, 09:09 AM
|
#6
|
LQ Newbie
Registered: Aug 2004
Distribution: Fedora Core 2
Posts: 16
Rep:
|
If you type at command prompt
~[1]$ mybin -myarg > mypath/myfile
Then mybin is executed with only two arguments (agrv[0]="mybin" and argv[1]="-myarg") and the shell takes care of the rest. You were sending "> mypath/myfile" as an argument to "mybin" who doesn't understand that. You should redirect the output before or in the case of find, send a parameter like "-fprintf", look at the man page for find.
R.
|
|
|
All times are GMT -5. The time now is 05:15 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|