代码之家  ›  专栏  ›  技术社区  ›  Blue

shell脚本中意外的标记“done”

  •  0
  • Blue  · 技术社区  · 6 年前

    我对编写shell脚本非常陌生,但我正在尝试编写一个脚本,在执行命令之前检查文件是否当前处于打开状态。我得到以下错误

     ./Script.sh: line 9: syntax error near unexpected token `done'
    ./Script.sh: line 9: `done'
    

    作为奖励我不知道 "|" operator我找到了一些shell语法站点,但是由于它的目的是搜索单个字符,所以很难。

    #!/bin/bash
    
    inotifywait -m -r -e create "sunshine" | while read NEWFILE
    do
            if [ lsof | grep NEWFILE ]; then
                    echo "found something";
            else
                    aws s3 sync sunshine s3://turnaround-sunshine/
    done
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   John Kugelman Michael Hodel    6 年前

    你的 if / else 丢失了 fi 最后。

    | 创建一个 两个命令之间,第一个fed的输出作为第二个fed的输入。第二根管子 如果 顺便说一下,声明不应该有方括号;并且 grep NEWFILE 应该是 grep "$NEWFILE" :

    if lsof | grep "$NEWFILE"; then
    

    使用 ShellCheck 捕捉这些类型的错误。