在 xargs 中选择位置参数

Law*_*tre 1 command-line bash

我想打开安装python的位置。检查我使用的位置。

whereis python | xargs -n 1 echo
Run Code Online (Sandbox Code Playgroud)

输出 :


/usr/bin/python3.8

/usr/bin/python3.8-config

/usr/bin/python

/usr/lib/python2.7

/usr/lib/python3.8

/usr/lib/python3.9

/etc/python3.8

/usr/local/lib/python3.8

/usr/include/python3.8
Run Code Online (Sandbox Code Playgroud)

虽然我可以复制 xdg 的位置,但我不想这样做。我想使用管道运算符并使用xdg-open. 然而,有一个问题。如何从上面的列表中选择参数。假设我想选择第三个位置。有什么办法可以做到。

我想跟随,但没有奏效。

whereis python | xargs $3 xdg-open
Run Code Online (Sandbox Code Playgroud)

ter*_*don 6

只需在将输出传递给 xargs 之前过滤输出:

whereis python | awk '{print $3}' | xargs xdg-open
Run Code Online (Sandbox Code Playgroud)

awk命令只会打印第三个单词,因此您将传递给xargs. 当然,xargs当你只有一个参数时,使用是没有意义的。也许你想要这个?

xdg-open $(whereis python | awk '{print $3}')
Run Code Online (Sandbox Code Playgroud)

或者,简单地使用whichwhich 将返回搜索字符串的第一次出现$PATH

xdg-open $(which python)
Run Code Online (Sandbox Code Playgroud)

请注意,您不能打开python使用xdg-open,因为没有图形程序,可以有效地打开一个二进制文件,这是荒谬的,更何况是一个二进制文件是一种脚本语言。