参考连接:

  • https://blog.csdn.net/wujizhishui/article/details/119955956

一,date获取时间精确到毫秒

举例:

[gudong@k8s-node01 ~]$ date +'%Y-%m-%d/%H:%M:%S.%N' 
2022-05-02/16:53:51.717582175
[gudong@k8s-node01 ~]$ date +'%Y-%m-%d/%H:%M:%S.%N' | cut -b 1-23
2022-05-02/16:53:54.573

二,获取时间戳

参数

-d, --date=STRING         display time described by STRING, not 'now'
%s   seconds since 1970-01-01 00:00:00 UTC

举例:

[gudong@k8s-node01 ~]$ date +%s ; date -d "10 minute ago"  +%s
1651481660
1651481060

三,时间比较

例子1:

[gudong@k8s-node01 ~]$ [ 2 -ge 1 ] && echo "成立" || echo "不成立"
成立
[gudong@k8s-node01 ~]$ 
[gudong@k8s-node01 ~]$ [ 1 -ge 2 ] && echo "成立" || echo "不成立"
不成立
[gudong@k8s-node01 ~]$ 
[gudong@k8s-node01 ~]$ [ `date +%s` -ge `date -d "10 minute ago"  +%s` ] && echo "成立" || echo "不成立"
成立

[gudong@k8s-node01 ~]$ [ `date +%s` -le `date -d "10 minute ago"  +%s` ] && echo "成立" || echo "不成立"
不成立

例子2:

[gudong@k8s-node01 ~]$ old_time=`date +"%Y-%m-%d-%H:%M:%S"`
[gudong@k8s-node01 ~]$ new_time=`date +"%Y-%m-%d-%H:%M:%S"`
[gudong@k8s-node01 ~]$ echo $old_time
2022-05-11-08:25:47
[gudong@k8s-node01 ~]$ echo $new_time
2022-05-11-08:25:54
[gudong@k8s-node01 ~]$ [[ $new_time > $old_time ]] && echo "真" || echo "假"

[gudong@k8s-node01 ~]$ [[ $new_time < $old_time ]] && echo "真" || echo "假"

[gudong@k8s-node01 ~]$ [[ $new_time = $old_time ]] && echo "真" || echo "假"

[gudong@k8s-node01 ~]$ [[ $new_time -ge $old_time ]] && echo "真" || echo "假"
-bash: [[: 2022-05-11-08: value too great for base (error token is "08")

例子3:

[gudong@k8s-node01 ~]$ old_time=`date +"%Y-%m-%d-%H:%M:%S"`
[gudong@k8s-node01 ~]$ new_time=`date +"%Y-%m-%d-%H:%M:%S"`
[gudong@k8s-node01 ~]$ new_new_time=`date +"%Y-%m-%d-%H:%M:%S"`
[gudong@k8s-node01 ~]$ 
[gudong@k8s-node01 ~]$ 
[gudong@k8s-node01 ~]$ echo $old_time
2022-05-11-08:33:04
[gudong@k8s-node01 ~]$ echo $new_time
2022-05-11-08:33:10
[gudong@k8s-node01 ~]$ echo $new_new_time
2022-05-11-08:33:18
[gudong@k8s-node01 ~]$ 
[gudong@k8s-node01 ~]$ 
[gudong@k8s-node01 ~]$ [[ $new_time > $old_time && $new_time < $new_new_time ]] && echo "真" || echo "假"

[gudong@k8s-node01 ~]$ [[ $new_time > $old_time && $new_time > $new_new_time ]] && echo "真" || echo "假"

[gudong@k8s-node01 ~]$

四,linux shell串行执行

[gudong@k8s-node01 yyy]$ cat f1.sh 
echo `date +%H:%M:%S`
echo "第一次"
sh $1
#sleep 10
echo `date +%H:%M:%S`
echo "第二次"
[gudong@k8s-node01 yyy]$ 
[gudong@k8s-node01 yyy]$ cat f2.sh 
echo "f2"
sleep 10
echo "f2-2"
[gudong@k8s-node01 yyy]$ 
[gudong@k8s-node01 yyy]$ ./f1.sh f2.sh 
12:31:55
第一次
f2
f2-2
12:32:05
第二次
[gudong@k8s-node01 yyy]$

五,位置变量

前提须知:下面这种情况,位置参数是否生效,取决于第一个脚本有没有包括它。

[gudong@k8s-node01 var]$ cat 1.sh 
echo '第1次'
sh $1 $2
echo '第2次'
sh $1 $2 $3 
[gudong@k8s-node01 var]$ cat 2.sh 
echo '$1:' $1 
echo '$2:' $2
echo '$3:' $3 
[gudong@k8s-node01 var]$ ./1.sh 2.sh 10 20
1
$1: 10
$2:
$3:
2
$1: 10
$2: 20
$3:
[gudong@k8s-node01 var]$

1,例1

[gudong@k8s-node01 hh]$ cat 1.sh 
fun1()
{
echo this is 1.sh
}
fun1

sh $1 $2 
[gudong@k8s-node01 hh]$ cat 2.sh 
echo 哈哈哈哈
ss=$1
echo '$1' $1
echo '$2' $2
echo 'ss' $ss
[gudong@k8s-node01 hh]$ ./1.sh 2.sh 酷酷酷
this is 1.sh
哈哈哈哈
$1 酷酷酷
$2
ss 酷酷酷
[gudong@k8s-node01 hh]$ ./1.sh 2.sh 酷酷酷 给力给力
this is 1.sh
哈哈哈哈
$1 酷酷酷
$2
ss 酷酷酷


[gudong@k8s-node01 hh]$ ./2.sh 酷酷酷 给力给力
哈哈哈哈
$1 酷酷酷
$2 给力给力
ss 酷酷酷

2,例2

[gudong@k8s-node01 hh]$ cat 1.sh 
fun1()
{
echo this is 1.sh
}
fun1

script_name=$1
shift 1
sh $script_name "$@"
[gudong@k8s-node01 hh]$ cat 2.sh 
echo 哈哈哈哈
ss=$1
echo '$1' $1
echo '$2' $2
echo 'ss' $ss
[gudong@k8s-node01 hh]$ ./1.sh 2.sh 酷酷酷 给力给力
this is 1.sh
哈哈哈哈
$1 酷酷酷
$2 给力给力
ss 酷酷酷

3,例3

[gudong@k8s-node01 yyy]$ cat f3.sh 
echo $1 $2 $3
fun1(){
echo '$1:' $1
echo '$3:' $3
}
fun1 $3
echo "-----------"
sh f4.sh $3

[gudong@k8s-node01 yyy]$ cat f4.sh 
echo '$1:' $1
echo '$3:' $3
[gudong@k8s-node01 yyy]$ 
[gudong@k8s-node01 yyy]$ sh f3.sh 1 2 3
1 2 3
$1: 3
$3:
-----------
$1: 3
$3:

六,shift

[gudong@k8s-node01 hh]$ cat 3.sh 
echo $1 $2 $3
shift 2
echo $1

[gudong@k8s-node01 hh]$ sh 3.sh 1 2 3
1 2 3
3
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐