Shell遍历文件内容

2014年6月11日星期三  --- 晴 

北京

新的环境总是慢慢从陌生变得熟悉,陌生的同事,陌生的工作环境,和陌生的领导。刚开始的工作也就是查查数据,测一测功能的实现,另外再熟悉一下新的业务。

测试功能的时候需要将数据库表名和文件名拼接成命令语句,执行测试。由于涉及的表和文件名比较多,所以第一次测的时候,很是花了一些时间,同时,把头给弄晕了。

所以,不得不使用不太熟悉的shell脚本来帮一帮忙。

Shell遍历文件内容_第1张图片 

1.      将需要拼接的表名和本地文件的路径写到配置文件

2.      通过参数的形式,将配置文件的路径传到脚本BatSendMsg.sh中,执行脚本

3.      脚本根据配置文件的数据库表名,从本地目录(`ls *table name*`)过滤文件名,将过滤得到的文件名拼接到命令语句中

4.      将拼接好的语句写入到sendMsg.out文件中去

#!/bin/bash
# batSendMsg.sh
# Generate the sentences used to sendmessages
# during testing  the function moving files
# Writen by: Jack Liu
# Date:         2014-06-11
 
# declare the path of config file
config_path="."                                   # path of config file
config_file="tables.config"                       # name of config file
sh_name=`basename $0`                             # name of shell
search_path="."                                   # where local files
 
# test if the params are valid
function check_path_file
{
  if[ $# -gt 2 ]                                  # invalid params
 then
   echo "Sorry, usage: $sh_name path file_name or $sh_name path or$sh_name"
   echo "Please try again!"
   exit 1
 elif [ $# -eq 2 ]                                # two params
 then
   config_path=$1
   config_file=$2
 elif [ $# -eq 1 ]                                # just one param
 then
   config_path=$1
  fi
 
  #debug
   echo
   echo "config_path: $config_path"
   echo "config_file: $config_file"
   echo
  #end debug
 
  #test if validable the params
  if[ ! -d "$config_path" ]                      # if path is invalid
 then
   echo "Sorry, invalid path: $config_path"
    exit 1
 elif [ ! -f "$config_path/$config_file" ]       # if the file_name isinvalid
 then
   echo "Sorry, no file: $config_path/$config_file"
   exit 1
  fi
 
  #debug
   echo
   echo "Full path: $config_path/$config_file"
   echo
  #end debug
}
 
######################
# main begin         #
######################
 
# first check if the path and file valid
check_path_file $@
 
# read the config file
# fetch the path of the local files and thetable names
IFS_OLD=$IFS                                # save the init seperators
IFS=$'\n'                                   # define the new seperator, one line one entry
count=1                                     # count the entry numbers                     
for entry in `cat $config_path/$config_file`
do
  if[ 1 -eq $count ]                        # first entry including path where localfiles
 then
   if [ ! -d $entry ]                       # check if path valid
   then
     echo "Sorry, invalid path: $entry"
     exit 1
   else                                     # valid path
     #debug
       echo "search_path: $entry"
     #end debug
     search_path=$entry                     # get the position where the localfiles
     cd $search_path                        # jump to the position
     
     # file to output
     if [ -f sendMsg.out ]
      then                                  # if the file exists
       rm sendMsg.out                       # remove the original file
       touch sendMsg.out                    # build a new one
     fi
   fi
 else
    #debug
     echo "mask name: $entry"
     echo `ls *$entry*`
    #end debug
    #mask file names based on the key words in the config files
   for file_name in `ls *$entry*`
   do
     echo "Hello $file_name, nice to meet you."
   done >> sendMsg.out                     # generate the sentences
  fi
 count=$[ $count + 1 ]                     # counter
done
 
IFS=IFS_OLD                                # recovery the IFS
exit 0
######################
# main end           #
######################


你可能感兴趣的:(Shell)