The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 09:25 AM
Why generate "ash and bash" different output for same bash script? s. murat Shell Programming and Scripting 0 05-26-2008 04:19 AM
Bash under AIX 5.3 taupin AIX 4 03-21-2008 03:03 AM
Bash: how to do it? sopel39 Shell Programming and Scripting 3 11-02-2007 01:35 AM
Bash it! Bash it! SoulForge Shell Programming and Scripting 5 11-22-2002 10:02 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-02-2007
Registered User
 

Join Date: Dec 2006
Posts: 6
Bash in C ?

Hi friends,Can iuse Bash commands in my C programs,like that

if((cd /root/Desktop/) == true)
do somethig.



I have to control a variable called path,i want to know,this path is in Unix Directories or not,Is there anybody help me?,

Thank you..

***email address removed by moderator -- reborg

Last edited by reborg; 02-02-2007 at 04:47 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 02-04-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
1. most commands are also found in the C library
remove() = rm
chdir() = cd
... etc.
2. You can execute shell commands in C using two methods:
A. system()
Code:
system("rm -f somefile");
B. popen
Code:
#include <stdio.h>
..........
FILE *cmd=popen(" find . -type -f -print","r");
char tmp[256]={0x0};
while (fgets(tmp,sizeof(tmp),cmd)!=NULL)
{
    printf("%s\n", tmp);
}
pclose(cmd);
Reply With Quote
  #3  
Old 02-04-2007
Registered User
 

Join Date: Dec 2006
Posts: 6
yes i try what you said,and it works,thank you jim mcnamara,

but i dont know,can we use our variables in system function like that?,


#include <stdio.h>

main()
{
char *path;
path = "/usr/bin/";

if(system("cd 'path'")==0) -------------------- ? my variable in
do;
else
go;
}


thank you very much again,
Reply With Quote
  #4  
Old 02-05-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,269
Code:
char cmd[80]={0x0};
char *path="/path/to/somewhere";
sprintf(cmd,"cd %s", path);
system(cmd);
Be aware: cd WILL NOT apply to the process running the C code because system creates a child process, so when it exits the cd command's effect is gone.
Use chdir() instead if you want the program to actually change working directories.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0