Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash shell script error sh: [: missing `]'

Aim:

I'm trying to create a useful shortcut for initializing a git repo locally, and simultaneously creating a remote repo origin on bitbucket or github. This function is added to my .bashrc file in my home directory.

Here is the bash function (which yields an error):

function initg() {
 # Defaults to bitbucket API
 local API="https://api.bitbucket.org/1.0/repositories/"
 local DATA="name=$3&description=$4&is_private=true&scm=git"
 local REMOTE="ssh://[email protected]/$2/$3"

 # Use github if specified
 if [ "$1" == "gh" ]; then
      API="https://api.github.com/user/repos"
      DATA='{"name":"$3", "description": "$4"}'
      REMOTE="[email protected]:$2/$3"
 fi

 if [ -z "$API" ] || [ -z "$DATA" ] || [ -z "$REMOTE" ] || [ -z "$2" ]
 then
      echo "A parameter is missing or incorrect"
 else
      curl -X POST -u $2 ${API} -d ${DATA}
      git init
      git add .
      git commit -m "Created repo"
      git remote add origin $REMOTE
      git push -u origin master
 fi
}

The error:

username@COMPUTER-NAME /path/to/local/repo
$ initg gh username bash_test desc
sh: [: missing `]'
sh: [: missing `]'
Enter host password for user 'username':

My Question:

First, where is my error? Secondly, how might I improve the control flow or structure of this script to achieve the stated goals?

like image 414
cjross Avatar asked Apr 06 '13 09:04

cjross


1 Answers

I've got that kind of error because I didn't use space before ] ex:

if [ "$#" -ne 2]; then

instead of

if [ "$#" -ne 2 ]; then

PS: I know this is an old question but it might do some help for someone :)

like image 166
Repky Avatar answered Sep 22 '22 03:09

Repky