循環(huán)控制語(yǔ)句---continue疙描、break诚隙;
continue:
循環(huán)控制語(yǔ)句continue 依據(jù)條件跳過(guò)繼續(xù)執(zhí)行循環(huán), ---continue 2---表示跳轉(zhuǎn)到當(dāng)前循環(huán)往外第二層循環(huán)起胰;
用于循環(huán)體中continue【N】;提前結(jié)束N層的本輪循環(huán)久又,而直接進(jìn)入下一輪判斷;最內(nèi)層為第一層
break:
循環(huán)控制語(yǔ)句break 終端效五,退出
用于循環(huán)體中
---break【N】;提前結(jié)束第N層循環(huán)地消,最內(nèi)層為第一層
可以退出死循環(huán);
#while true;do [ $i -eq 5 ] && break ;echo i=$i;sleep 0.3;let i++; done
if語(yǔ)句畏妖;
1脉执、單分支
if 判斷條件;then
條件為真的分支代碼
fi
# if cmd1戒劫; then cmd2;fi;cmd3
####################################################
cmd1執(zhí)行成功半夷,執(zhí)行cmd2,接著執(zhí)行cmd3
cmd1執(zhí)行不成功,則不執(zhí)行cmd2,跳過(guò)cmd2直接執(zhí)行cmd3
####################################################
2迅细、雙分支
if 判斷條件:then
條件為真的分支代碼
else
條件為假的分支代碼
fi
3玻熙、多分支
if 判斷條件1;then
條件為真的分之代碼
elif 判斷條件2疯攒;then
條件為真的分支代碼
elif 判斷條件3:then
條件為真的分支代碼
else
以上條件都為假的分支代碼
fi
####################################################
#if cmd1:then cmd2;elif cmd3;then cmd4;elif cmd5;then cmd6;else cmdn;fi
命令1執(zhí)行成功接著執(zhí)行命令2嗦随、如果命令1 執(zhí)行不成功,將從新再次判斷命令3是否成功敬尺、枚尼、、else指前面的全都不成功將執(zhí)行命令N
####################################################
read -p "please input your age: " age
[[ ! "$age" =~ ^[[:digit:]]+$ ]] && echo please input digital && exit 10
#如果非數(shù)字砂吞,提示請(qǐng)輸入數(shù)字署恍,并且退出;
if [ "$age" -le 18 ]; then
echo "you are baby"
#判斷是否小于18蜻直,小于等于18則輸出you are beby ,大于18則跳至下一行盯质;
elif [ "$age" -gt 18 -a "$age" -le 60 ];then
echo "you need work hard"
#判斷年齡,判斷大于18并小于等于60概而,則輸出you need work hard.不在此范圍則跳至下一行呼巷;
elif [ "$age" -le 80 ];then
echo "you can enjoy the life"
#判斷是否小于等于80,則輸出you can enjoy the life,大于80則跳至下一行赎瑰;
else
echo "you will be lucky"
#以上判斷全部錯(cuò)誤則輸出you will be lucky;則表示大于80王悍;
fi
-ge 是否大于等于
-le 是否小于等于
-eq 是否等于
-ne 是否不等于
-gt 是否大于
-lt 是否小于
. 匹配任意單個(gè)字符
[] 匹配指定范圍內(nèi)的任意單個(gè)字符
[^] 匹配指定范圍外的任意單個(gè)字符
[:alnum:] 字母和 數(shù)字
[:alpha:] 代表任何英文大小寫(xiě)字符,亦即 A-Z, a-z
[:lower:] 小寫(xiě)字母 [:upper:] 大寫(xiě)字母
[:blank:] 空白字符(空格和制表符)
[:space:] 水平和垂直的空白字符(比[:blank:] 包含的范圍廣)
[:cntrl:] 不可打印的控制字符(退格餐曼、刪除压储、警鈴...) )
[:digit:] 字 十進(jìn)制數(shù)字 [:xdigit:] 十六進(jìn)制數(shù)字
[:graph:] 可打印的非空白字符
[:print:] 可打印字符
[:punct:] 標(biāo)點(diǎn)符號(hào)
查看使用率
[root@centos6 ~]# df| sed -rn "/^\/dev\/sd/p"
/dev/sda2 100660656 4621768 90918888 5% /
/dev/sda3 50264772 53076 47651696 1% /app
/dev/sda1 991512 40312 900000 5% /boot
[root@centos6 ~]# df| sed -rn "/^\/dev\/sd/s/^.*[[:space:]]+(.*)%.*$/\1/p"
5
1
5
[root@centos6 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 100660656 4621768 90918888 5% /
tmpfs 953636 228 953408 1% /dev/shm
/dev/sda3 50264772 53076 47651696 1% /app
/dev/sda1 991512 40312 900000 5% /boot
/dev/sr0 3878870 3878870 0 100% /media/CentOS_6.9_Final
[root@centos6 ~]# df |sed -rn '/^\/dev\/sd/s/^.*[[:space:]]+([[:digit:]]{1,3})%.*$/\1/p'
5
1
5
case 語(yǔ)句
case支持glob風(fēng)格的通配符:
*:任意長(zhǎng)度任意字符
鲜漩?:任意單個(gè)字符
[]:指定范圍內(nèi)的任意單個(gè)字符
a|b:a或b
case 變量引用 in
PAT1)
分支1
;集惋;
PAT2)
分支2
孕似;;
...
*)
默認(rèn)分支
刮刑;喉祭;
esac
[root@centos6 ~]#vim case.sh
#!/bin/bash
read -p "please input a digit " n
case $n in
1|3|5) ------是1、3为朋、5其中一個(gè)就輸出cmd1
echo cmd1
;;
2|4|6)
echo cmd2
;;
10|20|30)
echo cmd3
;;
*) --------不符合上面條件時(shí)輸出defoult cmd
echo defoult cmd
esac
[root@centos6 ~]#. case.sh
please input a digit 2
cmd2
[root@centos6 ~]#. case.sh
please input a digit 3
cmd1
[root@centos6 ~]#. case.sh
please input a digit 4
cmd2
[root@centos6 ~]#. case.sh
please input a digit 7
defoult cmd
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
#!/bin/bash
read -p "Do you yue? " n
case $n in
1|3|5)
echo cmd1
;;
2|4|6)
echo cmd2
;;
[1-3]0)
echo cmd3
;;
*)
echo defoult cmd
esac
[root@centos6 ~]#vim yue.sh
read -p "yue bu yue? (yes or no) " ans
ans=`echo $ans |tr '[:upper:]' '[:lower:]'`----大小寫(xiě)轉(zhuǎn)換賦值
case $ans in
y|ye|yes)
echo ok,yue
;;
n|no)
echo no,buyue
;;
esac
##########################################################
for循環(huán)
循環(huán)語(yǔ)句
for臂拓、 while厚脉、until
for 語(yǔ)句习寸;
for 變量名 in 列表;do 變量名不加$
循環(huán)體
傻工;done
執(zhí)行機(jī)制:
依次將列表中的元素賦值給“變量名”霞溪;每次賦值后即執(zhí)行一次循環(huán)體;直到列表中元素耗盡中捆,循環(huán)結(jié)束鸯匹;
for循環(huán)
列表生成方式:
(1)直接給出列表
(2)整數(shù)列表
(a){start..end}
(b) $(seq [start[step]] end)
(3) 返回列表命令
$(COMMAND)
(4) 使用glob,如:*.sh
(5) 變量引用-----------------------------P刮薄E古睢!
$@ , $*
[root@centos6 ~]#for n in 1 2 5 7 8 ;do echo $n ;sleep 0.5 ;done
1
2
5
7
8
[root@centos6 ~]#for n in /var/log/*.log ;do echo "filename=`basename $n`" ;sleep 0.5 ;done
filename=anaconda.ifcfg.log
filename=anaconda.log
filename=anaconda.program.log
filename=anaconda.storage.log
filename=anaconda.yum.log
filename=boot.log
filename=dracut.log
filename=mysqld.log
filename=pm-powersave.log
filename=spice-vdagent.log
filename=wpa_supplicant.log
filename=Xorg.0.log
filename=Xorg.1.log
filename=Xorg.2.log
filename=Xorg.9.log
filename=yum.log
###basename 只顯示基名###
##########################################################
for循環(huán)
[root@centos6 ~]#sum=0;for i in {1..100..5};do let sum+=i;done;echo sum=$sum
sum=970
相加
bc計(jì)算
[root@centos6 ~]#echo {1..100..5}
1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
[root@centos6 ~]#echo {1..100..5}|tr " " "+"
1+6+11+16+21+26+31+36+41+46+51+56+61+66+71+76+81+86+91+96
[root@centos6 ~]#echo {1..100..5}|tr " " "+"|bc
970
![for特殊用法.png](http://upload-images.jianshu.io/upload_images/6132998-b4eb7e187eb56eaa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##########################################################
ping -[options]
- w的最后期限
在ping退出之前蟋滴,指定一個(gè)超時(shí)染厅,
發(fā)送或接收的數(shù)據(jù)包少了。在
這種情況下津函,ping在發(fā)送數(shù)據(jù)包后不會(huì)停止,
它會(huì)等待最后期限到期尔苦,或者直到計(jì)數(shù)探針
對(duì)網(wǎng)絡(luò)中的錯(cuò)誤通知進(jìn)行應(yīng)答或發(fā)送涩馆。
-W超時(shí)
在幾秒鐘內(nèi)等待響應(yīng)。的選項(xiàng)
只影響任何反應(yīng)的超時(shí)時(shí)間允坚,其他的——
聰明的ping等待著兩個(gè)rtt魂那。
root@centos6 ~]#arp 查看MAC地址
Address HWtype HWaddress Flags Mask Iface
172.17.252.218 (incomplete) eth1
172.17.252.243 (incomplete) eth1
192.168.239.1 ether 00:50:56:c0:00:01 C eth0
172.17.252.185 (incomplete) eth1
172.17.252.28 (incomplete) eth1
172.17.252.194 (incomplete) eth1
172.17.252.15 (incomplete) eth1
for語(yǔ)句特殊用法
for ((cmd1;cmd2;cmd3))
do
cmd4
done
[root@centos7 ~]#sum=0;for i in {1..100..2};do let sum+=i;done;echo sum=$sum
sum=2500
[root@centos7 ~]#for((sum=0,i=1;i<=100;i+=2));do let sum+=i;done;echo sum=$sum
sum=2500
[root@centos7 ~]#sum=0;i=1;while [ $i -le 100 ];do let sum+=i;let i+=2; done;echo sum=$sum
sum=2500
#################################################
nmap掃描端口 arp查看MAC地址
掃描端口;
[root@centos6 ~]#nmap -v -A 172.17.252.8
Starting Nmap 5.51 ( http://nmap.org ) at 2017-06-13 09:59 CST
NSE: Loaded 57 scripts for scanning.
Initiating ARP Ping Scan at 09:59
Scanning 172.17.252.8 [1 port]
Completed ARP Ping Scan at 09:59, 0.02s elapsed (1 total hosts)
Initiating Parallel DNS resolution of 1 host. at 09:59
Completed Parallel DNS resolution of 1 host. at 09:59, 6.61s elapsed
Initiating SYN Stealth Scan at 09:59
Scanning 172.17.252.8 [1000 ports]
Discovered open port 22/tcp on 172.17.252.8
Completed SYN Stealth Scan at 09:59, 5.09s elapsed (1000 total ports)
Initiating Service scan at 09:59
Scanning 1 service on 172.17.252.8
Completed Service scan at 09:59, 0.06s elapsed (1 service on 1 host)
Initiating OS detection (try #1) against 172.17.252.8
Retrying OS detection (try #2) against 172.17.252.8
NSE: Script scanning 172.17.252.8.
Initiating NSE at 09:59
Completed NSE at 09:59, 0.09s elapsed
Nmap scan report for 172.17.252.8
Host is up (0.0013s latency).
Not shown: 999 filtered ports
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 5.3 (protocol 2.0)
| ssh-hostkey: 1024 e3:96:d3:12:5d:13:49:3c:d2:16:c9:d8:40:14:fa:8a (DSA)
|_2048 f5:ea:a1:0c:c4:e7:b2:08:a7:7e:38:eb:c7:96:5c:a5 (RSA)
MAC Address: 00:0C:29:F7:2B:AD (VMware)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: specialized|WAP|general purpose
Running (JUST GUESSING): Crestron 2-Series (90%), Netgear embedded (90%), Linux 2.6.X (86%)
Aggressive OS guesses: Crestron XPanel control system (90%), Netgear DG834G WAP (90%), Linux 2.6.24 - 2.6.35 (86%), Linux 2.6.22 (85%), Linux 2.6.23 - 2.6.33 (85%), Linux 2.6.28 (Gentoo) (85%), Linux 2.6.31 - 2.6.34 (85%), Linux 2.6.9 - 2.6.27 (85%)
No exact OS matches for host (test conditions non-ideal).
Uptime guess: 0.124 days (since Tue Jun 13 07:00:56 2017)
Network Distance: 1 hop
TCP Sequence Prediction: Difficulty=262 (Good luck!)
IP ID Sequence Generation: All zeros
TRACEROUTE
HOP RTT ADDRESS
1 1.29 ms 172.17.252.8
Read data files from: /usr/share/nmap
OS and Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 16.26 seconds
Raw packets sent: 2059 (94.192KB) | Rcvd: 36 (3.228KB)
查看IP地址頻通情況稠项;
[root@centos7 bin]#cat IP2.sh
#冰寻!/bin/bash
up=0
down=0
net=172.17.252
for i in {1..100}
do
ping -c1 -W1 $net.$i &> /dev/null && { echo $net.$i is up;let ++up ; } || { echo $net.$i is down;let ++down ; }
done
echo the up host is $up
echo the host is $down
[root@centos7 bin]#ps -ax |grep IP2.sh
11618 pts/2 S+ 0:00 bash IP2.sh
11700 pts/1 S+ 0:00 grep --color=auto IP2.sh
##############################################
打印星;不換行
echo -e \c
echo -n
兩種不換行
##############################################
[root@centos6 ~]#cat xing.sh
#!/bin/bash
x=16
y=8
for i in `seq $y`
do
for j in `seq $x`
do
echo -e '*\c'
done
echo
done
[root@centos6 ~]#vim 9*9.sh
#!/bin/bash
for i in `seq 9` ;do----i單一調(diào)出seq9列出的數(shù)字
for j in `seq $i` ;do ---j單一調(diào)出seq $i列出的數(shù)字皿渗。
let x=j*i 乘法運(yùn)算
echo -en "$j*$i=$x\t"
done
echo
done
---\t為 TAB鍵為對(duì)齊---
echo -e為激活轉(zhuǎn)義 -n 為不換行
[root@centos6 ~]#. 9*9.sh
1=1
2=2 2=4
3=3 3=6 3=9
4=4 4=8 4=12 4=16
5=5 5=10 5=15 5=20 5=25
6=6 6=12 6=18 6=24 6=30 6=36
7=7 7=14 7=21 7=28 7=35 7=42 7=49
8=8 8=16 8=24 8=32 8=40 8=48 8=56 8=64
9=9 9=18 9=27 9=36 9=45 9=54 9=63 9=72 9=81
[root@centos6 ~]#vim 9*9.sh
#!/bin/bash
for i in `seq 9` ;do
for j in `seq $i` ;do
let x=j*i
echo -en "${j}x${i}=${x}\t"
done
echo
done
[root@centos6 ~]#. 9*9.sh
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
[root@centos6 ~]#vim 9*9.2.sh
#!/bin/bash
for i in `seq 9` ;do
for j in `seq $i`;do
echo -ne "$i*$j=$[j*i]\t"
done
echo
done
~
##############################################
[root@centos6 ~]#n=-20;[[ $n =~ ^-?[[:digit:]]+$ ]]&& echo true
true
[root@centos6 ~]#ans=Y;ans=`echo $ans |tr 'A-Z' 'a-z'`; [[ $ans =~ ^yes|y$ ]]&&echo yes
yes
雙中括號(hào)支持正則表達(dá)式
##############################################
三角形
read -p "請(qǐng)輸入行數(shù):" x
for i in `seq` $x ; do
let a=2*i-1
let b=x-i
for k in seq $b ; do
echo -n " "
done
for z in seq $a ; do
echo -n "*"
done
echo
done
##############################################
2*第幾行的編號(hào)=第幾行的星的個(gè)數(shù)
總行數(shù)-當(dāng)前行的編號(hào)=等于當(dāng)前行的空格數(shù)
#############################################
[root@centos6 ~]#vim sanjiao.sh
#!/bin/bash
read -p "請(qǐng)輸入數(shù)字:" x
[[ 斩芭!$x =~ [:digit:] ]] &&{ echo '請(qǐng)輸入數(shù)字'; exit ; } || echo '等邊三角形'
雙中括號(hào)轻腺,是否包含, 看成整體划乖,需加大括號(hào)贬养;
for i in `seq $x` ; do
let a=x-i
let b=2*i-1
for z in `seq $a`; do
echo -n " "
done
for y in `seq $b`; do
echo -n "*"
done
echo
done
#################################################
while循環(huán)
while CONDITION ; do
循環(huán)體
done
CONDITION: 循環(huán)控制條件;進(jìn)入循環(huán)之前琴庵,先做一次判斷误算;每一次循環(huán)之后會(huì)再次做判斷;條件為“true”,則執(zhí)行一次循環(huán)迷殿;直到條件測(cè)試狀態(tài)為“false”終止循環(huán)
因此:CONDITION一般應(yīng)該有循環(huán)控制變量儿礼;而此變量的值 會(huì)在循環(huán)體不斷地陪修正
進(jìn)入條件:CONDITION為true
退出條件:CONDITION為false
特殊用法
while 循環(huán)特殊用法
while read line ; do
循環(huán)體
done < /PATH/FROM/SOMEFILE
依次讀取文件中的每一行,并將行賦值給變量line
#################################################
選取隨機(jī)密碼庆寺;
bDN9ajsv
[root@centos6 ~]#cat /dev/urandom |tr -dc 'a-zA-Z0-9'|head -c 8
#####################################
tr -dc -----刪除并替換
#####################################
[root@centos6 ~]#vim while.sh
#!/bin/bash
i=1
while [ "$i" -le 10 ];do
useradd user$i
echo "user$i is created"
password=`cat /dev/urandom |tr -dc 'a-zA-Z0-9'|head -c 8`
echo $password |passwd --stdin user$i &> /dev/null
let i++ ;
done
##########################################################
[root@centos6 ~]#chage -d0 user1
重設(shè)口令
[root@centos6 ~]#passwd -e user2
重設(shè)口令
##########################################################
until
#################################################
until CONDITION;do 倒序 until false; do
循環(huán)體
循環(huán)體
done Done
#################################################
```
######continue ,break
```
循環(huán)控制語(yǔ)句continue ,break
用于循環(huán)中
continue [N];提前結(jié)束第N層的本輪循環(huán)蚊夫,而直接進(jìn)入下一輪判斷;最內(nèi)層為第一層
N --- 表示最外層循環(huán)懦尝;
#!/bin/bash
for i in {1..10}
do
[ $i -eq 5 ] && continue---結(jié)束本次循環(huán)
echo i=$1
sleep 0.5
done
echo test is finished
#!/bin/bash
for i in {1..10}
do
[ $i -eq 5 ] && break --- 結(jié)束整個(gè)循環(huán)
echo i=$1
sleep 0.5
done
echo test is finished
#################################################
```
######循環(huán)控制shift命令
```
shift [N]
用于將參量列表list 左移指定次數(shù)知纷,缺省為左移一次。
參數(shù)列表 list 一旦被移動(dòng)陵霉,最左端的那個(gè)參數(shù)就從列表中刪除琅轧。while 循環(huán)遍歷位置參量列表時(shí),常用到shift
#################################################
```
######trap信號(hào)捕捉踊挠;
```
#乍桂!/bin/bash
trap 'echo int' int
trap -p
for i in {1..10};
do
echo i=$i
sleep 0.3
done
trap '' int
trap -p
for i in {11..20}
do
echo i=$i
sleep 0.3
done
trap '-' int ---- 恢復(fù)信號(hào),不再捕捉
trap -p
for i in {21..30}
do
echo i=$i
sleep 0.3
done
腳本效床;
#!/bin/bash
date1="Monday"
date2="Tuesday"
date3="Wednesday"
date4="Thursday"
date5="Friday"
date6="Saturday"
date7="Sunday"
for week in `date +%A`;do date +%A 顯示星期幾
if [ $week==$date1 ];then ==-----是否等于
echo "This is $week" && ls -l
elif [ $week==$date2 ];then
echo "Thsi is $week" && pwd
elif [ $week==$date3 ];then
echo "This is $week" && cal
elif [ $week==$date4 ];then
echo "This is $week" && tty
elif [ $week==$date5 ];then
echo "this is $week" && id
elif [ $week==$date6 ];then
echo "This is $week" && du -sh ---顯示當(dāng)前目錄的總和大小
else
echo "This is $week" && history -r ---將歷史命令讀入緩沖區(qū)中
fi
done
[root@centos7 app]#cal
July 2017
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 31
[root@centos7 app]#tty
/dev/pts/1
[root@centos7 app]#id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
###統(tǒng)計(jì)文件行數(shù)睹酌;
[root@centos7 app]#vim wcfile.sh
#!/bin/bash
read -p "please input a file way name or quit:" file
until [ $file == 'quit' ]; do 條件判斷; 是否等于
line=`cat $file | wc -l`
echo "The file have a $line line!"
read -p "pieace input a file way name:" file
done
```
######select循環(huán)
```
select varible in list
do
循環(huán)體命令
done
select 循環(huán)主要用于創(chuàng)建菜單扁凛,按數(shù)字順序排列的才單項(xiàng)獎(jiǎng)顯示在標(biāo)準(zhǔn)錯(cuò)誤上忍疾,并顯示PS3提示符,等待用戶輸入
用戶輸入菜單中的某個(gè)數(shù)字谨朝,執(zhí)行相應(yīng)的命令
用戶輸入被保存在內(nèi)置變量REPLY中
select menu in huimian hulatang yangtang
do
[ -z "$menu" -o $menu= "exit" ] && break
echo your choose is $menu
done
#################################################
```
######函數(shù)
```
函數(shù) {cmd;cmd;cmd}匿名函數(shù)卤妒;沒(méi)有函數(shù)名
#bash --version
查看bash 版本
定義函數(shù)
函數(shù)有兩個(gè)部分組成;函數(shù)名和函數(shù)體
語(yǔ)法一字币;function f_name{
...函數(shù)體...
}
語(yǔ)法二:function f_name (){
...函數(shù)體...
}
語(yǔ)法三则披;f_name (){
...函數(shù)體...
}
----定義函數(shù) 大括號(hào)里要隔開(kāi)一個(gè)空格輸入----
declare -f 顯示函數(shù)
declare -f func2 也可以指定某個(gè)函數(shù)
declare -x 顯示所有環(huán)境變量
unset func2 和取消變量一樣
```
#grep '^.*()' /etc/init.d/functions/