LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-19-2010, 04:20 PM   #1
primerib
Member
 
Registered: Mar 2010
Posts: 48

Rep: Reputation: 20
Best way to convert this (bash) to c?


I was wondering how to & what the best way to convert this bash command to c would be:

Quote:
linux:$ find /dev/dvb/adapter*/frontend*
/dev/dvb/adapter0/frontend0
/dev/dvb/adapter1/frontend0
/dev/dvb/adapter3/frontend0
/dev/dvb/adapter3/frontend1
linux:~$
Doesn't matter if it's portable, just needs to work in linux. Thanks in advance.
 
Old 03-19-2010, 04:28 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
the lazy way would be:
system("find /dev/dvb/adapter*/frontend*")

there is also a readdir() function you can use.

Last edited by schneidz; 03-19-2010 at 04:31 PM.
 
Old 03-19-2010, 07:00 PM   #3
primerib
Member
 
Registered: Mar 2010
Posts: 48

Original Poster
Rep: Reputation: 20
Sorry, I should have been more clear. There are other things I need to do to that list. I thought doing the c equivalent of that bash command and putting the results into an array would be the best way. From there I can just cycle through the array and do what I need to each.
 
Old 03-19-2010, 07:07 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by primerib View Post
Sorry, I should have been more clear. There are other things I need to do to that list. I thought doing the c equivalent of that bash command and putting the results into an array would be the best way. From there I can just cycle through the array and do what I need to each.
The primitive approach is to execute the needed command and redirect its stdout to a file. Then, upon the said command completion, you can open the file by standard "C" means and read it line by line.
 
Old 03-20-2010, 02:30 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
The handy commmand to do so is popen()
Code:
FILE *cmdout = popen("find '/dev/dvb/adapter*/frontend*' -print0");
/* use getdelim() to parse out the filenames reliably */
pclose(cmdout);
I generally prefer using a pure-C approach, because if you need to use system() or popen(), you might as well use Bash or POSIX sh. Why can't it stay Bash?
 
  


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
convert tcsh script to the bash one fazanpai Linux - Software 2 11-29-2009 08:10 AM
Bash/contacts: convert vcf <=> csv ? frenchn00b Programming 1 04-26-2009 01:51 PM
convert bash scripts to Perl noir911 Programming 2 09-12-2006 08:42 PM
Convert float to integer in bash gn00kie Programming 2 07-30-2006 10:05 PM
Convert a Bash script to C Linh Programming 5 04-25-2004 12:51 PM

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

All times are GMT -5. The time now is 05:23 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