Bash shell 脚本错误 sh: [: 缺少 `]'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15849095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash shell script error sh: [: missing `]'
提问by cjross
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 存储库,同时在 bitbucket 或 github 上创建远程存储库源。此功能已添加到.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=&description=&is_private=true&scm=git"
local REMOTE="ssh://[email protected]//"
# Use github if specified
if [ "" == "gh" ]; then
API="https://api.github.com/user/repos"
DATA='{"name":"", "description": ""}'
REMOTE="[email protected]:/"
fi
if [ -z "$API" ] || [ -z "$DATA" ] || [ -z "$REMOTE" ] || [ -z "" ]
then
echo "A parameter is missing or incorrect"
else
curl -X POST -u ${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?
首先,我的错误在哪里?其次,我如何改进此脚本的控制流或结构以实现既定目标?
回答by Repky
I've got that kind of error because I didn't use space before ] ex:
我遇到了那种错误,因为我在 ] 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:我知道这是一个老问题,但它可能对某人有帮助:)
回答by cjross
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 test.sh
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://[email protected]/username/bash_test
+ '[' gh == gh ']'
+ API=https://api.github.com/user/repos
+ DATA='{"name":"", "description": ""}'
+ [email protected]:username/bash_test
+ '[' -z https://api.github.com/user/repos ']'
+ '[' -z '{"name":"", "description": ""}' ']'
+ '[' -z [email protected]:username/bash_test ']'
+ '[' -z username ']'
+ curl -X POST -u username https://api.github.com/user/repos -d '{"name":"",' '"description":' '""}'
+ 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 的变量表达式(如下)。首先,传递给脚本的参数被引用$3
并且$4
不能插入到单引号字符串中。
DATA='{"name":"", "description": ""}'
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 变量赋值中引号和空格的使用(如下):
DATA="{\"name\":\"\",\"description\":\"\"}"
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"}'
我可以改进这个答案,如果有人可以扩展这个答案,那就太好了。我很困惑为什么$DATA
curl 调用中使用的变量解析为'{"name":"$3",' '"description":' '"$4"}'
回答by vonbrand
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
我已经多次从 bash 中遇到这样的废话(一个错误?),出路是[ some-condition ]
用test some-condition
回答by Gilles Quenot
To feed DATA
variable, try this :
要提供DATA
变量,请尝试以下操作:
printf -vDATA '{"name":"%s", "description": "%s"}'
echo "$DATA"