shell:意外令牌“fi”附近的语法错误

时间:2021-09-12 00:59:27

Getting syntax error near unexpected token `fi' while executing below code. below is the output:-

在执行下面的代码时,在意外的令牌“fi”中出现语法错误。下面是输出:-

: command not founde 2: 
: command not founde 7: 
run_billing.sh: line 22: syntax error near unexpected token `fi'
'un_billing.sh: line 22: `fi

The script is:

脚本:

#!/bin/sh

#
# main
#
NARGS=$#

if [ $NARGS -lt 2 ]
then
        echo "$PRG_NAME: error: incorrect number of arguments ($NARGS)";
        echo "Usage: $PRG_NAME [Time]";
    echo "Time format - pin_virtual_time format. e.g:- 062000002015.00";
    echo "Example: sh run_billing.sh 062000002015.00";
        exit 1
fi

if [ $NARGS -eq 2 ]
then
        echo "Run Billing script - pin_bill_day";
        pin_virtual_time -m2 $1;
        pin_bill_day;
fi

1 个解决方案

#1


1  

Your line endings are screwed up, each line is terminated by a CR/LF rather than just an LF.

你的线头被搞砸了,每条线都被CR/LF终止,而不是LF。

If you do something like od -c run_billing.sh, you'll see them there as \r characters as per my test script (the ^M characters are CR):

如果你做了像od -c run_billing这样的事情。承宪,你会看到他们\ r字符按我的测试脚本(CR ^米字符):

if [[ 1 -eq 1 ]]^M
then^M
    echo x is 1^M
fi^M
^M

0000000   i   f       [   [       1       -   e   q       1       ]   ]
0000020  \r  \n   t   h   e   n  \r  \n  \t   e   c   h   o       x
0000040   i   s       1  \r  \n   f   i  \r  \n  \r  \n
0000054

And, when that file is run, we see a similar issue.

当该文件运行时,我们会看到类似的问题。

That's why you're getting the weird error output, because the CR is moving the cursor to the start of the line before continuing the message, overwriting some of what it's already output.

这就是为什么您会得到奇怪的错误输出,因为在继续消息之前,CR将光标移动到行的开头,覆盖了它已经输出的部分。

For example, lines 2 and 7 of your script (the supposedly blank lines) contain a single CR character which is interpreted as a command which doesn't exist. So, for line 2, you see (superimposed):

例如,脚本的第2行和第7行(假定为空行)包含一个单一的CR字符,它被解释为不存在的命令。因此,对于第2行,您可以看到(重叠):

run_billing.sh: line 2:<CR>
: command not found
=========================== giving:
: command not founde 2:

exactly what you see.

你看到什么。

You need to modify the file to remove those CR characters, a couple of ways of doing that are given in this excellent answer.

您需要修改文件以删除这些CR字符,在这个优秀的回答中给出了几种方法。

#1


1  

Your line endings are screwed up, each line is terminated by a CR/LF rather than just an LF.

你的线头被搞砸了,每条线都被CR/LF终止,而不是LF。

If you do something like od -c run_billing.sh, you'll see them there as \r characters as per my test script (the ^M characters are CR):

如果你做了像od -c run_billing这样的事情。承宪,你会看到他们\ r字符按我的测试脚本(CR ^米字符):

if [[ 1 -eq 1 ]]^M
then^M
    echo x is 1^M
fi^M
^M

0000000   i   f       [   [       1       -   e   q       1       ]   ]
0000020  \r  \n   t   h   e   n  \r  \n  \t   e   c   h   o       x
0000040   i   s       1  \r  \n   f   i  \r  \n  \r  \n
0000054

And, when that file is run, we see a similar issue.

当该文件运行时,我们会看到类似的问题。

That's why you're getting the weird error output, because the CR is moving the cursor to the start of the line before continuing the message, overwriting some of what it's already output.

这就是为什么您会得到奇怪的错误输出,因为在继续消息之前,CR将光标移动到行的开头,覆盖了它已经输出的部分。

For example, lines 2 and 7 of your script (the supposedly blank lines) contain a single CR character which is interpreted as a command which doesn't exist. So, for line 2, you see (superimposed):

例如,脚本的第2行和第7行(假定为空行)包含一个单一的CR字符,它被解释为不存在的命令。因此,对于第2行,您可以看到(重叠):

run_billing.sh: line 2:<CR>
: command not found
=========================== giving:
: command not founde 2:

exactly what you see.

你看到什么。

You need to modify the file to remove those CR characters, a couple of ways of doing that are given in this excellent answer.

您需要修改文件以删除这些CR字符,在这个优秀的回答中给出了几种方法。