简体   繁体   English

如何修复bash中意外的文件结尾错误?

[英]How to fix an unexpected end of file error in bash?

The code saying that there is "unexpected end of file" error in line 16. Could someone please tell me my mistake? 代码在第16行中指出“文件末尾”错误。有人可以告诉我我的错误吗?

#!/bin/bash

total=0
for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -n | tail`;
do if [[ $i =~ ^[0-9]+$ ]]; then
    total=$(( $total + $i )); fi

for i in `grep 01/Oct/2006 log.txt | cut -d' ' -f1 | sort | uniq -c | sort -rn | head -10 | tr -s ' ' | cut -d' ' -f2,3 | sed -E 's/(\S*) (\S*)/\2 - \1/' | nl -s'. '`;

do 
    if ! [[ $i =~ ^[0-9]+$ ]];
        then
            printf $i;
            printf " ";
        else
            printf " $i - $(echo "scale=0; $i * 100 / $total" | bc )%% \n" ;
    fi
done

Your first for loop lacks a done . 您的第一个for循环缺少done

Here's a working version with improved formatting (but with all original flaws and bugs left inside, I just fixed the one issue asked for here): 这是一个具有改进格式的工作版本(但其中保留了所有原始的缺陷和错误,我只修复了此处要求的一个问题):

#!/bin/bash

$total;
for i in $(
  grep 01/Oct/2006 log.txt | 
    cut -d' ' -f1 |
      sort |
        uniq -c |
          tail);
do
  if [[ $i =~ ^[0-9]+$ ]]
  then
    $total += $i
  fi
done

for i in $(
  grep 01/Oct/2006 log.txt |
    cut -d' ' -f1 |
      sort |
        uniq -c |
          sort -rn |
            head -10 |
              tr -s ' ' |
                cut -d' ' -f2,3 |
                  sed -E 's/(\S*) (\S*)/\2 - \1/' |
                    nl -s'. ')
do 
  if ! [[ $i =~ ^[0-9]+$ ]];
  then
    printf " $i - ";
  else
    printf " $i - 0$(echo "scale=0; $i / $total" | bc)%% " ;
  fi
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM