Bash的清单,范围的文件 [英] Bash list files with range

查看:126
本文介绍了Bash的清单,范围的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本具有列出一个文件名内的日期的一个给定范围之间的所有文件(例如:20130133.212001.log)。开始日期和结束日期将由用户给出。我现在有START_YEAR,END_YEAR,START_MONTH,END_MONTH ....等等,直到end_second。我的计划是用ls语法:

  LS * {start_year..end_year} {start_month..end_mo​​nth} {} ......。日志

但问题是,如果一个月或日期有01 bash的是把它作为1.事实上我的bash版本比4,我可以不考虑更新bash的版本旧。
我用在这样的范围内列出文件任何其它方法。

  $>猫tmp.sh
#! /斌/庆典START_YEAR = 2013
END_YEAR = 2013
START_MONTH = 01
END_MONTH = 01
START_DATE = 01
END_DATE = 12START_MONTH = $(的printf%02D $ START_MONTH)
END_MONTH = $(的printf%02D $ END_MONTH)START_DATE = $(的printf%02D $起始日期)
END_DATE = $(的printf%02D $ END_DATE)
LS {$ START_YEAR .. $ END_YEAR} {$ START_MONTH .. $ END_MONTH} {$起始日期.. $ END_DATE} .LOG
$>

但输出是:

  $> ./ tmp.sh
LS:{2013..2013} {01..01} {} 01..12 .LOG:没有这样的文件或目录

而当前目录中包含:

  $> LS
20130101.log 20130103.log 20130105.log 20130107.log 20130109.log 20130111.log 20130201.log abcd.log
20130102.log 20130104.log 20130106.log 20130108.log 20130110.log 20130112.log 2013.log tmp.sh
$>


解决方案

这个怎么样:

 开始= $ START_YEAR $(的printf%02D $ START_MONTH)
结束= $ $ END_YEAR(printf的%02D $ END_MONTH)
LS * .LOG | SED -ne/ $启动/,/ $结束/ P

My script has to list all files between a given range of date within a file name (ex: 20130133.212001.log). The start date and end date will be given by user. I now have start_year, end_year, start_month, end_month....so on till end_second. My plan was to use ls syntax:

ls *{start_year..end_year}{start_month..end_month}{.....}.log

But the problem is if month or date has 01 bash is taking it as 1. Indeed my bash version is older than 4. I can't considering updating the bash version. Any other method that I use to list files in such range.

[EDIT]

$> cat tmp.sh
#! /bin/bash

start_year=2013
end_year=2013
start_month=01
end_month=01
start_date=01
end_date=12

start_month=$(printf %02d $start_month)
end_month=$(printf %02d $end_month)

start_date=$(printf %02d $start_date)
end_date=$(printf %02d $end_date)
ls {$start_year..$end_year}{$start_month..$end_month}{$start_date..$end_date}.log
$> 

but output is:

$>./tmp.sh
ls: {2013..2013}{01..01}{01..12}.log: No such file or directory

while the current dir contains:

$> ls
20130101.log  20130103.log  20130105.log  20130107.log  20130109.log  20130111.log  20130201.log  abcd.log
20130102.log  20130104.log  20130106.log  20130108.log  20130110.log  20130112.log  2013.log      tmp.sh
$> 

解决方案

How about like this:

start=$start_year$(printf %02d $start_month)
end=$end_year$(printf %02d $end_month)
ls *.log | sed -ne "/$start/,/$end/p"

这篇关于Bash的清单,范围的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆