Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何运行这个bash脚本需要同时添加两条注释_Bash_Alias - Fatal编程技术网

如何运行这个bash脚本需要同时添加两条注释

如何运行这个bash脚本需要同时添加两条注释,bash,alias,Bash,Alias,我想在浏览器中打开链接我可以获得与此脚本第二部分的链接,但我只需要添加xdg open,就像前缀一样您可以使用两个由分隔的命令在别名中: 别名e=“echo 1;echo 2” E 1. 2. 但是看起来您想使用git config的输出作为xdh open的输入。在这种情况下,您需要使用$(…)。将三个sed命令合并到一个命令中得到: alias git repo=“xdg open$(git config--get remote.origin.url|sed-e's//://\//\//g

我想在浏览器中打开链接我可以获得与此脚本第二部分的链接,但我只需要添加
xdg open
,就像前缀一样您可以使用两个由
分隔的命令在别名中:

别名e=“echo 1;echo 2” E 1. 2.
但是看起来您想使用
git config的输出作为
xdh open
的输入。在这种情况下,您需要使用
$(…)
。将三个
sed
命令合并到一个命令中得到:

alias git repo=“xdg open$(git config--get remote.origin.url|sed-e's//://\//\//g'-e's/ssh\//\//\//\//g'-e's/git@/https:\//\//\//g')”
但是,如果您这样定义别名,那么
git config
将在您定义别名时运行,您可能希望在实际使用别名时发生这种情况。所以用单引号引起来:

alias git-repo="xdg-open" + "git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g'"
正如@KamilCuk在评论中所说,如果变得更复杂,shell脚本或函数可能会更好:

函数git\u repo{ url=$(git config--get remote.origin.url | sed-e's/://\//g'-e's/ssh\//\//\//\//\//g'-e's/git@/https:\//\//\//g') 回显“$url” xdg打开“$url” }
您可以将函数放入例如
~/.bashrc
或任何其他
源文件中

作为脚本:

alias git-repo="xdg-open '$(git config --get remote.origin.url | sed -e 's/:/\//g' -e 's/ssh\/\/\///g' -e 's/git@/https:\/\//g')'"

这更易于阅读和使用,您的编辑器可以进行语法突出显示,并在出现问题时向您发出警告。

请提供一个提示。git配置的输出是什么?仅供参考,
+
在定义别名时无效。为什么要使用
别名
?只需使用一个函数即可?谢谢兄弟,至少我是这样使用的
别名git repo=“git config--get remote.origin.url | sed-e's/ssh\//\//\//g';sed-e's/git@/https:\//\//\//g';xdg open'$(git-config--get remote origin.url | sed-e's//://\//\//\//\//\//\//g'-e's/git://\//\'/https''
这将在重定向我之后在终端中给出第一个URL。但有没有更简短的解决办法,就是我这样用。比如声明了一个变量,比如URL=“string”??
set -eu
url=$(git config --get remote.origin.url | sed -e 's/:/\//g' -e 's/ssh\/\/\///g' -e 's/git@/https:\/\//g')
echo "$url"
xdg-open "$url"