1.判断一个字符串是否为空
if [[ -z ${year_month} ]]
then
echo "yes"
else
echo "no"
fi
2.日期之间的大小计算
#把日期转换为时间戳格式
num_login=`date -d ${time_login} +%s`
out_date=`date -d ${date} +%s`
#然后进行大小判断
if [ ${num_login} -gt ${out_date} ]
then
echo $account "--yes"
elif [ ${num_login} -eq ${out_date} ]
then
echo $account "-yes"
else
echo $account "no"
fi
3.文本排序重复行计算
#sort排序
#uniq对重复行计算
#sort -r 升序
4.字符串的分割
#可以使用cut,awk等命令
username=`echo $line|cut -d " " -f 1`
5.把拿到的日期相加或相减(年,月,日)
#拿到的年月日进行相加一年
year=`date -d "${year_month} +1 year " +%Y-%m-%d`
#拿到的年月日进行相加一个月
month=`date -d "${year_month} +1 month " +%Y-%m-%d`
#拿到的年月日进行相加一天
day=`date -d "${year_month} +1 day " +%Y-%m-%d`
#拿到的年月日进行相减一个月
sub_month=`date -d "${year_month} -1 month " +%Y-%m-%d`
6.删除文件的第一行和最后一行
#删除文件第一行
sed -i '1d' /path/file
#删除文件最后一行
sed -i '$d' /path/file
7.去掉文件中字符串的双引号
sed -i 's/"//g' /tmp/20210601.csv
8.字节转换,转换字节为 B KB MB GB
#Convert bytes to B KB MB GB
if [ 1024 -gt "$Bytesize" ];
then size="$Bytesize B"
elif [ 1048576 -gt "$Bytesize" ];
then
size=`echo "scale=3; a = $Bytesize / 1024 ; if (length(a)==scale(a)) print 0;print a" | bc `
size="$size KB"
elif [ 1073741824 -gt "$Bytesize" ];
then
size=`echo "scale=3; a = $Bytesize / 1048576 ; if (length(a)==scale(a)) print 0;print a" | bc `
size="$size MB"
elif [ 1073741824 -le "$Bytesize" ];
then
size=`echo "scale=3; a = $Bytesize / 1073741824 ; if (length(a)==scale(a)) print 0;print a" | bc `
size="$size GB"
else
size="0"
fi
echo "$size"
9.获取文件最后修改时间
stat -c %y file_name #获取文件修改的时间
stat -c %Y file_name #获取文件修改的时间戳
date +"%Y-%m-%d %H" -d @$File_time_stamp #把时间戳转变为系统时间显示