如何从 Linux shell 运行与当前工作目录不同的程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/786376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:13:46  来源:igfitidea点击:

How do I run a program with a different working directory from current, from Linux shell?

linuxshellenvironment

提问by Anton Daneyko

Using a Linux shell, how do I start a program with a different working directory from the current working directory?

使用Linux shell,如何使用与当前工作目录不同的工作目录启动程序?

For example, I have a binary file helloworldthat creates the file hello-world.txtin the current directory.

This file is inside of directory /a.

例如,我有一个在当前目录helloworld中创建文件的二进制文件。 这个文件在目录里面。hello-world.txt

/a

Currently, I am in the directory /b. I want to start my program running ../a/helloworldand get the hello-world.txtsomewhere in a third directory /c.

目前,我在目录中/b。我想启动我的程序运行../a/helloworld并获取hello-world.txt第三个目录中的某个位置/c

采纳答案by David Schmitt

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 helloworldfrom /a. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.

括号会产生一个子shell。然后这个子 shell 将其工作目录更改为/c,然后helloworld/a. 程序退出后,子 shell 终止,使您返回到父 shell 的提示符,位于您开始的目录中。

Error handling:To avoid running the program without having changed the directory, e.g. when having misspelled /c, make the execution of helloworldconditional:

错误处理:为了避免在未更改目录的情况下运行程序,例如,当拼写错误时/c,请执行helloworld有条件的:

(cd /c && /a/helloworld)

Reducing memory usage:To avoid having the subshell waste memory while hello world executes, call helloworldvia exec:

减少内存使用:为了避免子shell在hello world执行时浪费内存,helloworld通过exec调用:

(cd /c && exec /a/helloworld)

[Thanks to Josh and Julianofor giving tips on improving this answer!]

[感谢Josh 和 Juliano提供有关改进此答案的提示!]

回答by Tom Ritter

If you always want it to go to /C, use an absolute path when you write the file.

如果您总是希望它转到 /C,请在写入文件时使用绝对路径。

回答by Jin Kim

One way to do that is to create a wrapper shell script.

一种方法是创建一个包装器 shell 脚本。

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.

shell 脚本会将当前目录更改为 /c,然后运行 ​​/a/helloworld。一旦 shell 脚本退出,当前目录将恢复到 /b。

Here's a bash shell script example:

这是一个 bash shell 脚本示例:

#!/bin/bash
cd /c
/a/helloworld

回答by mihi

sh -c 'cd /c && ../a/helloworld'

回答by Juliano

Similar to David Schmitt's answer, plus Josh's suggestion, but doesn't leave a shell process running:

类似于David Schmitt的回答,加上 Josh 的建议,但不会让 shell 进程运行:

(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 effrom another shell with each solution.

这种方式更类似于您通常在 shell 上运行命令的方式。要查看实际差异,您必须ps ef使用每个解决方案从另一个 shell运行。

回答by Harold

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...

}

回答by Luther Blisset

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.

我一直认为 UNIX 工具应该写成过滤器,从标准输入读取输入并将输出写入标准输出。如果可能,您可以更改 helloworld 二进制文件以将文本文件的内容写入 stdout 而不是特定文件。这样你就可以使用 shell 在任何地方写入你的文件。

$ cd ~/b

$ cd ~/b

$ ~/a/helloworld > ~/c/helloworld.txt

$ ~/a/helloworld > ~/c/helloworld.txt

回答by Sahil

why not keep it simple

为什么不保持简单

cd SOME_PATH && run_some_command && cd -

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.

最后一个 'cd' 命令将带您回到最后一个 pwd 目录。这应该适用于所有 *nix 系统。

回答by Loren

An option which doesn't require a subshell and is built in to bash

一个不需要子shell并内置于bash的选项

(pushd SOME_PATH && run_stuff; popd)

Demo:

演示:

$ pwd
/home/abhijit
$ pushd /tmp # directory changed
$ pwd
/tmp
$ popd
$ pwd
/home/abhijit

回答by johnnybravo

Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:

只需将最后一个“&&”改为“;” 无论命令失败还是成功,它都会 cd 返回:

cd SOME_PATH && run_some_command ; cd -