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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章