I am writing a post-update script for a git repository and am having a bit of trouble.
For whatever reason the if statement is throwing an error:
hooks/post-update: line 5: [master: command not found
I simply want to check and see if the pushed branch is equal to “master”. I am guessing there is something wrong with my syntax, this is my first sh script, but I’ve tried several variations with no luck.
Thanks for any help,
#!/bin/sh
BRANCH=$(git rev-parse --symbolic --abbrev-ref $1)
if ["$BRANCH" = "master"]
then
cd $HOME/domain.com || exit
else
cd $HOME/dev.domain.com || exit
fi
unset GIT_DIR
git pull hub $BRANCH
git checkout $BRANCH
exec git-update-server-info
The square brackets must be separate tokens! You see, it tries to look like nice syntax, but the fact is, that there is a (built-in in most shells) command
[. So the syntax should be:Without the first space the command becomes
[masterand such command does not exist — the command is[. Without the second space, the right argument will bemaster]and won’t match (and than[will fail with “missing]argument” error.Detailed description:
The shell
ifsyntax tries to be as simple as possible. It really iswhere each command has to be terminated either by semicolon or end of line. So you can write things like
Now in many cases you need to do things like compare strings, compare numbers, test whether file exists, test whether one file is newer than another etc. For this there is a standard command
test(which is built-in in most shells, but external version always exists). It allows things likeThis is what you use in if. For sake of nicer syntax, this command has alias
[which accepts exactly the same arguments, except it requires]as it’s last argument so the condition looks pretty. This is also built-in in most shells, but usually exists as symlink totesttoo. So don’t be fooled by what looks like special syntax; it is simple program invocation.