Bash-意外的文件结尾 [英] Bash - Unexpected End of File

查看:79
本文介绍了Bash-意外的文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究bash脚本以在程序上运行测试,但似乎找不到语法错误.当我使用-x时,它告诉我期望使用},但找不到.

I've been working on a bash script to run tests on a program and I can't seem to be able to find the syntax error. When I use -x, it tells me it is expecting a } but I can't find it.

请参见下面的代码.

#!/bin/bash
usagearg() {
    echo "You're missing an argument on the command line!" >&2}
usagemiss() {
    echo "A file requested in your filestem is missing or cannot be read!" >&2}
if [ ${#} -ne 2 ]; then
    usagearg;
    exit 1;
fi
x=1
endp=`wc -l ${1}`
end=$((endp+1))
while [ ${x} -ne ${end} ] ; do
    # redacted code which isn't related to the issue at hand.
done

我觉得我已经关闭了所有循环,if和所有括号,所以我不明白为什么会出现语法错误.

I feel like I've closed all the loops and ifs, and all the brackets, so I don't understand why I'm getting the syntax error.

推荐答案

复合命令的花括号内的命令列表必须以分号或换行符结尾;右括号本身是不够的.

The list of commands inside the braces for the compound command must be terminated by either a semicolon or a newline; the closing brace itself is not sufficient.

可以使用

usagearg() {
    echo "You're missing an argument on the command line!" >&2; }

usagearg() {
    echo "You're missing an argument on the command line!" >&2
}

按照编写的方式,您的代码将右花括号仅视为另一个字符,并且由于没有中间的空格,因此将其作为输出重定向的一部分.

As written, your code treats the right brace as simply another character, and part of the output redirection since there is no intervening whitespace.

为什么需要这样做,必须回到bash定义某些字符的方式.有一组元字符,当不加引号时,它们可以分隔单词.还有一组控制运算符,它们被模糊地定义为执行控制功能".右括号}不在这两个类别中. (为什么?我不确定,但是我认为这与在参数扩展(${foo})中使用花括号有关,这使花括号无法进行其他特殊处理.)

As to why this is necessary, you have to go back to how bash defines certain characters. There is the set of metacharacters, which can separate words when unquoted. There is also the set of control operators, which are vaguely defined as performing a "control function". The right brace } is in neither category. (Why? I'm not sure, but I think it's related to the use of braces in parameter expansion (${foo}) which preclude it having otherwise special handling.)

这篇关于Bash-意外的文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆