第 13 行:意外标记“(”附近出现语法错误

第 13 行:意外标记“(”附近出现语法错误

运行 shell 将多个 csv 文件串在一起,在年底前运行此过程并运行良好,今天在仅进行编辑以包含 2017 年并收到上述错误后运行。

#!/bin/bash

REPORTS="Product_Report"
YEARS="2014 2015 2016 2017”
MONTHS="01 02 03 04 05 06 07 08 09 10 11 12"

for r in ${REPORTS}; do
    echo "Rebuilding ${r}.csv"
    head -n 1 ${r}.header > ${r}.csv
    for y in ${YEARS}; do
        for m in ${MONTHS}; do
            f="Data/${r}_${y}${m}.csv"
            if [ ! -s "${f}" ]; then echo "Skipping ${f} (not present)"; continue; fi
            tail -n +2 ${f} >> ${r}.csv
            wc -l ${f}
        done
    done
    echo
    wc -l ${r}.csv
done

答案1

用于进行更改的文本编辑器使用 a而不是预期的"结束 YEARS 字符串。这会弄乱文件的完整字符串分隔,您可以在问题的语法突出显示中看到这一点。

下面是更正的版本,请注意不同的语法突出显示:

#!/bin/bash

REPORTS="Product_Report"
YEARS="2014 2015 2016 2017"
MONTHS="01 02 03 04 05 06 07 08 09 10 11 12"

for r in ${REPORTS}; do
    echo "Rebuilding ${r}.csv"
    head -n 1 ${r}.header > ${r}.csv
    for y in ${YEARS}; do
        for m in ${MONTHS}; do
            f="Data/${r}_${y}${m}.csv"
            if [ ! -s "${f}" ]; then echo "Skipping ${f} (not present)"; continue; fi
            tail -n +2 ${f} >> ${r}.csv
            wc -l ${f}
        done
    done
    echo
    wc -l ${r}.csv
done

相关内容