文件名相差1个数字的文件的输出行数

时间:2019-04-26 03:52:50

标签: linux bash date

我们需要报告写入7天周期的日志文件的行数。日志文件的名称为-今天的[filename].log.1.gz,昨天的[filename].log.2.gz,直到第七天的[filename].log.7.gz

我希望创建一个可立即输出数字的脚本,而不是针对每一行运行zcat [filename].log.1.gz | wc -l命令。我还希望针对每个输出值发出有意义的消息

我可以编写一个bash脚本,因为文件名相同,所以每行都会执行一次,但是我希望有一些更优雅的方法

代替这个

zcat position.log.3.gz | wc -l
zcat position.log.4.gz | wc -l
zcat position.log.5.gz | wc -l
zcat position.log.6.gz | wc -l
zcat position.log.7.gz | wc -l

我希望有更多类似的东西

for i in {1..7}
c=$(zcat position.log.$i.gz | wc -l)
message=$"The count for "
date1=$(date --date='$i days ago')
result=$"$message$date1$c"
echo $result
done 

但是,我无法运行它。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您编写了一些小修正的脚本:

<?php
  $x = (rand(1,10));
  $y = (rand(1,10));
  $sum = $x + $y;
  if (isset($_POST['submit'])) {
    $guess = $_POST['guess'];
    $value = $_POST['sum'];
    if ($value == $guess) {
      echo "nice, you got it right.";
    } else {
      echo "aw, too bad. <br>";
    }
  }
  echo "<br>What is " . $x . " + " . $y . "? <br>";
?>  
<form action="" method="post">
<input name="guess" type="text" />
<input type="hidden" name="sum" value="<?=$sum?>" />
<input name="submit" type="submit" />
</form>

或者更短一点:

# for loop starts with a 'do'
for i in {1..7}; do
   # indentation makes everything redable
   # always quote your variables
   c=$(zcat "position.log.$i.gz" | wc -l)
   # The "" is just a string, no '$' sign before it
   message="The count for "
   # The '$i' is literally `$i`, if you want to _expand_ a variable, you have to use "$i"
   date1=$(date --date="$i days ago")
   # same as above, drop the $
   result="$message$date1$c"
   # always quote your variables
   echo "$result"
done