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.
我正在尝试为在本地初始化一个git repo创建一个有用的快捷方式,并同时在bitbucket或github上创建远程repo源。这个函数被添加到我的主目录中的.bashrc文件中。
Here is the bash function (which yields an error):
这是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
}
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?
首先,我的错误在哪里?其次,如何改进该脚本的控制流或结构以实现既定目标?
4 个解决方案
#1
5
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 :)
PS:我知道这是个老问题,但可能对某人有帮助。
#2
3
Update/Provisional Answer
Okay this is strictly speaking not an (informed) answer, however I have managed to resolve the issue.
好吧,这严格来说不是一个(知情的)回答,但是我已经解决了这个问题。
Debugging
调试
I ran the function and command in a separate dedicated script file, using the command bash -x test.sh
suggested by devnull in the comments of the original post.
我使用命令bash -x测试在一个单独的专用脚本文件中运行该函数和命令。在原始帖子的评论中被devnull建议。
This yielded quite a lot of feedback in the shell. Here is what I believe to be the most critical feedback, before I aborted it.
这在shell中产生了相当多的反馈。这是我认为是最关键的反馈,在我放弃它之前。
username@COMPUTERNAME /d/test
$ bash -x test.sh
+ initg gh username bash_test desc
+ local API=https://api.bitbucket.org/1.0/repositories/
+ local 'DATA=name=bash_test&description=desc&is_private=true&scm=git'
+ local REMOTE=ssh://git@bitbucket.org/username/bash_test
+ '[' gh == gh ']'
+ API=https://api.github.com/user/repos
+ DATA='{"name":"$3", "description": "$4"}'
+ REMOTE=git@github.com:username/bash_test
+ '[' -z https://api.github.com/user/repos ']'
+ '[' -z '{"name":"$3", "description": "$4"}' ']'
+ '[' -z git@github.com:username/bash_test ']'
+ '[' -z username ']'
+ curl -X POST -u username https://api.github.com/user/repos -d '{"name":"$3",' '"description":' '"$4"}'
+ Enter host password for user 'username':
The Issue:
问题:
So the issue seems to lie with the variable expression (below) where DATA is set. Firstly the arguments passed to the script, referenced by $3
and $4
cannot be interpolated within a single quoted string.
因此,问题似乎在于数据被设置的变量表达式(下面)。首先,传递给脚本的参数($3和$4)不能在一个引用的字符串中插入。
DATA='{"name":"$3", "description": "$4"}'
Also the spaces after the comma and second semi-colon strangely wrapped in single quotes breaking the string in several pieces, visible in the resulting failed curl call.
还有逗号和第二分号后面的空格,奇怪地用单引号括起来,将字符串分成几段,在结果失败的curl调用中可见。
Resolution
决议
So apart from using [[ ]]
double square brackets instead of the original singular square brackets, I simply wrapped each interpolated variable with curly braces such as ${3}
and fixed the use of quotes and spaces in the DATA variable assignment (below):
因此,除了使用[[]]双方括号代替原来的奇异方括号外,我简单地将每个插值变量用花括号括起来,例如${3},并固定在数据变量赋值(下)中使用引号和空格:
DATA="{\"name\":\"${3}\",\"description\":\"${4}\"}"
Post script
发布脚本
I this answer can be improved, if anyone can extend on this answer that would be great. I'm confused as to why the $DATA
variable used in the curl call resolved to '{"name":"$3",' '"description":' '"$4"}'
我这个答案是可以改进的,如果任何人都能回答这个问题,那就太好了。我搞不清楚为什么curl调用中使用的$DATA变量解析为" {"name":"$3", "description": "$4"}
#3
3
I've run into such nonsense from bash several times (a bug?), and the way out was to replace [ some-condition ]
by test some-condition
我已经遇到过几次这样的废话(bug?),而解决问题的方法就是通过测试某种条件来替换(某些条件)。
#4
0
To feed DATA
variable, try this :
若要输入数据变量,请尝试:
printf -vDATA '{"name":"%s", "description": "%s"}' $3 $4
echo "$DATA"
#1
5
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 :)
PS:我知道这是个老问题,但可能对某人有帮助。
#2
3
Update/Provisional Answer
Okay this is strictly speaking not an (informed) answer, however I have managed to resolve the issue.
好吧,这严格来说不是一个(知情的)回答,但是我已经解决了这个问题。
Debugging
调试
I ran the function and command in a separate dedicated script file, using the command bash -x test.sh
suggested by devnull in the comments of the original post.
我使用命令bash -x测试在一个单独的专用脚本文件中运行该函数和命令。在原始帖子的评论中被devnull建议。
This yielded quite a lot of feedback in the shell. Here is what I believe to be the most critical feedback, before I aborted it.
这在shell中产生了相当多的反馈。这是我认为是最关键的反馈,在我放弃它之前。
username@COMPUTERNAME /d/test
$ bash -x test.sh
+ initg gh username bash_test desc
+ local API=https://api.bitbucket.org/1.0/repositories/
+ local 'DATA=name=bash_test&description=desc&is_private=true&scm=git'
+ local REMOTE=ssh://git@bitbucket.org/username/bash_test
+ '[' gh == gh ']'
+ API=https://api.github.com/user/repos
+ DATA='{"name":"$3", "description": "$4"}'
+ REMOTE=git@github.com:username/bash_test
+ '[' -z https://api.github.com/user/repos ']'
+ '[' -z '{"name":"$3", "description": "$4"}' ']'
+ '[' -z git@github.com:username/bash_test ']'
+ '[' -z username ']'
+ curl -X POST -u username https://api.github.com/user/repos -d '{"name":"$3",' '"description":' '"$4"}'
+ Enter host password for user 'username':
The Issue:
问题:
So the issue seems to lie with the variable expression (below) where DATA is set. Firstly the arguments passed to the script, referenced by $3
and $4
cannot be interpolated within a single quoted string.
因此,问题似乎在于数据被设置的变量表达式(下面)。首先,传递给脚本的参数($3和$4)不能在一个引用的字符串中插入。
DATA='{"name":"$3", "description": "$4"}'
Also the spaces after the comma and second semi-colon strangely wrapped in single quotes breaking the string in several pieces, visible in the resulting failed curl call.
还有逗号和第二分号后面的空格,奇怪地用单引号括起来,将字符串分成几段,在结果失败的curl调用中可见。
Resolution
决议
So apart from using [[ ]]
double square brackets instead of the original singular square brackets, I simply wrapped each interpolated variable with curly braces such as ${3}
and fixed the use of quotes and spaces in the DATA variable assignment (below):
因此,除了使用[[]]双方括号代替原来的奇异方括号外,我简单地将每个插值变量用花括号括起来,例如${3},并固定在数据变量赋值(下)中使用引号和空格:
DATA="{\"name\":\"${3}\",\"description\":\"${4}\"}"
Post script
发布脚本
I this answer can be improved, if anyone can extend on this answer that would be great. I'm confused as to why the $DATA
variable used in the curl call resolved to '{"name":"$3",' '"description":' '"$4"}'
我这个答案是可以改进的,如果任何人都能回答这个问题,那就太好了。我搞不清楚为什么curl调用中使用的$DATA变量解析为" {"name":"$3", "description": "$4"}
#3
3
I've run into such nonsense from bash several times (a bug?), and the way out was to replace [ some-condition ]
by test some-condition
我已经遇到过几次这样的废话(bug?),而解决问题的方法就是通过测试某种条件来替换(某些条件)。
#4
0
To feed DATA
variable, try this :
若要输入数据变量,请尝试:
printf -vDATA '{"name":"%s", "description": "%s"}' $3 $4
echo "$DATA"