`
DangDang0701
  • 浏览: 74818 次
  • 性别: Icon_minigender_2
  • 来自: 大连
社区版块
存档分类
最新评论

Shell练习(2)_解压缩文件

阅读更多

Shell练习(2)

#!/bin/sh

NOW=`DATE +"%Y%m%d %A %T"`
echo "${NOW} /STATE TEST UNZIP SOURCEFILE">>D:/IDBCDB/test/unzip.log

# 目的文件夹移动
cd D:/IDBCDB/test

# 既存err_file删除
if [ -w err_file ];then
	rm err_file
fi

# 解压缩处理
ls -1 *.zip | while read LINE; do
        PERC=`echo $LINE | cut -d"." -f1 |awk '{ print $1 }'`

        # 源文件名文件名列表添加
        echo $PERC >> d1_d_source_files.lst

        # 解压缩处理
	(unzip -p $PERC.zip |cat >$PERC) 2>> err_file

	# 备份压缩文件
        mv $PERC.zip GzFileBackUp
done

# err_file的大小检查

if [ -s err_file ];then
	exit 1
fi

# 删除err_file
   rm err_file

NOW=`DATE +"%Y%m%d %A %T"`
echo "${NOW} /END TEST UNZIP SOURCEFILE">>D:/IDBCDB/test/unzip.log

 解析:

1.Date

命令$ date --help

输出有关Date的各种命令。如下所示:

$ date –help
Usage: date [OPTION]… [+FORMAT]
or: date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
-d, –date=STRING display time described by STRING, not `now’
# such as ‘n days ago |1 month ago|n years ago’
-f, –file=DATEFILE like –date once for each line of DATEFILE
-ITIMESPEC, –iso-8601[=TIMESPEC] output date/time in ISO 8601 format.
TIMESPEC=`date’ for date only,
`hours’, `minutes’, or `seconds’ for date and
time to the indicated precision.
–iso-8601 without TIMESPEC defaults to `date’.
-r, –reference=FILE display the last modification time of FILE
-R, –rfc-2822 output RFC-2822 compliant date string
-s, –set=STRING set time described by STRING
-u, –utc, –universal print or set Coordinated Universal Time
–help display this help and exit
–version output version information and exit
FORMAT controls the output. The only valid option for the second form
specifies Coordinated Universal Time. Interpreted sequences are:
%% 输出%符号 a literal %
%a 当前域的星期缩写 locale’s abbreviated weekday name (Sun..Sat)
%A 当前域的星期全写 locale’s full weekday name, variable length (Sunday..Saturday)
%b 当前域的月份缩写 locale’s abbreviated month name (Jan..Dec)
%B 当前域的月份全称 locale’s full month name, variable length (January..December)
%c 当前域的默认时间格式 locale’s date and time (Sat Nov 04 12:02:33 EST 1989)
%C n百年 century (year divided by 100 and truncated to an integer) [00-99]
%d 两位的天 day of month (01..31)
%D 短时间格式 date (mm/dd/yy)
%e 短格式天 day of month, blank padded ( 1..31)
%F 文件时间格式 same as %Y-%m-%d
%g the 2-digit year corresponding to the %V week number
%G the 4-digit year corresponding to the %V week number
%h same as %b
%H 24小时制的小时 hour (00..23)
%I 12小时制的小时 hour (01..12)
%j 一年中的第几天 day of year (001..366)
%k 短格式24小时制的小时 hour ( 0..23)
%l 短格式12小时制的小时 hour ( 1..12)
%m 双位月份 month (01..12)
%M 双位分钟 minute (00..59)
%n 换行 a newline
%N 十亿分之一秒 nanoseconds (000000000..999999999)
%p 大写的当前域的上下午指示 locale’s upper case AM or PM indicator (blank in many locales)
%P 小写的当前域的上下午指示 locale’s lower case am or pm indicator (blank in many locales)
%r 12小时制的时间表示(时:分:秒,双位) time, 12-hour (hh:mm:ss [AP]M)
%R 24小时制的时间表示 (时:分,双位)time, 24-hour (hh:mm)
%s 自基础时间 1970-01-01 00:00:00 到当前时刻的秒数 seconds since `00:00:00 1970-01-01 UTC’ (a GNU extension)
%S 双位秒 second (00..60); the 60 is necessary to accommodate a leap second
%t 横向制表位(tab) a horizontal tab
%T 24小时制时间表示 time, 24-hour (hh:mm:ss)
%u 数字表示的星期(从星期一开始 1-7)day of week (1..7); 1 represents Monday
%U 一年中的第几周星期天为开始 week number of year with Sunday as first day of week (00..53)
%V 一年中的第几周星期一为开始 week number of year with Monday as first day of week (01..53)
%w 一周中的第几天 星期天为开始 0-6 day of week (0..6); 0 represents Sunday
%W 一年中的第几周星期一为开始 week number of year with Monday as first day of week (00..53)
%x 本地日期格式 locale’s date representation (mm/dd/yy)
%X 本地时间格式 locale’s time representation (%H:%M:%S)
%y 两位的年 last two digits of year (00..99)
%Y 年 year (1970…)
%z RFC-2822 标准时间格式表示的域 RFC-2822 style numeric timezone (-0500) (a nonstandard extension)
%Z 时间域 time zone (e.g., EDT), or nothing if no time zone is determinable
By default, date pads numeric fields with zeroes. GNU date recognizes
the following modifiers between `%’ and a numeric directive.
`-’ (hyphen) do not pad the field
`_’ (underscore) pad the field with spaces

 

2.echo  >>

输出到文件

>>:表示不会清空以前的内容,只是追加输出。

>:会清空以前的内容。

 

3.ls命令

   ls -1 *.zip

   列出目录下后缀是.zip的文件,且每行之输出一个文件名。

    -a 列出目录下的所有文件,包括以 . 开头的隐含文件。

  -b 把文件名中不可输出的字符用反斜杠加字符编号(就象在C语言里一样)的形式列出。

  -c 输出文件的 i 节点的修改时间,并以此排序。

  -d 将目录象文件一样显示,而不是显示其下的文件。

  -e 输出时间的全部信息,而不是输出简略信息。

  -f -U 对输出的文件不排序。

  -g 无用。

  -i 输出文件的 i 节点的索引信息。

  -k 以 k 字节的形式表示文件的大小。

  -l 列出文件的详细信息。

  -m 横向输出文件名,并以“,”作分格符。

  -n 用数字的 UID,GID 代替名称。

  -o 显示文件的除组信息外的详细信息。

  -p -F 在每个文件名后附上一个字符以说明该文件的类型,“*”表示可执行的普通

  文件;“/”表示目录;“@”表示符号链接;“|”表示FIFOs;“=”表示套

  接字(sockets)。

  -q 用?代替不可输出的字符。

  -r 对目录反向排序。

  -s 在每个文件名后输出该文件的大小。

  -t 以时间排序。

  -u 以文件上次被访问的时间排序。

  -x 按列输出,横向排序。

  -A 显示除 “.”和“..”外的所有文件。

  -B 不输出以 “~”结尾的备份文件。

  -C 按列输出,纵向排序。

  -G 输出文件的组的信息。

  -L 列出链接文件名而不是链接到的文件。

  -N 不限制文件长度。

  -Q 把输出的文件名用双引号括起来。

  -R 列出所有子目录下的文件。

  -S 以文件大小排序。

  -X 以文件的扩展名(最后一个 . 后的字符)排序。

  -1 一行只输出一个文件。

  --color=no 不显示彩色文件名

  --help 在标准输出上显示帮助信息。

  --version 在标准输出上输出版本信息并退出。

  

4.While循环

while read LINE; do
        PERC=`echo $LINE | cut -d"." -f1 |awk '{ print $1 }'`

        # 源文件名文件名列表添加
        echo $PERC >> d1_d_source_files.lst

          # 解压缩处理
          (unzip -p $PERC.zip |cat >$PERC) 2>> err_file

          # 备份压缩文件
        mv $PERC.zip GzFileBackUp
done

 

 循环3中得到的文件列表,把每行以“.”为分隔符分开,取第一个字符串赋给PERC。其中awk '{ print $1 }'我觉得去掉也可以。在这里感觉没有起什么作用。但是这段是项目上用的很成熟的一段,不敢贸然去掉。

 

    把PREC这个变量的值输出到文件d1_d_source_files.lst中。

  

    用unzip命令解压文件,解压后输出到另一个同名文件。如果有错则把错误信息输出到err_file

 

    把解压的源文件移动到另一个文件夹做备份。

  

   awk用法:

可以把awk看作一个小的c语言的实现,awk的灵活多变黎补了shell的不足。

1、awk程序体的一般格式为:
   awk '
   BEGIN{}         #程序开始前的预处理部分可以对一些变量进行初始化
   {}              #对每条记录进行操作的主体部分
   END{}' <文件名> #程序的结束部分,可以对处理的结果作一些善后或总结

2、awk的程序风格完全可以按照c语言的风格书写。例如:
awk '{
if ($1 == "FIND")
regex = $2;
else 
{
where = match($0, regex);
if (where)
   printf("Match of %s found at %d in %s",regex,where,$0);
}
}' <文件名>
当然,awk支持一些更简洁的书写风格,例如上面的printf语句可以写成:
print "Match of", regex, "found at", where, "in", $0

3、记录和字段的表示法。
   整条记录表示为$0,第n个字段表示为$n

操作符描述操作符描述
< 小于
> = 大于等于
< = 小于等于
~ 匹配正则表达式
= = 等于
!~ 不匹配正则表达式
!= 不等于

awk '{if($4~/Gui/) print $0}' awktest.txt
awk '$4~/Gui/' awktest.txt
打印出awktest.txt中第四个字段是Gui的纪录

awk '{if ($4=="GuiGui" || $4~/Gui/) print $0}' grade.txt
还可以用AND OR等逻辑运算符。

4、常用内在变量。
   NR 是记录号,即当前正在处理第几条记录。
   NF 是当前记录共有多少个字段,
这样,每条记录最后一个字段可表示为$NF,倒数第2个字段可表示为$(NF-1)
   FS 是一个说明各个字段如何分隔开的预定义变量.
这经常用于BEGIN语句体中预定义,类似于cut中的-d功能.
   RS 是一个说明各个记录如何分隔开的预定义变量.
这经常用于BEGIN语句体中预定义,例如,RS=":" 表示各条记录是用":"分开的.
   OFS 定义各字段在输出时如何分隔开.
   ORS 定义各条记录在输出时如何分隔开.

5、输出重定向。
   例如:
awk '{ print $2 > "phone-list"
print $1 > "name-list" }' BBS-list 
双引号内为文件名。

6、在awk中利用管道向shell命令传递参数。
   例如:
awk '{ print $1 > "names.unsorted"
print $1 | "sort -r > names.sorted" }' BBS-list

7、awk的特有函数和其它功能请参阅awk手册。

另外,nawk比awk具有更多的功能,它能够把shell中的变量传递给nawk。
nawk格式:
Usage: nawk [-f programfile | 'program'] [-Ffieldstep] [-v var=value] [files]
例如,可以这样向nawk传递shell变量:
nawk -v dr=$HOME -v pk=`hostname` 'BEGIN{print dr pk}'

 

cat使用方法:

使用方式:cat [-AbeEnstTuv] [--help] [--version] fileName  
说明:把档案串连接后传到基本输出(>fileName 到另一个档案)

参数: 
-n 或 --number 由 1 开始对所有输出的行数编号 
-b 或 --number-nonblank 和 -n 相似,只不过对于空白行不编号 
-s 或 --squeeze-blank 当遇到有连续两行以上的空白行,就代换为一行的空白行 
-v 或 --show-nonprinting 

范例: 
cat -n textfile1 >; textfile2 把 textfile1 的档案内容加上行号后输入 textfile2 这个档案里 
cat -b textfile1 textfile2 >;>; textfile3 把 textfile1 和 textfile2 的档案内容加上行号(空白行不加)之后将内容附加到 textfile3 里。 

  

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics