linux - 意外标记附近的语法错误 "do"

标签 linux shell loops sh

此 shell 脚本抛出一条错误消息:

syntax error near unexpected token do

这是我的代码

#!/bin/bash
DIRS="/home/delhi_qa/mkv/18000/marketview/log/FIFO_OPTIMISER/LOGS 1"
DEST="home/delhi_qa/Tests/Ankit/TTLOGSBACKUP"
DELETE_OLD_ZIP_FILES="no" 
BASENAME=/usr/bin/basename 
ZIP=/usr/bin/zip
for i in $DIRS
do
    #On running this unexpected token message occurred at this line#
    zipfile="${DEST}/$(${BASENAME} ${d}).zip"
    echo -n "Creating $zipfile..."
    if [ "$DELETE_OLD_ZIP_FILES" == "yes" ]
    then
        [ -f $zipfile ] && /bin/rm -f $zipfile 
    fi
    ${ZIP} -r $zipfile $d &>/dev/null && echo "Done!"
done

你能告诉我为什么吗?

最佳答案

Your input file contains carriage returns. Use dos2unix or something similar to get rid of those. – devnull

devnull 是正确的。问题的错误修订 3 通过扭曲错误消息来隐藏这一点,错误消息之前显示为

syntax error near unexpected token `do
- note the lone grave accent (`), which bash uses at the beginning of token names in messages together with an apostrophe (') at the end to quote the token.

The exact error message got to be e. g. for a script called ./do

'/do: line 8: syntax error near unexpected token `do

附加撇号出现在行首,覆盖了命令 ./do 的第一个字符。这只能由 do 后的回车引起。

关于linux - 意外标记附近的语法错误 "do",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23625397/

相关文章:

c++ - 关于消息队列与共享内存在这种情况下的适用性或适用性

linux - bugzilla 所需的 apache 2.4 的 mod_perl.so

mysql - 如何将 bzip 的输出通过管道传输到 mysql,以将数据直接从 bzip 压缩文件恢复到数据库中

bash - 遍历在 awk 中共享一个字段的行

Python urllib.urlretrieve 和用户代理

linux - 从另一个 script2.sh 调用 bash 时找不到 ./script1.sh

更改不相关的代码会导致段错误。它为什么要这样做?

windows - 如何通过 Windows 命令行 shell 脚本保持文件打开(锁定)?

objective-c - 评估字符串并作为 Objective-C 代码执行

关于 vector 和 for/while 循环的 C++ 新手