【问题标题】:Bash for loop syntax error removing semicolon didnt do the trickBash for 循环语法错误删除分号并没有解决问题
【发布时间】:2023-03-17 16:39:02
【问题描述】:

我正在为编辑器做包装器,我部分地遇到了这个错误,我正在检查文件是否已经在某个目录中被编辑过。目录以以下格式存储在文件中:path//date//openCount。双斜杠是分隔符,因为斜杠是类 unix 系统名称中唯一被禁止的字符之一。

/home/test//20150320//1
/home/d3ad/Documents/skola/2015_zima/ios/proj1/ads//20150320//1
/home/d3ad/Documents/skola/2015_zima/ios/proj1/backup.sh//20150320//1
/home/d3ad/Documents/skola/2015_zima/ios/proj1/asd//20150320//7
/home/d3ad/Documents/skola/2015_zima/ios/proj1/bs//20150320//9

代码:

FILE="false"
DIR=$(readlink -f "${2}")
#check if a file was already edited in this directory
for LINE in $(grep -n -o "${DIR}/[^/]*" ${WEDI_RC}); do
    if[ -f "${LINE}" ]; then
        FILE="true"
    fi
done

错误:

./wedi: Syntax error: "then" unexpected (expecting "done")

我尝试删除分号,将“do”和“then”移至下一行,但均未成功。你有什么想法?我很绝望,因为我上面有 1 个 for 循环并且它工作正常。

感谢您的回答

【问题讨论】:

    标签: bash for-loop syntax token


    【解决方案1】:

    if[ 之间缺少空格,因此出现语法错误。

    if [ -f "${LINE}" ]; then # add missing space
        FILE="true"
    fi
    

    【讨论】:

    • 不客气。 :) 你应该接受这个作为答案
    • 供将来参考,有关 Linux 的其他问题:空格很重要。被其他操作系统宠坏而忘记这条非常苛刻的规则变得很常见。
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2015-04-17
    • 2017-04-17
    • 2018-02-23
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多