linux shell [: missing `]',git - Bash shell script error sh: [: missing `]' - Stack Overflow

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.shsuggested by devnull in the comments of the original post.

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.

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.

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.

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):

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"}'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值