新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
3shell編程知識(shí)
[TOC]
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、南岔ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的南岔網(wǎng)站制作公司
一,DAY6
1.shell腳本介紹
- shell是一種腳本語(yǔ)言 aming_linux blog.lishiming.net
- 可以使用邏輯判斷、循環(huán)等語(yǔ)法
- 可以自定義函數(shù)
- shell是系統(tǒng)命令的集合
- shell腳本可以實(shí)現(xiàn)自動(dòng)化運(yùn)維,能大大增加我們的運(yùn)維效率
2.shell腳本結(jié)構(gòu)和執(zhí)行
- 開(kāi)頭需要加
#!/bin/bash
,這是shell的固有格式,指定接下來(lái)要運(yùn)行的命令,是通過(guò)那一個(gè)解釋器來(lái)操作的 - 以#開(kāi)頭的行作為解釋說(shuō)明,某些啟動(dòng)腳本#號(hào)開(kāi)頭的行有特定作用
#! /bin/bash # chkconfig: 2345 10 90 定義啟動(dòng)級(jí)別, # description: Activates/Deactivates all network interfaces configured to \ 描述信息 這兩行一定要,沒(méi)有這兩行就不能添加到chkconfig列表里去 # start at boot time. #
- 腳本的名字以
.sh
結(jié)尾,用于區(qū)分這是一個(gè)shell腳本 - 執(zhí)行方法有兩種:
- chmod +x 1.sh; ./1.sh
- bash 1.sh
- 執(zhí)行腳本用bash或sh都可以
[root@mydb1 test]# ls -l /bin/sh lrwxrwxrwx 1 root root 4 Apr 24 2019 /bin/sh -> bash [root@mydb1 test]# ls -l /bin/bash -rwxr-xr-x 1 root root 906568 Mar 23 2017 /bin/bash
- 查看腳本執(zhí)行過(guò)程
bash -x 1.sh
- 查看腳本是否語(yǔ)法錯(cuò)誤
bash -n 1.sh
只能檢查語(yǔ)法上的錯(cuò)誤
3.date命令用法
date +%Y-%m-%d, date +%y-%m-%d
年月日date +%H:%M:%S = date +%T
時(shí)間date +%s
時(shí)間戳date -d @1504620492
date -d "+1day"
一天后date -d "-1 day"
一天前date -d "-1 month"
一月前date -d "-1 min"
一分鐘前date +%w, date +%W
星期演示:
[root@mydb1 test]# date +%y 簡(jiǎn)寫年 19 [root@mydb1 test]# date +%Y 2019 [root@mydb1 test]# date +%m 月份 11 [root@mydb1 test]# date +%M 分 40 [root@mydb1 test]# date +%d 天 20 [root@mydb1 test]# date +%D 月/日/年 11/20/19 [root@mydb1 test]# date +%Y%m%d 年月日 20191120 [root@mydb1 test]# date +%F 年-月-日 2019-11-20 [root@mydb1 test]# date +%H 時(shí) 11 [root@mydb1 test]# date +%s 時(shí)間戳,距離197001010000到現(xiàn)在過(guò)去多少秒 1574221403 [root@mydb1 test]# date +%S 秒 30 [root@mydb1 test]# date +%T 時(shí):分:秒 11:49:36 [root@mydb1 test]# date +%h 英文月份 Nov [root@mydb1 test]# date +%H:%M:%S 等同于T 11:52:40 [root@mydb1 test]# date +%w 星期幾 3 [root@mydb1 test]# date +%W 一年中第幾周 46 [root@mydb1 test]# cal 日歷形式 November 2019 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 29 30 [root@mydb1 ~]# date +'%Y%m%d' -d '1 days ago' 20191119 [root@mydb1 ~]# date +'%Y%m%d' -d '-1 days' 20191119 [root@mydb1 ~]# date +%F -d '1 year' 1年以后 2020-11-20 [root@mydb1 ~]# date +%F -d '-1 year' 1年以前 2018-11-20 [root@mydb1 ~]# date +%T 13:55:03 [root@mydb1 ~]# date +%T -d '-1 hour' 1小時(shí)以前 12:55:18 [root@mydb1 ~]# date +%s 1574229561 [root@mydb1 ~]# date -d @1574229561 將時(shí)間戳轉(zhuǎn)換為具體日期 Wed Nov 20 13:59:21 CST 2019 [root@mydb1 ~]# date +%s -d "2019-11-20 14:01:01" 將具體時(shí)間轉(zhuǎn)換為時(shí)間戳 1574229661
4.shell腳本中的變量
- 當(dāng)腳本中使用某個(gè)字符串較頻繁并且字符串長(zhǎng)度很長(zhǎng)時(shí)就應(yīng)該使用變量代替
- 使用條件語(yǔ)句時(shí),常使用變量 if [ $a -gt 1 ]; then ... ; fi
- 引用某個(gè)命令的結(jié)果時(shí),用變量替代 n=`wc -l 1.txt`
- 寫和用戶交互的腳本時(shí),變量也是必不可少的read -p "Input a number: " n; echo $n 如果沒(méi)寫這個(gè)n,可以直接使用$REPLY
- 內(nèi)置變量 $0, $1, $2… $0表示腳本本身,$1 第一個(gè)參數(shù),$2 第二個(gè) .... $#表示參數(shù)個(gè)數(shù)
- 數(shù)學(xué)運(yùn)算a=1;b=2; c=$(($a+$b))或者$[$a+$b]
5.shell腳本中的邏輯判斷
- 格式1:if 條件 ; then 語(yǔ)句; fi
#!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi
- 格式2:if 條件; then 語(yǔ)句; else 語(yǔ)句; fi
#!/bin/bash a=2 if [ $a -gt 3 ] then echo ok else echo nook fi
- 格式3:if …; then … ;elif …; then …; else …; fi
#!/bin/bash a=3 if [ $a -gt 4 ] then echo ">1" elif [ $a -lt 6 ] then echo "<6 && >1" else echo nook fi
- 邏輯判斷表達(dá)式:
- if [ $a -gt $b ]
- if [ $a -lt 5 ]
- if [ $b -eq 10 ]
- -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到處都是空格
- 可以使用 && || 結(jié)合多個(gè)條件
- if [ $a -gt 5 ] && [ $a -lt 10 ]; then 并且
- if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或者
二,DAY7
6.文件目錄屬性判斷
[ -f file ]
判斷是否是普通文件,且存在#!/bin/bash f="/tmp/test.txt" if [ -f $f ] then echo $f exist else touch $f fi 如果判斷不存在,加上感嘆號(hào)! #!/bin/bash f="/tmp/abc.txt" if [ ! -f $f ] then touch $f fi
[ -d file ]
判斷是否是目錄,且存在[ -e file ]
判斷文件或目錄是否存在[ -r file ]
判斷文件是否可讀[ -w file ]
判斷文件是否可寫[ -x file ]
判斷文件是否可執(zhí)行
7.if特殊用法
if [ -z "$a" ]
這個(gè)表示當(dāng)變量a的值為空時(shí)會(huì)怎么樣#!/bin/bash if [ ! -f /tmp/abc.log ] then echo "/tmp/abc.log not exist" exit fi n=`wc -l /tmp/abc.log` if [ -z "$n" ] then echo error exit elif [ $n -gt 100 ] then echo $n fi
if [ -n "$a" ]
表示當(dāng)變量a的值不為空,可以判斷一個(gè)文件的內(nèi)容不為空[root@localhost tmp]# if [ -n if.sh ];then echo ok;fi 判斷文件不用雙引號(hào) ok [root@localhost tmp]# if [ -n "$a" ];then echo $a;else echo "a is null";fi 如果是變量一定要加雙引號(hào) a is null
if grep -q '123'
1.txt; then表示如果1.txt中含有'123'的行時(shí)會(huì)怎么樣[root@localhost tmp]# grep -w 'zabbix' /etc/passwd zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin [root@localhost tmp]# [root@localhost tmp]# if grep -w 'zabbix' /etc/passwd;then echo "zabbix exist";fi zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin zabbix exist [root@localhost tmp]# if grep -wq 'zabbix' /etc/passwd;then echo "zabbix exist";fi -q表示僅僅做過(guò)濾,不把過(guò)濾的內(nèi)容顯示出來(lái) zabbix exist
if [ ! -e file ]; then
表示文件不存在時(shí)會(huì)怎么樣if (($a<1)); then …
等同于if [ $a -lt 1 ]; then…
[ ]
中不能使用<,>,==,!=,>=,<=這樣的符號(hào)
8.cace判斷(上)
格式:
case 變量名 in value1) command ;; value2) command ;; *) commond ;; esac
- 在case程序中,可以在條件中使用|,表示或的意思, 比如
- 2|3) command ;;
案例:
#!/bin/bash read -p "Please input a number: " n if [ -z "$n" ] then echo "Please input a number." exit 1 fi n1=`echo $n|sed 's/[0-9]//g'` if [ -n "$n1" ] then echo "Please input a number." exit 1 fi if [ $n -lt 60 ] && [ $n -ge 0 ] then tag=1 elif [ $n -ge 60 ] && [ $n -lt 80 ] then tag=2 elif [ $n -ge 80 ] && [ $n -lt 90 ] then tag=3 elif [ $n -ge 90 ] && [ $n -le 100 ] then tag=4 else tag=0 fi case $tag in 1) echo "not ok" ;; 2) echo "ok" ;; 3) echo "ook" ;; 4) echo "oook" ;; *) echo "The number range is 0-100." ;; esac
9.cace判斷(下)
10.for循環(huán)
- 語(yǔ)法:for 變量名 in 條件; do …; done
- 案例1
#!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] echo $i done echo $sum
- 案例2
文件列表循環(huán) #!/bin/bash cd /etc/ for a in `ls /etc/` do if [ -d $a ] then ? ?? ls -d $a fi done
新聞標(biāo)題:3shell編程知識(shí)
新聞來(lái)源:http://www.dlmjj.cn/article/jcgsce.html
其他資訊
- 可以語(yǔ)音對(duì)話的導(dǎo)航樹(shù)莓派怎么調(diào)用科大訊飛的語(yǔ)音庫(kù)實(shí)現(xiàn)語(yǔ)音識(shí)別?-創(chuàng)新互聯(lián)
- 深入淺析Java中的volatile-創(chuàng)新互聯(lián)
- JavaScript高級(jí)函數(shù)應(yīng)用之如何使用分時(shí)函數(shù)-創(chuàng)新互聯(lián)
- Git常見(jiàn)命令-創(chuàng)新互聯(lián)
- 使用jQuery怎么實(shí)現(xiàn)一個(gè)記住帳號(hào)密碼功能-創(chuàng)新互聯(lián)