簡體   English   中英

如何修復bash中意外的文件結尾錯誤?

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

代碼在第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

您的第一個for循環缺少done

這是一個具有改進格式的工作版本(但其中保留了所有原始的缺陷和錯誤,我只修復了此處要求的一個問題):

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