![]() |
|
#1
|
|||
|
|||
How to make Linux shell calls in a C++ programHello,
I am writing a C++ program. I need to make some system calls, like ls, cd, etc. I can do "ls" easily by doing system("ls") but when I do system("cd"), it does not change the directory but rather stays in the same one. I have a whole directory tree that I have to traverse through during the course of my entire program but I am not able to make "cd" calls. Is there some other way of doing a cd. For example, I have a home directory and within it, I have a directory called test1 and within it I have directory test2. I am initially in "home" but when I do system("cd test1") or even system("cd home/test1") from within my C++ program, it stays in home and doesn't change to test1. So can anyone please suggest something else that I can do? I know its something very simple but I am not able to find it so far. Thanks a lot. |
|||
|
#2
|
||||
|
||||
Re: How to make Linux shell calls in a C++ programHi salmaz,
try inserting an extra /. i.e "cd home//test1" Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: How to make Linux shell calls in a C++ programyou can go up one level by calling:
//==============begin system ("cd .."); //==============end you must type it "cd -space- .." |
|
#4
|
|||
|
|||
Re: How to make Linux shell calls in a C++ programQuote:
I am assuming your Linux shell is bash as it is with all distributions that I know about. When you execute a system(...) call, here's what happens: The operating system spawns a new shell, executes whatever stuff you have in the argument's string, and then returns to the function that invoked the system() call. Environment changes that were made (including the result of the "cd") can not be passed back upstream. Sorry. In other words, each system() call starts in whatever directory it was in whenever program execution began. For test purposes (this works in Linux, or with Windows if you use cygwin/gcc) you could try the following to illustrate the point: CPP / C++ / C Code:
At the program's completion, look in directory temp, and you will find the new file and its contents will be a friendly greeting (to me). So the program did execute the "cd" thing (as it did the other system() things), it just couldn't stay there after the system() call that got it there. Instead of stringing together a bunch of shell commands with separate system() calls you could create, then invoke, a shell script that executes the sequence, or you could make a single system call that strings together the shell commands, separated by semicolons, or you could write C function calls to do whatever it is that you want to accomplish, or ... (there are probably lots of other ways). Here's the output of a run that I made with the above example program (the computer name has been changed to protect the innocent): Quote:
Regards, Dave Last edited by davekw7x : 14-Oct-2005 at 07:33.
|
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 13:30 |
| Mozilla Thunderbird | dsmith | Computer Software Forum - Linux | 9 | 01-Mar-2005 11:56 |
| Linux Kernel Upgrade Mini Howto | dsmith | Computer Software Forum - Linux | 3 | 05-Apr-2004 22:10 |
| Call a C program through Linux shell script | nuwandee | C Programming Language | 3 | 29-Mar-2004 21:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The