目标:
我想创建一个有用的快捷方式,用于在本地初始化git仓库,并同时在Bitbucket或Github上创建远程仓库origin。这个函数被添加到了我的主目录下的.bashrc
文件中。
以下是Bash函数(会产生错误):
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://git@bitbucket.org/$2/$3"
# Use github if specified
if [ "$1" == "gh" ]; then
API="https://api.github.com/user/repos"
DATA='{"name":"$3", "description": "$4"}'
REMOTE="git@github.com:$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
}
错误信息:
username@COMPUTER-NAME /path/to/local/repo
$ initg gh username bash_test desc
sh: [: missing `]'
sh: [: missing `]'
Enter host password for user 'username':
我的问题:
首先,我的错误在哪里?其次,我该如何改进此脚本的控制流程或结构,以实现所述目标?
bash -x initg gh username bash_test desc
命令来查找哪些行触发了错误,并使用[[ ]]
语法而不是[]
(例如 我的一个脚本 中的用法)。 - VonCtest.sh
,然后调用bash -x test.sh
。 - devnull$1
等于 "gh" 时,由于它们出现在单引号中,因此在设置DATA
的值时不会扩展$3
和$4
。这可能会在稍后未使用引号的情况下调用curl
时或可能(因为[
相关错误)与-z
一起使用$DATA
时导致问题。 - chepner