简体   繁体   中英

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?

#!/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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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