bash - 如何将which命令的结果通过管道传输到cd

发布于 2024-09-13 02:42:39 字数 415 浏览 11 评论 0原文

如何将 which 命令的结果传送到 cd

这就是我想做的:

which oracle | cd
cd < which oracle

但它们都不起作用。

有没有办法实现这一点(当然不是复制/粘贴)?

编辑:再想一想,此命令会失败,因为目标文件不是文件夹/目录

所以我正在思考并制定出一种更好的方法来摆脱尾随的“/oracle”部分(sed或awk,甚至Perl):)

编辑: 好吧,这就是我最终得到的:

cd `which oracle | sed 's/\/oracle//g'`

How could I pipe the result from a which command to cd?

This is what I am trying to do:

which oracle | cd
cd < which oracle

But none of them works.

Is there a way to achieve this (rather than copy/paste of course)?

Edit : on second thought, this command would fail, because the destination file is NOT a folder/directory.

So I am thinking and working out a better way to get rid of the trailing "/oracle" part now (sed or awk, or even Perl) :)

Edit :
Okay that's what I've got in the end:

cd `which oracle | sed 's/\/oracle//g'`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泪痕残 2024-09-20 02:42:39

如果命令需要来自标准输入的参数,则可以使用管道。 (更多信息)。

使用 cd 命令时情况并非如此。目录是命令参数。在这种情况下,您可以使用命令替换。使用反引号或 $(...) 来评估命令,将其存储到变量中..

path=`which oracle`
echo $path # just for debug
cd $path

尽管它可以以更简单的方式完成:

cd `which oracle` 

或者如果您的路径有特殊字符

cd "`which oracle`"

cd $(which oracle)

相当于反引号表示法,但建议使用(反引号可能与撇号混淆)

..但它看起来像您想要的:(

cd $(dirname $(which oracle))

这表明您可以轻松使用嵌套)

$(...)(以及作为反引号)也适用于双引号字符串,这在结果最终可能包含空格时有所帮助。

cd "$(dirname "$(which oracle)")"

(请注意,两个输出都需要一组双引号。)

You use pipe in cases where the command expects parameters from the standard input. ( More on this ).

With cd command that is not the case. The directory is the command argument. In such case, you can use command substitution. Use backticks or $(...) to evaluate the command, store it into variable..

path=`which oracle`
echo $path # just for debug
cd $path

although it can be done in a much simpler way:

cd `which oracle` 

or if your path has special characters

cd "`which oracle`"

or

cd $(which oracle)

which is equivalent to backtick notation, but is recommended (backticks can be confused with apostrophes)

.. but it looks like you want:

cd $(dirname $(which oracle))

(which shows you that you can use nesting easily)

$(...) (as well as backticks) work also in double-quoted strings, which helps when the result may eventually contain spaces..

cd "$(dirname "$(which oracle)")"

(Note that both outputs require a set of double quotes.)

恰似旧人归 2024-09-20 02:42:39

使用 dirname 获取目录:

cd $(which oracle | xargs dirname)

编辑:注意包含空格的路径,请参阅下面的 @anishpatel 评论

With dirname to get the directory:

cd $(which oracle | xargs dirname)

EDIT: beware of paths containing spaces, see @anishpatel comment below

看透却不说透 2024-09-20 02:42:39
cd `which oracle`

请注意这些是反引号(通常是美式键盘上 1 左侧的键)

cd `which oracle`

Note those are backticks (generally the key to the left of 1 on a US keyboard)

静谧 2024-09-20 02:42:39

好的,这里有一个使用正确引用的解决方案:

cd "$(dirname "$(which oracle)")"

避免反引号,它们的可读性较差,并且始终引用进程替换。

OK, here a solution that uses correct quoting:

cd "$(dirname "$(which oracle)")"

Avoid backticks, they are less readable, and always quote process substitutions.

在风中等你 2024-09-20 02:42:39

你不需要管道,你可以使用 Bash 参数扩展来做你想做的事!

进一步提示:如果您使用的是 Bash,请使用“type -P”而不是外部“which”命令。

# test
touch /ls
chmod +x /ls
cmd='ls'
PATH=/:$PATH
if cmdpath="$(type -P "$cmd")" && cmdpath="${cmdpath%/*}" ; then
   cd "${cmdpath:-/}" || { echo "Could not cd to: ${cmdpath:-/}"; exit 1; }
else
   echo "No such program in PATH search directories: ${cmd}"
   exit 1
fi

You don't need a pipe, you can do what you want using Bash parameter expansion!

Further tip: use "type -P" instead of the external "which" command if you are using Bash.

# test
touch /ls
chmod +x /ls
cmd='ls'
PATH=/:$PATH
if cmdpath="$(type -P "$cmd")" && cmdpath="${cmdpath%/*}" ; then
   cd "${cmdpath:-/}" || { echo "Could not cd to: ${cmdpath:-/}"; exit 1; }
else
   echo "No such program in PATH search directories: ${cmd}"
   exit 1
fi
剧终人散尽 2024-09-20 02:42:39

除了上面的好答案之外,需要提到的一件事是 cd 是一个内置的 shell,它在同一个进程中运行,而不是像 ls 这样的新进程,它是一个命令。

  1. https ://unix.stackexchange.com/questions/50022/why-cant-i-redirect-a-path-name-output-from-one-command-to-cd

  2. http://en.wikipedia.org/wiki/Shell_builtin

besides good answer above, one thing needs to mention is that cd is a shell builtin, which run in the same process other than new process like ls which is a command.

  1. https://unix.stackexchange.com/questions/50022/why-cant-i-redirect-a-path-name-output-from-one-command-to-cd

  2. http://en.wikipedia.org/wiki/Shell_builtin

静若繁花 2024-09-20 02:42:39

为了回答您编辑的问题,您可以使用 dirname 去掉命令的名称:

cd $(dirname `which oracle`)

In response to your edited question, you can strip off the name of the command using dirname:

cd $(dirname `which oracle`)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文