Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2501218
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: LINUX

2014-03-10 10:27:26


点击(此处)折叠或打开

  1. #!/bin/sh

  2. #程序描述:通用shell脚本,抽离出来通用的shell函数,供其他脚本调用
  3. #作者:    程晓鹏
  4. #日期:    2014.1.25

  5. #获取操作系统的当前日期,时间
  6. #返回值 yyyy-MM-dd hh24:mi:ss
  7. function GetCurrentTime
  8. {
  9.   echo `date +"%Y-%m-%d %H:%M:%S"`;
  10. }

  11. #获取文件的记录数(参数为文件的全路径)
  12. function GetFileLineCount
  13. {
  14.   result=0;
  15.   if [ -f "$1" ]; then
  16.     result=`wc -l "$1" | awk '{print $1}'`;
  17.   fi
  18.   echo $result;
  19. }

  20. #清空文件内容
  21. function ClearFileContent
  22. {
  23.   if [ -f "$1" ]; then
  24.     cat /dev/null > "$1";
  25.   fi
  26. }

  27. #合并文件
  28. #参数1:合并的源文件
  29. #参数2:合并的目标文件
  30. function MergeFile
  31. {
  32.    filelen=$(GetFileLineCount $1);
  33.    if [ $filelen -gt 0 ]; then
  34.      cat $1 >> $2; #写入文件
  35.    fi
  36. }

  37. #获取查询语句中的IN字符串(文件中指定字段为要变化为in的关键数据)
  38. #参数1:要进行数据格式化的文件路径
  39. #参数2: 要取数据的数据列数
  40. function GetInData
  41. {
  42.   result=`cat $1 | awk 'BEGIN{RS="\r\n"; ORS=", "}{if(length($COLUMN) > 0){print "'\''"$COLUMN"'\''"}}' COLUMN=$2 | awk '{print substr($0,1, length($0)-2)}'`;
  43.   echo $(GetInDataResult "$result");
  44. }

  45. #获取查询语句中的IN字符串,多个值用逗号',' 进行分割 (程晓鹏 2014.02.21 add)
  46. #参数1:字符串
  47. #说明:如参数1为:'16, 17, 38, 39',则返回值为 ('16', '17', '38', '39')
  48. function GetInData2
  49. {
  50.   result=`echo "$1" | awk 'BEGIN{RS="\n"; ORS=", "} {len=split($0,a,", *"); for(i=1; i<=len; i++){if(length(a[i])>0){print "'\''"a[i]"'\''"}}}' | awk '{print substr($0,1, length($0)-2)}'`;
  51.   echo $(GetInDataResult "$result");
  52. }

  53. #获取GetInData函数的结果值
  54. #参数1:结果字符串
  55. function GetInDataResult
  56. {
  57.   result="$1";
  58.   if [ `echo "$result" | wc -c ` -lt 2 ]; then
  59.     result="''";
  60.   fi
  61.   echo "("$result")";
  62. }

  63. #在oracle数据库上执行,含有SQL语句的数据文件
  64. #参数说明
  65. #参数1 用户名
  66. #参数2 密码
  67. #参数3 TNS名称
  68. #参数4 提交的文件全路径
  69. function ExecuteSqlFile
  70. {
  71.   linecount=$(GetFileLineCount $4); #获取文件记录数
  72.   if [ $linecount -gt 0 ]; then
  73.     . ~/.profile;
  74.     cmd=`sqlplus $1/$2@$3 <
  75.       @$4;
  76.     commit;
  77.     exit;
  78.     EOF`;
  79.   fi
  80. }

  81. #判断是否是闰年
  82. #参数1:年份(四位数字)
  83. #返回值 0:不是闰年;1:是闰年
  84. function IsLeapYear
  85. {
  86.   result=`echo "$1" | awk '{print ($1%4==0 && $1%100!=0 || $1%400==0)}'`;
  87.   echo $result;
  88. }

  89. #获取含有前缀0的字符串[当数字在0-9之间时,追加前缀0]
  90. #参数1:数字
  91. #返回值:当数据在0-9时,追加前缀0
  92. function Get0Str
  93. {
  94.   result=$(Get0StrFormat "$1" 2);
  95.   echo "$result";
  96. }

  97. #获取前置0的字符串
  98. #参数1:原始字符串
  99. #参数2:总长度(int类型)
  100. #返回值:当原始字符串长度小于总长度,前面补充0,使其达到总长度
  101. function Get0StrFormat
  102. {
  103.   result="";
  104.   strlen=`echo "$1" | awk '{print length($0)}'`;
  105.   if [ $2 -gt 1 ]; then
  106.     if [ $strlen -lt $2 ]; then
  107.       i=1;
  108.       tmp=`expr "$2" - $strlen | bc`;
  109.       while [ $i -le $tmp ]
  110.       do
  111.         result="0$result";
  112.     i=`expr $i + 1 | bc`;
  113.       done
  114.     fi
  115.     result="$result$1";
  116.   else
  117.     result="$1";
  118.   fi
  119.   echo "$result";
  120. }

  121. #根据年份,月份,获取该年月的总天数
  122. #参数1:年份
  123. #参数2:月份
  124. #返回值,天数
  125. function GetDayCount
  126. {
  127.   result=0;
  128.   isLeap=$(IsLeapYear "$1");
  129.   month=$(GetIntValue "$2"); #对月份值,变成Int
  130.   case "$month" in
  131.     4 | 6 | 9 | 11)
  132.       result=30;
  133.       ;;
  134.     2)
  135.       if [ $isLeap -eq 0 ]; then
  136.         result=28;
  137.       else
  138.         result=29;
  139.       fi
  140.       ;;
  141.     *)
  142.      result=31;
  143.      ;;
  144.   esac

  145.   echo "$result";
  146. }

  147. #获取Int类型值
  148. #参数1:要进行格式化的字符串
  149. function GetIntValue
  150. {
  151.   result=`echo "$1" | awk '{print int($0)}'`;
  152.   echo $result;
  153. }

  154. #获取指定时间的前某个时间
  155. #参数1:指定时间(格式:yyyy-MM-dd hh24:mi:ss 如:2014-03-07 09:09:09)
  156. #参数2:小时数(int类型)
  157. function GetPreviousDateTimeByHours
  158. {
  159.    strdate=`echo "$1" | sed 's/[-: ]//g'`;
  160.    intX=`echo "$2" | awk '{print int($1)}'`;

  161.    #获取输入参数的年,月,日,时,分,秒数据
  162.    intYear0=`echo "$strdate" | awk '{print substr($1, 1, 4)}' | awk '{print int($1)}'`;
  163.    intMonth0=`echo "$strdate" | awk '{print substr($1, 5, 2)}' | awk '{print int($1)}'`;
  164.    intDay0=`echo "$strdate" | awk '{print substr($1, 7, 2)}' | awk '{print int($1)}'`;
  165.    intHour0=`echo "$strdate" | awk '{print substr($1, 9, 2)}' | awk '{print int($1)}'`;
  166.    intMinute0=`echo "$strdate" | awk '{print substr($1, 11, 2)}'`;
  167.    intSecond0=`echo "$strdate" | awk '{print substr($1, 13, 2)}'`;
  168.    
  169.    intYear1=$intYear0;
  170.    intMonth1=$intMonth0;
  171.    intDay1=$intDay0;
  172.    intHour1=$intHour0;

  173.    #进行相关的计算
  174.    while [ $intHour1 -le $intX ]
  175.    do
  176.      if [ $intDay1 -le 1 ]; then
  177.        intMonth1=`expr "$intMonth0" - 1 | bc`; #取上个月
  178.        if [ $intMonth1 -le 0 ]; then
  179.          intDay1=31;
  180.      intMonth1=12;
  181.      intYear1=`expr "$intYear1" - 1 | bc`;
  182.        else
  183.          intDay1=$(GetDayCount "$intYear1" "$intMonth1");
  184.        fi
  185.      else
  186.        intDay1=`expr "$intDay1" - 1 | bc`;
  187.      fi
  188.      #进行累加小时数
  189.      intHour1=`expr "$intHour1" + 24 | bc`;
  190.    done
  191.    
  192.    #确定最终的小时数
  193.    intHour1=`expr "$intHour1" - "$intX" | bc`;
  194.    
  195.    #进行字符格式化
  196.    intMonth2=$(Get0Str "$intMonth1");
  197.    intDay2=$(Get0Str "$intDay1");
  198.    intHour2=$(Get0Str "$intHour1");

  199.    result="$intYear1-$intMonth2-$intDay2 $intHour2:$intMinute0:$intSecond0";
  200.    echo "$result";
  201. }

  202. #获取指定时间的前某个时间
  203. #参数1:指定时间(格式:yyyy-MM-dd hh24:mi:ss 如:2014-03-07 09:09:09, 其中时分秒数据可选)
  204. #参数2:天数(int类型)
  205. function GetPreviousDateByDays
  206. {
  207.    strdate=`echo "$1" | sed 's/[-: ]//g'`;
  208.    strlen=$(GetStrLength "$1");
  209.    intX=`echo "$2" | awk '{print int($1)}'`;

  210.    #获取输入参数的年,月,日数据
  211.    intYear0=`echo "$strdate" | awk '{print substr($1, 1, 4)}' | awk '{print int($1)}'`;
  212.    intMonth0=`echo "$strdate" | awk '{print substr($1, 5, 2)}' | awk '{print int($1)}'`;
  213.    intDay0=`echo "$strdate" | awk '{print substr($1, 7, 2)}' | awk '{print int($1)}'`;
  214.    
  215.    intYear1=$intYear0;
  216.    intMonth1=$intMonth0;
  217.    intDay1=$intDay0;

  218.    #进行相关的计算
  219.    while [ $intDay1 -le $intX ]
  220.    do
  221.      intMonth1=`expr "$intMonth1" - 1 | bc`;
  222.      if [ $intMonth1 -le 0 ]; then
  223.        tmpCount=31;
  224.        intMonth1=12;
  225.        intYear1=`expr "$intYear1" - 1 | bc`;
  226.      else
  227.        tmpCount=$(GetDayCount "$intYear1" "$intMonth1");
  228.      fi
  229.      intDay1=`expr "$intDay1" + "$tmpCount" | bc`; #进行天数的累加
  230.    done
  231.    #计算日期中的天
  232.    intDay1=`expr "$intDay1" - "$intX" | bc`;

  233.    #进行字符格式化
  234.    intMonth2=$(Get0Str "$intMonth1");
  235.    intDay2=$(Get0Str "$intDay1");
  236.    result="$intYear1-$intMonth2-$intDay2";
  237.    if [ $strlen -gt 10 ]; then #当含有时,分,秒数据时,追加上去
  238.      strSubLen=`expr "$strlen" - 10 | bc`;
  239.      strHMS=`expr substr "$1" 11 "$strSubLen"`;
  240.      result="$result$strHMS";
  241.    fi
  242.    echo "$result";
  243. }

  244. #获取字符串长度
  245. function GetStrLength
  246. {
  247.   result=`echo "$1" | awk '{print length($0)}'`;
  248.   echo "$result";
  249. }

  250. #获取指定时间的前某个时间
  251. #参数1:指定时间(格式:yyyy-MM-dd hh24:mi:ss 如:2014-03-07 09:09:09)
  252. #参数2:计算的单位(DD 天数;HH 小时数)
  253. #参数3:偏移量值
  254. function GetPreviousDateTime
  255. {
  256.   result="";
  257.   if [ "$2" = "HH" ]; then
  258.     result=$(GetPreviousDateTimeByHours "$1" "$3");
  259.   elif [ "$2" = "DD" ]; then
  260.     result=$(GetPreviousDateByDays "$1" "$3");
  261.   fi
  262.   echo "$result;
  263. }
Common.rar
阅读(12180) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

少林功夫好2016-07-15 17:32:58

感谢分享,回头提取。