天天看點

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令

SHELL簡介:

  • 什麼是shell?

shell是linux的一外殼,包在linux核心的外面,為使用者和核心的互動提供了一個接口,當使用者下達指令給作業系統的時候,實際上是把指令告訴shell,經過shell的解釋,處理後讓核心做出相應的動作,系統的回應和輸出的資訊也由shell處理,然後顯示在使用者的螢幕上。

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令
  • 什麼是shell腳本?

簡單來說,當指令或者程式不在指令行執行,而是通過一個程式檔案來執行,這個程式就被稱為shell腳本,也就是在shell腳本裡設定了多條指令,語句,循環控制,然後将這些指令一次性執行完畢,這種通過檔案執行指令的方式稱為非互動式。

  • 為什麼使用shell腳本?

  1. 适合處理作業系統的底層業務,有衆多系統指令為其做支撐(還有文本處理三兄弟 grep,sed,awk)
  2. 适合處理純文字檔案,linux中許多服務配置檔案,啟動腳本,都是純文字(httpd,nfs,nginx,lvs)
  3. linux系統腳本用shell開發更簡單
  • 如何檢視系統預設shell?

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令
  • 腳本的開發規範

1.第一行:#!/bin/bash

指定解釋器:由哪個程式來執行腳本内容

#!:幻數

2.腳本資訊

#!/bin/bash

#Date:2018-12-25

#Author: westos-wsp

#Connect: [email protected]

#Desc: This scripts is for...

#Version:1.0

3.腳本名最好以 .sh 結尾

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令
  • 腳本執行方法

sh script.sh | bash script.sh     沒有執行權限時

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令

path/script.sh | ./script.sh         絕對路徑,目前目錄下

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令

source script.sh | . script.sh     這種方式會使用source或.号來讀如指定shell檔案,并會把其他shell中的變量值或函數傳回給父shell繼續使用,前兩種方式,在執行腳本的時候,會預設打開一個新的shell,而新shell的變量值和函數不會傳回給父shell

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令

練習:寫一個安裝,啟動并開機自啟的apache腳本。

SHELL變量:

定義變量

  • 環境變量
初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令
  • 普通變量

變量名=hello         不能有空格

變量名=‘hello’       對單引号内内容不解釋,可以有空格(如果需要原樣輸出就用單引号)

變量名=“hello”       對雙引号内内容解釋,可以有空格

[[email protected] ~]# a=hello
[[email protected] ~]# echo $a
hello
[[email protected] ~]# b='hello'
[[email protected] ~]# echo $b
hello
[[email protected] ~]# c="hello"
[[email protected] ~]# echo $c
hello
[[email protected] ~]# a=westos-$a
[[email protected] ~]# echo $a
westos-hello
[[email protected] ~]# b='westos-$a'
[[email protected] ~]# echo $b
westos-$a
[[email protected] ~]# c="westos-$a"
[[email protected] ~]# echo $c
westos-westos-hello
[[email protected] ~]# a=westos hello
bash: hello: command not found...
[[email protected] ~]# a="westos hello"
[[email protected] ~]# echo $a
westos hello
           

注意:建議沒有特别要求時,字元串都加雙引号,需要原樣輸出就加單引号

  • 将指令的結果指派給變量:
[[email protected] mnt]# CMD=`ls -l`
[[email protected] mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh

[[email protected] mnt]# CMD=$(ls -l)
[[email protected] mnt]# echo $CMD
total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh
           
  • 特殊變量

$0:擷取shell腳本檔案名,如果執行時包含路徑,則輸出腳本路徑

$n(>0):擷取腳本的第n個參數

$#:擷取腳本參數的總個數

$*:擷取所有參數

[email protected]:擷取所有參數

$?:

表示上條指令執行結果的傳回值,0表示執行成功,非0表示執行失敗

$$:擷取目前shell程序号

$0:
[[email protected] mnt]# cat westos.sh
#!/bin/bash
echo $0
[[email protected] mnt]# sh westos.sh
westos.sh
[[email protected] mnt]# /mnt/westos.sh
/mnt/westos.
           
$n(n>0):
[[email protected] mnt]# cat westos.sh
#!/bin/bash
echo $1 $2

[[email protected] mnt]# sh westos.sh hello westos
hello westos
[[email protected] mnt]# sh westos.sh hello redhat
hello redhat

[[email protected] mnt]# echo \${1..10} > westos.sh
[[email protected] mnt]# cat westos.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[[email protected] mnt]# sh westos.sh  {1..10}
1 2 3 4 5 6 7 8 9 10
[[email protected] mnt]# sh westos.sh  {a..z}
a b c d e f g h i a0
[[email protected] mnt]# cat 01.sh 
#!/bin/bash
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
echo $#
# ${10}
[[email protected] mnt]# sh westos.sh  {a..z}
a b c d e f g h i j
           
$#:
[[email protected] mnt]# cat westos.sh
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[[email protected] mnt]# sh westos.sh {1..100}
1 2 3 4 5 6 7 8 9
100
           
$?:
表示上條指令執行結果的傳回值
0表示執行成功
非0表示執行失敗
           

read用法:

[[email protected] mnt]# read str
westos hello
[[email protected] mnt]# echo $str
westos hello
[[email protected] mnt]# read -p "請輸入一個整數:" i
請輸入一個整數:10
           

打包日志:

[[email protected] mnt]# tar zcf log_$(date +%F).tar.gz /var/log/
tar: Removing leading `/' from member names
[[email protected] mnt]# ls
log_2018-12-22.tar.gz  test.sh  westos.sh

           

變量的數值計算

  • expr指令(注意 * 直接用不能識别,需要進行轉譯,才可以執行,即在*前加\)
[[email protected] mnt]# a=123
[[email protected] mnt]# expr $a + 10
133
[[email protected] mnt]# expr $a - 10
113
[[email protected] mnt]# expr $a * 10
expr: syntax error
[[email protected] mnt]# expr $a \* 10
1230
[[email protected] mnt]# expr $a / 10
12
[[email protected] mnt]# expr $a % 10
3
           
  • $[ ]和$(())表達式
[[email protected] mnt]# echo $[a+10]
20
[[email protected] mnt]# echo $[a-10]
0
[[email protected] mnt]# echo $[a*10]
100
[[email protected] mnt]# echo $[a/10]
1
[[email protected] mnt]# echo $[a%10]
0
[[email protected] mnt]# echo $((a+10))
20
[[email protected] mnt]# echo $((a-10))
0
           
  • let指令(let指令在執行後會儲存新的值)
[[email protected] mnt]# let a+=10
[[email protected] mnt]# echo $a
20
[[email protected] mnt]# let a-=10
[[email protected] mnt]# echo $a
10
[[email protected] mnt]# let a*=10
[[email protected] mnt]# echo $a
100
[[email protected] mnt]# let a/=10
[[email protected] mnt]# echo $a
10
[[email protected] mnt]# let a%=10
[[email protected] mnt]# echo $a
0
           
  • 小數運算工具bc
[[email protected] mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[[email protected] mnt]# echo "scale=2;1.23*4.56" | bc
5.60
[[email protected] mnt]# echo 1.2+3.4 | bc
4.6
[[email protected] mnt]# echo 1.23+4.56 | bc
5.79
           

練習:計算兩個數的加減乘除

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令

文本處理

  • grep,egrep

grep 指令是一種強大的文本搜尋工具,根據使用者指定的“模式”對目标文本進行比對檢查,列印比對到的行

由正規表達式或者字元及基本文本字元所編寫的過濾條件全面搜尋研究正規表達式并顯示出來

正規表達式:

由一類特殊字元及文本字元所編寫的模式,其中有些字元不表示字元的字面意思,而表示控制或通配的功能^

^:比對開頭

$:比對結尾

.:比對任意字元

[ ]:比對括号中的任意字元

[^]:比對不在括号中的任意字元

grep

    -i     ##忽略字母大小寫

    -v    ##條件取反

    -c    ##統計比對行數

    -q    ##靜默,無任何輸出

    -n    ##顯示比對結果所在的行号

-q:
[[email protected] mnt]# grep '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
172.25.254.250 content.example.com
YES
[[email protected] mnt]# grep -q '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
YES
-c:
[[email protected] mnt]# egrep -c '/sbin/nologin' /etc/passwd
35
           
  • 基本元字元:^ $
[[email protected] mnt]#egrep -m10 '/sbin/nologin' /etc/passwd   ##比對前10行
[[email protected] mnt]# egrep -m10 'sbin$'   wsp                          ##比對前10行中以sbin結尾的行
root sbin
root sbin sbin
[[email protected] mnt]# cat wsp
root sbin
root sbin root
root sbin sbin
           
  • 基本元字元:.    ##過濾非空行
[[email protected] mnt]# egrep '.' wsp
root sbin
root sbin root
root sbin sbin
root
awd
awd
awd


[[email protected] mnt]# egrep -v '.' wsp    ##過濾空行
[[email protected] mnt]# egrep  '^$' wsp    ##過濾空行
           
  • 基本元字元: + ? *
[[email protected] ~]# egrep 'f+' 1.sh     ##輸出包括f,ff,fff....,即至少出現一次
colorful,color
colorfulful?
stuf
stuff
stufff
stuffff
stufawd
we adw dfg awda
wea web wef

[[email protected] ~]# egrep 'color(ful)?' 1.sh     ##末尾的ful最多出現一次,也可以沒有
color color color
colorful,color
color color.
colorfulful?
           
  • 元字元:{ }
[[email protected] ~]# egrep '(we){3}' 1.sh      ##比對出現字元"we"3次的行
rere wewewe
westos wewewewe Shell
[[email protected] ~]# egrep '(we){2,4}' 1.sh   ##比對出現字元"we"2~4次的行
xcvb wewe asdawd
rere wewewe
westos wewewewe Shell
[[email protected] ~]#
[[email protected] ~]# egrep '(we){3,}' 1.sh     ##比對出現字元"we"3次及3次以上的行
rere wewewe
westos wewewewe Shell
[[email protected] ~]# egrep '(we)[ab]' 1.sh    ##比對字元"we"後有a或者b的行
weawe IPADDR
wea web wef
[[email protected] ~]# egrep '[A-Z]' 1.sh         ##比對含有大寫字母的行
weawe IPADDR
westos wewewewe Shell
           
  • cut指令
cut -d    ##指定分隔符
cut -d : -f 1-3 /etc/passwd    ##指定分隔符為:,顯示第1到3列
cut -c 1,4 /etc/passwd        ##顯示第一和第四個字元
           

練習:擷取主機IP

[[email protected] ~]# ifconfig eth0 | grep "inet " | awk '{print $2}'

172.25.254.100

[[email protected] ~]# ifconfig eth0 | grep "inet " | cut -d " " -f 10

172.25.254.100

練習:檢測網絡

ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down

sort指令:排序

sort

    -n    ##純數字排序

    -r     ##倒序

    -u     ##去掉重複數字

    -o     ##輸出到指定檔案中

    -t      ##指定分隔符

    -k     ##指定要排序的列

[[email protected] ~]# sort westos         ##預設第1列排序

1
12
123
2
3
32
5
51
6
7

[[email protected] ~]# sort -n westos    ##數字大小排序

1
2
3
5
6
7
12
32
51
123

[[email protected] ~]# sort -u westos    ##去除重複數字
1
12
123
2
3
32
5
51
6
7

[[email protected] ~]# sort -t : -k 2 westos     ##指定分隔符為:,第二列預設排序
2:0
12:10
2:12
3:2
51:20
5:21
123:22
32:31
5:4
6:4
1:5
51:55
123:66
7:79

[[email protected] ~]# sort -nt : -k 2 westos   ##指定分隔符為:,第二列按數字大小排序
2:0
3:2
5:4
6:4
1:5
12:10
2:12
51:20
5:21
123:22
32:31
51:55
123:66
7:79


[[email protected] ~]# sort -nt : -k 2 westos -o /mnt/file      ##将排序後的儲存到/mnt/file
           

uniq指令:對重複字元處理

uniq

    -u    ##顯示唯一的行

    -d    ##顯示重複的行

    -c    ##每行顯示一次并統計重複次數

[[email protected] ~]# sort -n westos | uniq -c
      1 0
      1 1
      2 2
      1 4
      1 6
      1 9
      2 10
      1 20
      1 22
      2 31
      1 55


[[email protected] ~]# sort -n westos | uniq -d
2
10
31


[[email protected] ~]# sort -n westos | uniq -u
0
1
4
6
9
20
22
55

           

練習:将/tmp目錄中的檔案取出最大的

[[email protected] ~]$ ls -Sl /tmp/ | head -2 | cut -d " " -f 9

條件判斷--test指令

test "$a" == "$b" 等同于 [ "$a" == "$b" ]

[ "$a" = "$b" ]        ##等于

[ "$a" != "$b" ]     ##不等于

[ "$a" -eq "$b" ]    ##等于

[ "$a" -ne "$b" ]    ##不等于

[ "$a" -le "$b" ]    ##小于等于

[ "$a" -ge "$b" ]    ##大于等于

[ "$a" -gt "$b" ]    ##大于

[ "$a" -lt "$b" ]    ##小于

[ "$a" -ne "$b" -a "$a" -gt "$b" ]    ##-a必須條件都滿足

[ "$a" -ne "$b" -o"$a" -gt "$b" ]    ##-a條件至少滿足一個

[ -z "$a" ]        ##是否為空

[ -e "file" ]        ##是否存在

[ -f "file" ]        ##普通檔案

[ -b "file" ]        ##塊裝置

[ -S "file" ]        ##套接字

[ -c "file" ]        ##字元裝置

[ -L "file" ]        ##軟連結

練習

判斷輸入的數字是否在10以内

1.輸入是否為空

2.是否在10以内

3.1<$a<10 --> yes

4.$a<1 $a>10 --> no

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令

判斷檔案類型:

初識SHELL腳本SHELL簡介:SHELL變量:變量的數值計算文本處理sort指令:排序uniq指令:對重複字元處理條件判斷--test指令

繼續閱讀