[英]"git push origin master" gives weird behavior in bash script
I have this simple function in my bashrc file我的 bashrc 文件中有这个简单的函数
function aupgrade {
cat ~/.bash_aliases > ~/bash/.bash_aliases
cd ~/bash
git add .
if [[ $1 == "" ]]; then
git commit -m "Update"
else
git commit -m "$1"
fi
git push origin master
cd - 1>/dev/null
}
This function has a purpose, this is the expected behavior:
这个函数有一个目的,这是预期的行为:
First , replace the content of the .bash_aliases file in the bash repository with the stdout of cat ~/.bash_aliases
首先,将 bash 存储库中 .bash_aliases 文件的内容替换为
cat ~/.bash_aliases
的标准输出
Second , go into the ~/bash
directory which is a git respository其次,进入
~/bash
目录,这是一个 git 存储库
Third , stage all changes三、阶段性变化
Fourth , if when calling the aupgrade
function the following argument is nothing, just commit with the "Update" message, but if the user wrote an argument, like aupgrade "New commit!"
第四,如果在调用
aupgrade
函数时下面的参数没有任何内容,只需提交“更新”消息,但如果用户写了一个参数,比如aupgrade "New commit!"
, commit the changes with such argument as the message, git commit -m $1
, 使用诸如消息之类的参数提交更改,
git commit -m $1
Fifth , push the changes五、推动变化
Sixth , go back to the previous directory六、回到上一目录
BUT, it doesnt do that, instead it just does:
但是,它不会那样做,而是会那样做:
First , replace the content of the .bash_aliases file in the bash repository with the stdout of cat ~/.bash_aliases
首先,将 bash 存储库中 .bash_aliases 文件的内容替换为
cat ~/.bash_aliases
的标准输出
Second , go into the ~/bash
directory which is a git respository其次,进入
~/bash
目录,这是一个 git 存储库
Third , stage all changes三、阶段性变化
Fourth , commit with the "Update" message although there is an argument第四,尽管有争论,但提交“更新”消息
Fifth , push the changes五、推动变化
Sixth , go back to the previous directory六、回到上一目录
This is weird.这很奇怪。 This looks product of the
git push origin master
line.这看起来像是
git push origin master
line 的产物。 It's not a conditional problem, because when I wrote another function like this but without the git push origin master
line it worked as desired.这不是条件问题,因为当我编写另一个这样的函数但没有
git push origin master
line 时,它按预期工作。
Why does this happen?为什么会发生这种情况? Is there any solution?
有什么解决办法吗?
This is what set -x shows to me, it's really weird这就是 set -x 显示给我的,这真的很奇怪
So I have tested your code, and the only problem I could find is that passing your first argument as a string with multiple exclamation marks inserts the same (or previous?) command unless you escape it:所以我测试了你的代码,我能找到的唯一问题是将你的第一个参数作为带有多个感叹号的字符串传递会插入相同(或以前?)命令,除非你将它转义:
It looks like this is exactly what happened for you, but you had "set -x" ran beforehand, thus it replaced the double exclamation marks with that command.看起来这正是您遇到的情况,但是您事先运行了“set -x”,因此它用该命令替换了双感叹号。
I'd also recommend storing function arguments in named local variables for cleaner code.我还建议将函数参数存储在命名的局部变量中以获得更清晰的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.