Chinaunix首页 | 论坛 | 博客
  • 博客访问: 770796
  • 博文数量: 274
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 862
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-24 15:31
个人简介

不合格的程序猿

文章分类

全部博文(274)

文章存档

2019年(3)

2018年(1)

2017年(4)

2016年(160)

2015年(106)

我的朋友

分类: LINUX

2015-12-14 15:54:27


点击(此处)折叠或打开

  1. #!/bin/bash

  2. #1.变量定义
  3. directory="/server/www"

  4. #2.遍历子目录
  5. for file in `ls $directory`
  6. do
  7.         if [ -d $directory/$file ]
  8.         then
  9.                 echo $file is dir
  10.         else
  11.                 echo $file is file
  12.         fi
  13. done

需求

遍历指定根目录下的所有文件,进行文件编码的转换

技巧

1.bash shell中for循环的使用
2.iconv字符编码转换工具的使用


点击(此处)折叠或打开

  1. #!/bin/bash

  2. #1.变量定义
  3. directory="/home/wzy/Downloads/execl"
  4. f_encoding="utf-8"
  5. t_encoding="gbk"

  6. #2.遍历子目录
  7. for dir in `ls $directory`
  8. do
  9.     if [ -d $directory/$dir ]
  10.     then
  11.         #3.遍历子目录的文件
  12.         for file in `ls $directory/$dir`
  13.         do
  14.             if [ -e $directory/$dir/$file ]
  15.             then
  16.                 #4.文件类型转换
  17.                 iconv -f $f_encoding -t $t_encoding $directory/$dir/$file -o $directory/$dir/iconv.$file
  18.                 #5.删除原始文件
  19.                 if [ $? -eq 0 ]
  20.                 then
  21.                     rm $directory/$dir/$file
  22.                 fi
  23.             fi
  24.         done
  25.     fi
  26. done


阅读(404) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~