shell基礎

1.shell開頭需要指定解釋器,例如#!/bin/bash,指定解釋器的路徑爲/bin/bash,並讓解釋器得以從頭解釋腳本文件並執行腳本中的命令。

[root@localhost test_shell]# vim test.sh
#!/bin/bash
echo "123"

2.文件名通常以.sh結尾,雖然linux系統中後綴名與文件類型並無關,但是這樣的命名規則可以讓管理員更好地識別腳本的類別。

3.使用-x選項查看腳本的執行過程

[root@localhost test_shell]# bash -x test.sh
+ echo 123
123

4.使用-n選項來判斷shell腳本是否有語法錯誤,如果沒有錯誤則沒有任何輸出

5.date命令用法(date常用於對腳本執行產生的記錄文件等添加時間信息)

①直接執行date得到時間日期信息

[root@localhost test_shell]# date
Wed Feb  7 14:19:03 CST 2018

②附加選項獲取年、月、日等信息

[root@localhost test_shell]# date
Wed Feb  7 14:19:03 CST 2018
[root@localhost test_shell]# date +%Y        ⇒獲取年份
2018
[root@localhost test_shell]# date +%y        ⇒獲取年份
18
[root@localhost test_shell]# date +%m        ⇒獲取月份
02
[root@localhost test_shell]# date +%M        ⇒獲取分鐘
23
[root@localhost  test_shell]#  date  +%H                ⇒獲取小時數
14
[root@localhost  test_shell]#  date  +%h                ⇒獲取月份(英文簡稱)
Feb
[root@localhost  test_shell]#  date  +%s                ⇒獲取時間戳(距離1970/1/1的秒數)
1517985022
[root@localhost  test_shell]#  date  +%S                ⇒獲取秒數
29
[root@localhost  test_shell]#  date  +%T                ⇒獲取時間
14:33:25
[root@localhost  test_shell]#  date  +%H:%M:%S          ⇒獲取時間(與%T效果一致)
14:34:29
[root@localhost  test_shell]#  date  +%w                ⇒獲取星期幾
3
[root@localhost  test_shell]#  date  +%W                ⇒獲取本年內週數(年內第n周)
06
[root@localhost test_shell]# date +%d        ⇒獲取日期
07
[root@localhost test_shell]# date +%D        ⇒獲取日期(以 月/日/年 格式排列)
02/07/18
[root@localhost test_shell]# date +%Y%m%d        ⇒獲取日期(以 年月日 格式排列,無分隔字符)
20180207
[root@localhost test_shell]# date +%F                ⇒獲取日期(以 年-月-日 格式排列)
2018-02-07

③使用-d選項來獲取過去的時間

[root@localhost test_shell]# date -d "-1 day" +%F       ⇒顯示前一天的日期
2018-02-06
[root@localhost test_shell]# date -d "-1 year" +%F      ⇒顯示前一年的日期
2017-02-07
[root@localhost test_shell]# date -d "-1 month" +%F     ⇒顯示前一個月的日期
2018-01-07
[root@localhost test_shell]# date -d "-1 minute" +%T    ⇒顯示前一分鐘的時間
14:38:41
[root@localhost test_shell]# date -d "-1 hour" +%T      ⇒顯示前一小時的時間
13:39:58
[root@localhost test_shell]#

④時間戳轉換

[root@localhost test_shell]# date +%s -d "2018-02-07 14:34:29"
1517985269
[root@localhost test_shell]# date -d @1517985022
Wed Feb  7 14:30:22 CST 2018




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