linux - shell脚本: syntax error near unexpected token `while'

标签 linux bash shell

我正在 Linux 中执行该脚本,并且对它非常陌生:-

 #!/bin/bash 
 for FILENAME   in /u01/oracle/domains/custom.log  

 while :

   do  
    FILESIZE=$(stat -c%s "$FILENAME")

        if [[ $FILESIZE > 1000000 ]] ;then
        echo "$FILENAME is too large = $FILESIZE bytes."
        > $FILENAME
        fi
     sleep 2m
    done    
  exit 0

我收到的错误为:

./ScriptCustom.sh: line 4: syntax error near unexpected token `while'
./ScriptCustom.sh: line 4:  `while :'

最佳答案

问题出在for语句上,它不完整。 for 语句的语法为:

for 名称 [ in 单词] ;做 列表 ;完成

forindodone 和分号 (;) 是关键字,namewordlist 是语言概念,方括号([])是表示法助手,告诉它们所包装的片段是可选的。括号本身不是语句的一部分,无论是否包含它们所包裹的片段,它们都不能出现。

回到你的代码,它应该是这样的:

for FILENAME in /u01/oracle/domains/custom.log; do
   while :
   do
       # ...
   done    # end while
done       # end for

但是,如果列表仅包含一项 (/u01/oracle/domains/custom.log),则您甚至不需要 for 语句。您可以简单地将值分配给变量,而不是使用 for block :

FILENAME=/u01/oracle/domains/custom.log  
while :
do
    # ...
done    # end while

关于linux - shell脚本: syntax error near unexpected token `while' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47787200/

相关文章:

java - Netbeans Maven 项目在 Windows 中运行,但在 Linux 中不运行

linux - 目录和所有子目录中的递归解压缩和删除

Bash:文件描述符

python - 计算 Django 项目中的代码行数

linux shell 脚本 : getting filename from a user input string

Linux 为 DECLARE_WORK 中的函数传递参数

c - 加载共享库 : libcudart. so.4 时出错:无法打开共享对象文件:没有这样的文件或目录

bash - 为什么 **find** 找不到任何东西?

c - 将数据写入 fork 管道不起作用

linux - 如何并行启动多个命令行