【发布时间】:2012-05-14 18:25:42
【问题描述】:
我通过大量videos 观看了出色的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 ] 中使用了哪些意外的运算符?
作为奖励,您对如何提高此脚本的效率、逻辑或可读性有任何建议或技巧吗?
【问题讨论】:
-
从逻辑上讲,如果你这样做会更容易理解: die() { echo "ERROR: $*" >&2; 1号出口; }; git diff-index --quiet HEAD ||工作目录不干净; git 分支 -f 部署 || die 未能创建或覆盖部署分支 ...
-
顺便说一句——你在哪个平台上?
/bin/sh是 POSIX sh,而不是 Bourne,几乎在过去二十年内构建的所有操作系统上,Sun 都是唯一的落后者。 (Bash 的名字暗示 Bourne 是它的前任,但这是一个牺牲准确性以支持双关语的例子)。
标签: debugging bash shell unix sh