Linux bash脚本帮助中的新手,请

Linux bash脚本帮助中的新手,请,linux,bash,scripting,Linux,Bash,Scripting,我经常在我的服务器上运行bash脚本,我试图编写一个脚本来监视日志文件夹,并在文件夹超过定义的容量时压缩日志文件。我知道有更好的方法来做我目前正在尝试做的事情,你的建议非常受欢迎。下面的脚本抛出错误“意外的文件结尾”。下面是我的脚本 dir_base=$1 size_ok=5000000 cd $dir_base curr_size=du -s -D | awk '{print $1}' | sed 's/%//g' zipname=archivedate +%Y%m%d if

我经常在我的服务器上运行bash脚本,我试图编写一个脚本来监视日志文件夹,并在文件夹超过定义的容量时压缩日志文件。我知道有更好的方法来做我目前正在尝试做的事情,你的建议非常受欢迎。下面的脚本抛出错误“意外的文件结尾”。下面是我的脚本

dir_base=$1   
size_ok=5000000  
cd $dir_base  
curr_size=du -s -D | awk '{print $1}' | sed 's/%//g' zipname=archivedate +%Y%m%d

if (( $curr_size > $size_ok ))
then
    echo "Compressing and archiving files, Logs folder has grown above 5G"
    echo "oldest to newest selected."
    targfiles=( `ls -1rt` )
    echo "rocess files."
    for tfile in ${targfiles[@]}
    do
        let `du -s -D | awk '{print $1}' | sed 's/%//g' | tail -1`
        if [ $curr_size -lt $size_ok ];
        then
            echo "$size_ok has been reached. Stopping processes"
            break
        else  if [ $curr_size -gt $size_ok ];
        then
            zip -r $zipname $tfile
            rm -f $tfile
            echo "Added ' $tfile ' to archive'date +%Y%m%d`'.zip and removed"
        else [ $curr_size -le $size_ok ];
            echo "files in $dir_base are less than 5G, not archiving"
        fi

调查。这是一个使用它的方法。

根据您给我们的信息,您缺少结束for循环的“done”和结束主if的“fi”。请重新格式化您的代码,您将得到更精确的答案

编辑:

看看你重新格式化的脚本,它是这样说的:“意外的文件结尾”是因为你没有关闭“for”循环,也没有关闭“if”

由于您似乎模仿了logrotate行为,请按照@Hank的建议进行检查


my2c我的
du-s-D
不显示
%
符号。所以你可以这么做

curr_size=$(du -s -D)
set -- $curr_size
curr_size=$1
为您节省了一些开销,而不是
du-s-D | awk'{print$1}| sed的/%//g

如果它确实显示
%
符号,您可以像这样处理它
du-s-D | awk'{print$1+0}'
。无需使用
sed

尽可能使用
$()
语法而不是反勾号

对于
targfiles=(
ls-1rt
,可以省略
-1
。所以它是可以的
targfiles=($(ls-rt))


尽可能在变量周围使用引号。例如“$zipname”、“$tfile”

请再次正确格式化您的代码。