linux 脚本 字符串函数调用函数调用,自己封装的SHELL脚本函数,方便调用,或许能给你帮助......

#!/bin/sh

#程序描述:通用shell脚本,抽离出来通用的shell函数,供其他脚本调用

#作者:    程晓鹏

#日期:    2014.1.25

#获取操作系统的当前日期,时间

#返回值 yyyy-MM-dd hh24:mi:ss

function GetCurrentTime

{

echo `date +"%Y-%m-%d %H:%M:%S"`;

}

#获取文件的记录数(参数为文件的全路径)

function GetFileLineCount

{

result=0;

if [ -f "$1" ]; then

result=`wc -l "$1" | awk '{print $1}'`;

fi

echo $result;

}

#清空文件内容

function ClearFileContent

{

if [ -f "$1" ]; then

cat /dev/null > "$1";

fi

}

#合并文件

#参数1:合并的源文件

#参数2:合并的目标文件

function MergeFile

{

filelen=$(GetFileLineCount $1);

if [ $filelen -gt 0 ]; then

cat $1 >> $2; #写入文件

fi

}

#获取查询语句中的IN字符串(文件中指定字段为要变化为in的关键数据)

#参数1:要进行数据格式化的文件路径

#参数2: 要取数据的数据列数

function GetInData

{

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)}'`;

echo $(GetInDataResult "$result");

}

#获取查询语句中的IN字符串,多个值用逗号',' 进行分割 (程晓鹏 2014.02.21 add)

#参数1:字符串

#说明:如参数1为:'16, 17, 38, 39',则返回值为 ('16', '17', '38', '39')

function GetInData2

{

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)}'`;

echo $(GetInDataResult "$result");

}

#获取GetInData函数的结果值

#参数1:结果字符串

function GetInDataResult

{

result="$1";

if [ `echo "$result" | wc -c ` -lt 2 ]; then

result="''";

fi

echo "("$result")";

}

#在oracle数据库上执行,含有SQL语句的数据文件

#参数说明

#参数1 用户名

#参数2 密码

#参数3 TNS名称

#参数4 提交的文件全路径

function ExecuteSqlFile

{

linecount=$(GetFileLineCount $4); #获取文件记录数

if [ $linecount -gt 0 ]; then

. ~/.profile;

cmd=`sqlplus $1/$2@$3 <

@$4;

commit;

exit;

EOF`;

fi

}

#判断是否是闰年

#参数1:年份(四位数字)

#返回值 0:不是闰年;1:是闰年

function IsLeapYear

{

result=`echo "$1" | awk '{print ($1%4==0 && $1%100!=0 || $1%400==0)}'`;

echo $result;

}

#获取含有前缀0的字符串[当数字在0-9之间时,追加前缀0]

#参数1:数字

#返回值:当数据在0-9时,追加前缀0

function Get0Str

{

result=$(Get0StrFormat "$1" 2);

echo "$result";

}

#获取前置0的字符串

#参数1:原始字符串

#参数2:总长度(int类型)

#返回值:当原始字符串长度小于总长度,前面补充0,使其达到总长度

function Get0StrFormat

{

result="";

strlen=`echo "$1" | awk '{print length($0)}'`;

if [ $2 -gt 1 ]; then

if [ $strlen -lt $2 ]; then

i=1;

tmp=`expr "$2" - $strlen | bc`;

while [ $i -le $tmp ]

do

result="0$result";

i=`expr $i + 1 | bc`;

done

fi

result="$result$1";

else

result="$1";

fi

echo "$result";

}

#根据年份,月份,获取该年月的总天数

#参数1:年份

#参数2:月份

#返回值,天数

function GetDayCount

{

result=0;

isLeap=$(IsLeapYear "$1");

month=$(GetIntValue "$2"); #对月份值,变成Int

case "$month" in

4 | 6 | 9 | 11)

result=30;

;;

2)

if [ $isLeap -eq 0 ]; then

result=28;

else

result=29;

fi

;;

*)

result=31;

;;

esac

echo "$result";

}

#获取Int类型值

#参数1:要进行格式化的字符串

function GetIntValue

{

result=`echo "$1" | awk '{print int($0)}'`;

echo $result;

}

#获取指定时间的前某个时间

#参数1:指定时间(格式:yyyy-MM-dd hh24:mi:ss 如:2014-03-07 09:09:09)

#参数2:小时数(int类型)

function GetPreviousDateTimeByHours

{

strdate=`echo "$1" | sed 's/[-: ]//g'`;

intX=`echo "$2" | awk '{print int($1)}'`;

#获取输入参数的年,月,日,时,分,秒数据

intYear0=`echo "$strdate" | awk '{print substr($1, 1, 4)}' | awk '{print int($1)}'`;

intMonth0=`echo "$strdate" | awk '{print substr($1, 5, 2)}' | awk '{print int($1)}'`;

intDay0=`echo "$strdate" | awk '{print substr($1, 7, 2)}' | awk '{print int($1)}'`;

intHour0=`echo "$strdate" | awk '{print substr($1, 9, 2)}' | awk '{print int($1)}'`;

intMinute0=`echo "$strdate" | awk '{print substr($1, 11, 2)}'`;

intSecond0=`echo "$strdate" | awk '{print substr($1, 13, 2)}'`;

intYear1=$intYear0;

intMonth1=$intMonth0;

intDay1=$intDay0;

intHour1=$intHour0;

#进行相关的计算

while [ $intHour1 -le $intX ]

do

if [ $intDay1 -le 1 ]; then

intMonth1=`expr "$intMonth0" - 1 | bc`; #取上个月

if [ $intMonth1 -le 0 ]; then

intDay1=31;

intMonth1=12;

intYear1=`expr "$intYear1" - 1 | bc`;

else

intDay1=$(GetDayCount "$intYear1" "$intMonth1");

fi

else

intDay1=`expr "$intDay1" - 1 | bc`;

fi

#进行累加小时数

intHour1=`expr "$intHour1" + 24 | bc`;

done

#确定最终的小时数

intHour1=`expr "$intHour1" - "$intX" | bc`;

#进行字符格式化

intMonth2=$(Get0Str "$intMonth1");

intDay2=$(Get0Str "$intDay1");

intHour2=$(Get0Str "$intHour1");

result="$intYear1-$intMonth2-$intDay2 $intHour2:$intMinute0:$intSecond0";

echo "$result";

}

#获取指定时间的前某个时间

#参数1:指定时间(格式:yyyy-MM-dd hh24:mi:ss 如:2014-03-07 09:09:09, 其中时分秒数据可选)

#参数2:天数(int类型)

function GetPreviousDateByDays

{

strdate=`echo "$1" | sed 's/[-: ]//g'`;

strlen=$(GetStrLength "$1");

intX=`echo "$2" | awk '{print int($1)}'`;

#获取输入参数的年,月,日数据

intYear0=`echo "$strdate" | awk '{print substr($1, 1, 4)}' | awk '{print int($1)}'`;

intMonth0=`echo "$strdate" | awk '{print substr($1, 5, 2)}' | awk '{print int($1)}'`;

intDay0=`echo "$strdate" | awk '{print substr($1, 7, 2)}' | awk '{print int($1)}'`;

intYear1=$intYear0;

intMonth1=$intMonth0;

intDay1=$intDay0;

#进行相关的计算

while [ $intDay1 -le $intX ]

do

intMonth1=`expr "$intMonth1" - 1 | bc`;

if [ $intMonth1 -le 0 ]; then

tmpCount=31;

intMonth1=12;

intYear1=`expr "$intYear1" - 1 | bc`;

else

tmpCount=$(GetDayCount "$intYear1" "$intMonth1");

fi

intDay1=`expr "$intDay1" + "$tmpCount" | bc`; #进行天数的累加

done

#计算日期中的天

intDay1=`expr "$intDay1" - "$intX" | bc`;

#进行字符格式化

intMonth2=$(Get0Str "$intMonth1");

intDay2=$(Get0Str "$intDay1");

result="$intYear1-$intMonth2-$intDay2";

if [ $strlen -gt 10 ]; then #当含有时,分,秒数据时,追加上去

strSubLen=`expr "$strlen" - 10 | bc`;

strHMS=`expr substr "$1" 11 "$strSubLen"`;

result="$result$strHMS";

fi

echo "$result";

}

#获取字符串长度

function GetStrLength

{

result=`echo "$1" | awk '{print length($0)}'`;

echo "$result";

}

#获取指定时间的前某个时间

#参数1:指定时间(格式:yyyy-MM-dd hh24:mi:ss 如:2014-03-07 09:09:09)

#参数2:计算的单位(DD 天数;HH 小时数)

#参数3:偏移量值

function GetPreviousDateTime

{

result="";

if [ "$2" = "HH" ]; then

result=$(GetPreviousDateTimeByHours "$1" "$3");

elif [ "$2" = "DD" ]; then

result=$(GetPreviousDateByDays "$1" "$3");

fi

echo "$result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值