从 bash 中的 URL 解析出数字并输入到另一个 bash 函数中?

rya*_*342 2 bash shell-script github

https://github.com/org/repo/pull/777

我试图解析 bash 函数 1 中该 url 的最后一个数字,并将该结果传递给 bash 函数 2。

我有一个我一直在玩的基本例子:

  1 pullpr() {
  2         git checkout pr/$1
  3 }
  4
  5 co() {
  6         pullpr awk -F/ '{print $7}' <<<$1
  7 }
Run Code Online (Sandbox Code Playgroud)

我似乎无法让它发挥作用,但我想以正确的方式做到这一点。

Ste*_*itt 5

您可以使用 shell\xe2\x80\x99s参数扩展功能来提取数字:

\n\n
url=https://github.com/org/repo/pull/777\necho "${url##*/}"\n
Run Code Online (Sandbox Code Playgroud)\n\n

这也适用于位置参数:

\n\n
set https://github.com/org/repo/pull/777\necho "${1##*/}"\n
Run Code Online (Sandbox Code Playgroud)\n