如何从 Linux shell 运行与当前工作目录不同的程序?
使用Linux shell,如何使用与当前工作目录不同的工作目录启动程序?
例如,我有一个二进制文件 helloworld
,它在当前目录 中创建文件 hello-world.txt
。
该文件位于目录 /a
内。
目前,我位于目录 /b
中。 我想启动我的程序运行 ../a/helloworld
并在第三个目录 /c
中的某个位置获取 hello-world.txt
。
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 the directory /b
. I want to start my program running ../a/helloworld
and get the hello-world.txt
somewhere in a third directory /c
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
像这样调用程序:
括号导致生成一个子 shell。 然后,该子 shell 将其工作目录更改为
/c
,然后从/a
执行helloworld
。 程序退出后,子 shell 终止,返回到父 shell 的提示符,位于您启动的目录中。错误处理:为了避免在未更改目录的情况下运行程序,例如拼写错误
/c
时,请将helloworld
的执行设置为有条件的:< strong>减少内存使用:为了避免在 hello world 执行时子 shell 浪费内存,请通过 exec 调用
helloworld
:[感谢 Josh 和 Juliano 提供了有关以下方面的提示改进这个答案!]
Call the program like this:
The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to
/c
, then executeshelloworld
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 ofhelloworld
conditional:Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call
helloworld
via exec:[Thanks to Josh and Juliano for giving tips on improving this answer!]
类似于 David Schmitt 的回答,加上 Josh 的建议,但不会让 shell 进程保持运行:
这种方式更类似于您通常在 shell 上运行命令的方式。 要查看实际差异,您必须使用每个解决方案从另一个 shell 运行
ps ef
。Similar to David Schmitt's answer, plus Josh's suggestion, but doesn't leave a shell process running:
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.使用 Pushd/popd 和子 shell:
如果您不需要子 shell,您可以将它们作为单独的命令运行,但请注意,如果其中任何一个失败,您的 shell 最后可能不会返回到原始目录:
演示:
Using pushd/popd and a sub shell:
If you don't want a subshell, you could run them as separate commands, but be aware that if any of them fail, your shell might not end up back in the original directory at the end:
Demo:
只需更改最后一个“&&” 进入 ”;” 无论命令失败还是成功,它都会 cd 返回:
Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:
我一直认为 UNIX 工具应该写成过滤器,从 stdin 读取输入并将输出写入 stdout。 如果可能,您可以更改 helloworld 二进制文件以将文本文件的内容写入 stdout 而不是特定文件。 这样您就可以使用 shell 在任何地方写入文件。
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 SOME_PATH && run_some_command && cd -
最后一个“cd”命令将带您返回到最后一个 pwd 目录。 这应该适用于所有 *nix 系统。
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.
一种方法是创建一个包装 shell 脚本。
shell 脚本会将当前目录更改为 /c,然后运行 /a/helloworld。 shell 脚本退出后,当前目录将恢复到 /b。
下面是一个 bash shell 脚本示例:
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:
以下内容满足了我设置别名的简单需求,我可以在其中附加参数,而无需将指令放入函数中; 在 Bash 和 ZSH 中工作:
我的用例:我有一个名为
run-service
的 shell 脚本,它根据自己的位置解析其他文件的路径。 如果我用 cd ~/Code/company/scripts && 来调用它 run-service dev/some-service,它会期望在~/Code/company/services/dev/some-service
中找到一个配置文件。 因此,我不必总是cd
进入目录并调用脚本,而是这样做:它可能太简单而无法通用,但它适用于我的基本用例。
The following worked for my simple need to set up an alias to which I can append an argument, without having to put the instructions in a function; worked in Bash and ZSH:
My use case: I have a shell script named
run-service
that resolves paths to other files based on its own location. If I call it withcd ~/Code/company/scripts && run-service dev/some-service
, it will expect to find a config file in~/Code/company/services/dev/some-service
. So instead of always having tocd
into the directory and calling the script, I just did this:It's probably too simplistic to be of general use, but it works for my basic use-case.
如果您始终希望它位于 /C,请在写入文件时使用绝对路径。
If you always want it to go to /C, use an absolute path when you write the file.
如果您想在程序中执行此操作,那么我会执行以下操作:
If you want to perform this inside your program then I would do something like: