shell是什麼shell
實例01:bash
[root@qingyun-01 shell]# cat 1.sh #!/bin/bash echo "123"
[root@qingyun-01 shell]# chmod a+x 1.sh [root@qingyun-01 shell]# ./1.sh 123
[root@qingyun-01 shell]# bash 1.sh 123
[root@qingyun-01 shell]# sh -x 1.sh + echo 123 123
[root@qingyun-01 shell]# sh -n 1.sh #無錯誤 #只用於檢測shell的沒法錯誤
顯示當前時間:運維
[root@qingyun-01 ~]# date 2018年 02月 05日 星期一 17:55:33 CST #變動爲英文 [root@qingyun-01 ~]# LANG=en [root@qingyun-01 ~]# date Mon Feb 5 17:57:24 CST 2018
;年 [root@qingyun-01 ~]# date +%Y 2018 ;取後兩位 [root@qingyun-01 ~]# date +%y 18 ;月份 [root@qingyun-01 ~]# date +%m 02 ;號(日) [root@qingyun-01 ~]# date +%d 05 ;日期 [root@qingyun-01 ~]# date +%D 02/05/18 ;即 [root@qingyun-01 ~]# date +%Y-%m-%d 2018-02-05 ;或 [root@qingyun-01 ~]# date +%F 2018-02-05 [root@qingyun-01 ~]# date +%y-%m-%d 18-02-05
;時 [root@qingyun-01 ~]# date +%H 18 ;分 [root@qingyun-01 ~]# date +%M 07 ;秒 [root@qingyun-01 ~]# date +%S 11 ;即 [root@qingyun-01 ~]# date +%H:%M:%S 18:06:43 ;或 [root@qingyun-01 ~]# date +%T 18:08:48
;時間戳 [root@qingyun-01 ~]# date +%s 1517825370
#根據時間戳轉換成日期 [root@qingyun-01 ~]# date -d @1517825370 Mon Feb 5 18:09:30 CST 2018
[root@qingyun-01 ~]# date -d "-1 day" Sun Feb 4 18:19:25 CST 2018 [root@qingyun-01 ~]# date -d "-1 month" +%F 2018-01-05
[root@qingyun-01 ~]# date -d "-1 years" +%F 2017-02-05
[root@qingyun-01 ~]# date -d "-1 month" +%F 2018-01-05
[root@qingyun-01 ~]# date -d "-1 min" +%F 2018-02-05
[root@qingyun-01 ~]# date +%w 1 ;是今年的多少周(星期) [root@qingyun-01 ~]# date +%W 06
[root@qingyun-01 ~]# cal February 2018 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
當腳本中使用某個字符串,屢次頻繁而且字符串長度很長時,就應該使用變量代替ide
使用條件語句時,常使用變量 if [ $a -gt 1];then ...;fi函數
引用某個命令的結果時,用變量替代 n=wc -l 1.txt
code
寫和用戶交互的腳本時,變量也是必不可少的 read -p "lnput a number:" n; echo $n 若是沒有寫這個n, 能夠直接使用$REPLY字符串