我通过大量视频观看了出色的 shell 脚本课程。既然我认为我对 Bourne shell 相当熟悉,我决定编写我的第一个 shell 脚本。
脚本目标:检查 git 工作目录是否干净。如果是这样,请将工作目录覆盖到名为deployment
. 最后,将部署分支推送到源站。
我最终得到了这段代码:
#!/bin/sh
######################################################
# Deploys working directory to git deployment branch.
# Requires that the working directory is clean.
######################################################
#check if the working directory is clean
if [ git diff-index --quiet HEAD ]
then
if [ git branch -f deployment ]
then
if [ git push origin deployment ]
then
echo
echo "OK. Successfully deployed to git deployment branch."
echo
exit 0 #success
else
echo
echo "Error: failed to push deployment branch to origin."
echo
exit 1 #failure
fi
else
echo
echo "Error: failed to create or overwrite deployment branch."
echo
exit 1 #failure
fi
else
echo
git status #show the status of the working directory
echo
echo "Error: working directory is not clean. Commit your changes first..."
echo
exit 1 #failure
fi
不幸的是,这似乎给了我一个错误:./tools/deploygit: 9: [: git: unexpected operator
为什么会这样?我在使用什么运算符if [ git diff-index --quiet HEAD ]
是出乎意料的?
作为奖励,您对如何提高此脚本的效率、逻辑或可读性有任何建议或提示吗?