Linux Shell脚本中常用的操作

1.获取当前脚本路径

2.判读字符串变量为空和不为空

3.获取日期及相关操作

4.脚本输入参数指定与判定

5.数组遍历

6.shell执行结果判断

7.读配置文件

8.获取当前机器IP

9.程序暂停一段时间

10.字符串截取与替换

11.发送邮件

12.读写数据库

13.压缩与解压缩

14.判断进程是否存在(程序是否在运行)

15.日期循环

#!/bin/bash
#1.current path
#method 1:
current_dir=`pwd`
echo "current path:$current_dir"

#method 2:
current_dir=$(cd `dirname $0`;pwd)
echo "current path:$current_dir"

#2.string empty and not empty
#string is empty
str=""
if [ -z "$str" ];then
  echo "string is empty"
else 
  echo "string is not empty"
fi

#string is not empty
str1="shell is good"
if [ -n "$str1" ];then
  echo "string is not empty"
else 
  echo "string is empty"
fi

#3.date operation
now_date=`date "+%Y-%m-%d"`
echo "now date:$now_date"

now_time=`date "+%Y-%m-%d %H:%M:%S"`
echo "now time:$now_time"

before_date=`date -d "$now_date 1 days ago" "+%Y-%m-%d"`
echo "before date:$before_date"

after_date=`date -d "$now_date -1 days ago" "+%Y-%m-%d"`
echo "after date:$after_date"

unix_time=`date -d "$now_date -1 days ago" "+%s"`
echo "unix_time:$unix_time"

year=`date -d "$now_date -1 days ago" "+%Y"`
echo "year:$year"

month=`date -d "$now_date -1 days ago" "+%m"`
echo "month:$month"

day=`date -d "$now_date -1 days ago" "+%d"`
echo "day:$day"

hour=`date "+%H"`
echo "hour:$hour"

minute=`date "+%M"`
echo "minute:$minute"

second=`date "+%S"`
echo "second:$second"

day_of_year=`date "+%j"`
echo "day of year:$day_of_year"

day_of_week=`date -d "$now_date -2 days ago" "+%w"`
echo "day of week:$day_of_week"

#今天是本周的第几天,如果是周日,系统默认为第0天,改写为第六天
whichday=`date -d "$now_date" +%w`
if [ $whichday == 0 ] ; then
    whichday=7
fi

#本周第一天/本周最后一天/本月第一天/本月最后一天
this_week_first=`date -d "$now_date -$[${whichday}-1] days" +%Y-%m-%d`
this_week_end=`date -d "$this_week_first+6 days" +%Y-%m-%d`
this_month_first=`date -d "$now_date" +%Y-%m-01`
this_month_end=`date -d "$(date -d "$now_date 1 month" +%Y-%m-01) -1 day" +%Y-%m-%d`
#上月时间/上月第一天/上月最后一天
last_month_day=`date -d "${now_date} last month" "+%Y-%m-%d"`
last_month_first=`date -d "$last_month_day" +%Y-%m`"-01"
last_month_end=`date -d "${this_month_first} yesterday" "+%F"`
#上周时间/上周第一天/上周最后一天
last_week_day=`date -d "$now_date -1 week" +%Y-%m-%d`
last_week_first=`date -d "$last_week_day -$[${whichday}-1] days" +%Y-%m-%d`
last_week_end=`date -d "$last_week_first+6 days" +%Y-%m-%d`
echo "this_week_first:$this_week_first"
echo "this_week_end:$this_week_end"
echo "this_month_first:$this_month_first"
echo "this_month_end:$this_month_end"
echo "last_month_day:$last_month_day"
echo "last_month_first:$last_month_first"
echo "last_month_end:$last_month_end"
echo "last_week_day:$last_week_day"
echo "last_week_first:$last_week_first"
echo "last_week_end:$last_week_end"

echo `date -d "2 weeks" "+%Y-%m-%d"`
echo `date -d "next monday" "+%Y-%m-%d"`
echo `date -d "next friday" "+%Y-%m-%d"`
echo `date -d "next-day" "+%Y-%m-%d"`
echo `date -d "tomorrow" "+%Y-%m-%d"`
echo `date -d "last-day" "+%Y-%m-%d"`
echo `date -d "yesterday" "+%Y-%m-%d"`
echo `date -d "last month" "+%Y-%m-%d"`
echo `date -d "next-month" "+%Y-%m-%d"`

#4.args
help_str="Usage:\n
              -f file_name \n
              -d process date,eg:2020-01-01 \n
              -i init "
file_name=""
process_date="" 
init="false"
while getopts "f:d:i:h" opt
do
    case $opt in
        f) file_name=$OPTARG
        ;;
        d) process_date=$OPTARG
        ;;
        i) init=$OPTARG
        ;;
        h) echo $help_str
           exit 0
        ;;
        \?) echo "Invalid option: -$OPTARG"
            echo $help_str
            exit -1
        ;;
  esac
done
echo "file_name:$file_name"
echo "process_date:$process_date"
echo "init:$init"

#5.array iterator
arr=("role" "order" "go")
for kw in ${arr[*]}
do
  echo "keyword:$kw"
done

#6.shell result
path=`pwd`
if [ $? -ne 0 ];then
  echo "execute fail"
else
  echo "execute success"
fi

#7.read config file
#config file example:
#mysql ip
#mysql_ip=192.168.1.1

#mysql端口
#mysql_port=3306

#mysql登陆用户名
#mysql_user=root

#mysql登陆密码
#mysql_password=123456
while read line;do
   eval "$line"
done < mysql.properties

echo "mysql ip:              $mysql_ip"
echo "mysql port:            $mysql_port"
echo "mysql login username:  $mysql_user"
echo "mysql login password:  $mysql_password" 

#8.current machine ip
ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}')
echo "ipaddress:$ipaddr"

#9.stop 
sleep 0.1s

#10.string split and replace
path="hdfs://user/hive/warehouse/os.db/order"
echo "path:$path"
args1=${path#*/}
echo "args1:$args1"
args2=${path##*/}
echo "args2:$args2"
args3=${path%/*}
echo "args:$args3"
args4=${path%%/*}
echo "args4:$args4"

echo ${#path}
len=`expr length $path`
echo $len
substr=${path:0:4}
echo "substr:$substr"


table=ods.order
schema=`echo $table|cut -d "." -f 1`
name=`echo $table|cut -d "." -f 2`
echo "schema:$schema,name:$name"

rep=`echo $path|sed "s/order/user/g"`
echo "rep:$rep"

#11.send email
https://blog.csdn.net/m0_37673753/article/details/81356298

#12.read and write database
https://blog.csdn.net/javajxz008/article/details/104554967

#13.zip unzip
sync_job_dir="sync"
zip $sync_job_dir.zip -q -r "$sync_job_dir"

unzip $sync_job_dir.zip -d all $sync_job_dir

#14.is exists pid
ps -ef|grep class-ip-etl |grep -v grep
res=$?
if [ $res -ne 0 ];then
    echo "program not running"
else
    echo "program is running"
fi

#15.date loop
start_date="2020-02-01"
end_date="2020-01-01"

start_sec=`date -d "$start_date" "+%s"`
end_sec=`date -d "$end_date" "+%s"`

for((mid_sec=$start_sec;mid_sec>=$end_sec;mid_sec-=86400))
do
    biz_date=`date -d "@$mid_sec" "+%Y-%m-%d"`
    echo "biz_date:$biz_date"
done

 

你可能感兴趣的:(linux)