Bash 如何在不同的目录上下文中执行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10566532/
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-09-09 22:04:07  来源:igfitidea点击:

How can Bash execute a command in a different directory context?

bashshellworking-directory

提问by Marty Wallace

I have a common command that gets called from within very specific directories. There is only one executable sitting in /bin for this program, and the current working directory is very important for running it correctly. The script affects the files that live inside the directory it is run within.

我有一个从非常特定的目录中调用的通用命令。这个程序只有一个可执行文件位于 /bin 中,当前工作目录对于正确运行它非常重要。该脚本会影响其运行所在目录中的文件。

Now, I also have a custom shell script that does some things in one directory, but I need to call that command mentioned above as if it was in another directory.

现在,我还有一个自定义的 shell 脚本,它在一个目录中执行一些操作,但是我需要调用上面提到的那个命令,就好像它在另一个目录中一样。

How do you do this in a shell script?

你如何在shell脚本中做到这一点?

采纳答案by Todd A. Jacobs

You can use the cdbuiltin, or the pushdand popdbuiltins for this purpose. For example:

您可以使用cd内置的,或者pushdpopd建宏用于这一目的。例如:

# do something with /etc as the working directory
cd /etc
:

# do something with /tmp as the working directory
cd /tmp
:

You use the builtins just like any other command, and can change directory context as many times as you like in a script.

您可以像使用任何其他命令一样使用内置命令,并且可以在脚本中根据需要多次更改目录上下文。

回答by geekosaur

Use cdin a subshell; the shorthand way to use this kind of subshell is parentheses.

cd在子shell中使用;使用这种子shell的简写方式是括号。

(cd wherever; mycommand ...)

That said, if your command has an environment that it requires, it should really ensure that environment itself instead of putting the onus on anything that might want to use it (unless it's an internal command used in very specific circumstances in the context of a well defined larger system, such that any caller already needs to ensure the environment it requires). Usually this would be some kind of shell script wrapper.

也就是说,如果你的命令有一个它需要的环境,它应该真正确保环境本身而不是把责任放在任何可能想要使用它的东西上(除非它是在井的上下文中非常特定的情况下使用的内部命令定义了更大的系统,这样任何调用者已经需要确保它需要的环境)。通常这将是某种 shell 脚本包装器。

回答by bmargulies

(cd /path/to/your/special/place;/bin/your-special-command ARGS)

回答by abc123

If you want to return to your current working directory:

如果要返回当前工作目录:

current_dir=$PWD;cd /path/to/your/command/dir;special command ARGS;cd $current_dir;
  1. We are setting a variable current_direqual to your pwd
  2. after that we are going to cdto where you need to run your command
  3. then we are running the command
  4. then we are going to cdback to our variable current_dir
  1. 我们正在设置一个变量current_dir等于你的pwd
  2. 之后我们将前往cd您需要运行命令的地方
  3. 然后我们运行命令
  4. 然后我们将cd回到我们的变量current_dir

Another Solution by @apieceofbart pushd && YOUR COMMAND && popd

@apieceofbart 的另一个解决方案 pushd && YOUR COMMAND && popd