Command line app: Unix cd command -


my mac os command line application making unix calls such as:

system("rm -rf /users/stu/developer/file); 

perfectly successfully.

so why following not changing current directory?

system("cd /users/me/whatever"); system("pwd");    //cd has not changed 

because

system() executes command specified in command calling /bin/sh -c command, , returns after command has been completed.

so each command executed independently, each in new instance of shell.

so first call spawns new sh (with current working directory), changes directories, , exits. second call spawns new sh (again in cwd).

see man page system().


the better solution to not use system. has inherent flaws can leave open security vulnerabilities. instead of executing system() commands, should use equivalent posix c functions. everything can command-line, can c functions (how think utilities work?)

  • instead of system("rm -rf ...") use this.
  • instead of system("cd ...") use chdir().
  • instead of system("pwd ...") use getcwd().

there differences, of course, these fundamental equivalents of you're trying do.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -