可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Using a Linux shell, how do I start a program with a different working directory from the current working directory?
For example, I have a binary file helloworld
that creates the file hello-world.txt
in the current directory. This file is inside of directory /a
. Currently I am in directory /b
. I want to start my program running ../a/helloworld
and get the hello-world.txt
somewhere in a third directory /c
.
回答1:
Call the program like this:
(cd /c; /a/helloworld)
The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c
, then executes helloworld
from /a
. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.
Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c
, make the execution of helloworld
conditional:
(cd /c && /a/helloworld)
Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld
via exec:
(cd /c && exec /a/helloworld)
[Thanks to Josh and Juliano for giving tips on improving this answer!]
回答2:
Similar to David Schmitt's answer, plus Josh's suggestion, but doesn't leave a shell process running:
(cd /c && exec /a/helloworld)
This way is more similar to how you usually run commands on the shell. To see the practical difference, you have to run ps ef
from another shell with each solution.
回答3:
sh -c 'cd /c && ../a/helloworld'
回答4:
An option which doesn't require a subshell and is built in to bash
(pushd SOME_PATH && run_stuff; popd)
Demo:
$ pwd
/home/abhijit
$ pushd /tmp # directory changed
$ pwd
/tmp
$ popd
$ pwd
/home/abhijit
回答5:
I always think UNIX tools should be written as filters, read input from stdin and write output to stdout. If possible you could change your helloworld binary to write the contents of the text file to stdout rather than a specific file. That way you can use the shell to write your file anywhere.
$ cd ~/b
$ ~/a/helloworld > ~/c/helloworld.txt
回答6:
Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:
cd SOME_PATH && run_some_command ; cd -
回答7:
One way to do that is to create a wrapper shell script.
The shell script would change the current directory to /c, then run /a/helloworld. Once the shell script exits, the current directory reverts back to /b.
Here's a bash shell script example:
#!/bin/bash
cd /c
/a/helloworld
回答8:
If you always want it to go to /C, use an absolute path when you write the file.
回答9:
why not keep it simple
cd SOME_PATH && run_some_command && cd -
the last 'cd' command will take you back to the last pwd directory. This should work on all *nix systems.
回答10:
If you want to perform this inside your program then I would do something like:
#include <unistd.h>
int main()
{
if(chdir("/c") < 0 )
{
printf("Failed\n");
return -1 ;
}
// rest of your program...
}
回答11:
from the current directory provide the full path to the script directory to execute the command
/root/server/user/home/bin/script.sh