Shell編程一

Shell編程

shell腳本介紹

shell是一種腳本語言。它既有本身的語法規制,可使用邏輯判斷、循環等語法,也能夠自定義函數。
shell是系統命令的集合,而且經過主機的語法能夠對命令進行判斷等。
shell腳本能夠實現自動化運維,它能大大增長咱們的運維效率。

shell腳本結構

shell腳本結構
	開頭須要加#!/bin/bash #!/bin/bash是告訴系統經過/bin/bash解析器來解析操做腳本的
	以#開頭的行做爲解釋說明
	腳本的名字以.sh結尾,用於區分這是一個shell腳本

執行方法html

[root[@localhost](https://my.oschina.net/u/570656) shell]# vim hello.sh
[root[@localhost](https://my.oschina.net/u/570656) shell]# cat !$
cat hello.sh
#!/bin/bash
echo 'hello world'
echo 'How are you?'

執行方法有兩種:
chmod +x 1.sh; ./1.sh
sh 1.sh

[root[@localhost](https://my.oschina.net/u/570656) shell]# sh hello.sh 
hello world
How are you?
[root[@localhost](https://my.oschina.net/u/570656) shell]# chmod +x hello.sh 
[root[@localhost](https://my.oschina.net/u/570656) shell]# ./hello.sh 
hello world
How are you?
[root@localhost shell]#

查看腳本執行過程mysql

[root@localhost shell]# sh -x hello.sh 
+ echo 'hello world'
hello world
+ echo 'How are you?'
How are you?
[root@localhost shell]#

查看腳本是否語法錯誤sql

[root@localhost shell]# sh -n hello.sh 
[root@localhost shell]# echo 'for' >> hello.sh 
[root@localhost shell]# sh -n hello.sh 
hello.sh:行5: 未預期的符號 `newline' 附近有語法錯誤
hello.sh:行5: `for'
[root@localhost shell]# 
這裏可見 原先的腳本是無誤的,在我加入了一個 for後,提示報錯,緣由是for循環沒有寫完整

date命令方法

[root@localhost shell]# date
2018年 07月 23日 星期一 23:56:39 CST
[root@localhost shell]# 
date 顯示當前系統時間 在shell腳本中才用於標誌時間

命令				含義
date +%Y	  	  年(4位)
date +%y		  年(2位)
date +%m		  月
date +%d		  日
date +%D		  以 月/日/年 格式顯示日期
date +%F		  以 年-月-日 格式顯示日期
date +%H		  小時
date +%M		  分鐘
date +%S		  秒
date +%T		  以時:分:秒 格式顯示時間 等於 date +%H:%M:%S
date +%s		  時間戳 表示距離1970年1月1日到現的秒數
date +%w		  這個星期的第幾周
date +%W		  今年的第幾周
date +%h		  月份縮寫 等於 date +%b
date -d 「-1 day」	一天前
date -d 「+1 day」	一天後
date -d 「-1 year」	一年前
date -d 「-1 month」	一個月前
date -d 「-1 hour」	一小時前
date -d 「-1 min」	一分鐘前

只顯示日期shell

[root@localhost shell]# date +%Y%m%d
20180723
[root@localhost shell]# 
[root@localhost shell]# date +%D
07/23/18
[root@localhost shell]# date +%F
2018-07-23
[root@localhost shell]#

顯示時間編程

[root@localhost shell]# date +%T
00:00:04
[root@localhost shell]#

顯示時間戳vim

[root@localhost shell]# date +%s
1532361632
[root@localhost shell]#

用時間戳來顯示日期bash

[root@localhost shell]# date -d @1532361532
2018年 07月 23日 星期一 23:58:52 CST
[root@localhost shell]# date -d @1532361632
2018年 07月 24日 星期二 00:00:32 CST
[root@localhost shell]#

指定時間來顯示對應的時間戳less

[root@localhost shell]# date +%s -d'2018-07-29 08:59:00'
1532825940
[root@localhost shell]#

顯示前一天的時間運維

[root@localhost shell]# date -d '-1 day' +%F
2018-07-23
[root@localhost shell]# date -d '+1 day' +%F
2018-07-25
[root@localhost shell]# 
後一天的時間便是 +1

顯示一個小時前的時間dom

[root@localhost shell]# date -d '-1 hour' +%T
23:05:31
[root@localhost shell]# date -d '+1 hour' +%T
01:05:36
[root@localhost shell]# 
一個小時後的時間便是 +1

shell腳本中的變量

當腳本中使用某個字符串較頻繁而且字符串長度很長時就應該使用變量代替,既能夠更加工做效率,也能夠方便更改
	使用條件語句時,常使用變量 if [ $a -gt 1 ]; then … ; fi
	引用某個命令的結果時,用變量替代 n=`wc -l 1.txt`
	寫和用戶交互的腳本時,變量也是必不可少的 read -p 「Input a number: 「 n; echo $n 若是沒寫這個n,能夠直接使用$REPLY
	內置變量 $0, $1, $2… $0表示腳本自己文件名,$1表示第一個參數,$2 第二個 …. $#表示參數個數
	數學運算a=1;b=2; c=$(($a+$b))或者$[$a+$b]

Shell腳本的邏輯判斷

語法格式

格式1:if 條件;then 語句; fi
格式2:if 條件; then 語句; else 語句; fi
格式3:if 條件1; then 語句1;elif 條件2; then 語句2; else 語句3; fi

格式1演示:

腳本形式

[root@localhost shell]# vim if_1.sh
[root@localhost shell]# sh !$
sh if_1.sh
YES
[root@localhost shell]# cat !$
cat if_1.sh
#!/bin/bash
a=5
if [ $a > 4 ]
then
	echo 'YES'
fi
[root@localhost shell]#

命令行形式

[root@localhost shell]# a=5
[root@localhost shell]# if [ $a > 3 ];then echo 'yes';fi
yes
[root@localhost shell]# 
不推薦,建議使用腳本方式

或者

[root@localhost shell]# a=5
[root@localhost shell]# if [ $a > 3 ]
> then echo 'yes'
> fi
yes
[root@localhost shell]# 
不推薦,建議使用腳本方式

格式2演示:

[root@localhost shell]# vim if_2.sh
[root@localhost shell]# sh if_2.sh 
No
[root@localhost shell]# cat !$
cat if_2.sh
#!/bin/bash
a=1
b=2
if [ $a -gt $b ]
then
	echo 'Yes'
else
	echo 'No'
fi
[root@localhost shell]#

格式3演示:

意思爲符合條件1則執行語句1,不符合則再判斷是否符合條件2,都不符合則執行語句3

[root@localhost shell]# vim if_3.sh
[root@localhost shell]# sh if_3.sh 
the num < 3
[root@localhost shell]# cat !$
cat if_3.sh
#!/bin/bash
a=2
if [ $a -eq 3 ]
then
        echo "the num = 3"
elif [ $a -gt 3 ]
then
        echo "the num > 3"
else
        echo "the num < 3"
fi
[root@localhost shell]# 

參數		數學符號		含義
-gt			>			大於(greater than)
-lt			<			小於 (less than)
-eq			==			等於(equal)
-ge			>=			大於等於
-le			<=			小於等於
-ne			!=			不等於

可使用 && || 結合多個條件
if [ $a -gt 5 ] && [ $a -lt 10 ] 	&&表示而且
if [ $b -gt 5 ] || [ $b -lt 3 ] 	||表示或者

文件目錄屬性判斷

參數					含義
[ -f file ]			判斷是不是普通文件,且存在
[ -d file ]			判斷是不是目錄,且存在
[ -e file ]			判斷文件或目錄是否存在
[ -r file ]			以當前用戶來判斷文件是否可讀
[ -w file ]			以當前用戶來判斷文件是否可寫
[ -x file ]			以當前用戶來判斷文件是否可執行

判斷是否存在

[root@localhost shell]# vim file1.sh
[root@localhost shell]# cat file1.sh 
#!/bin/bash
f="/tmp/test"
if [ -f $f ]
then
	echo '$f exist'
else
	touch $f
fi
[root@localhost shell]# sh file1.sh 
[root@localhost shell]# sh file1.sh 
$f exist
[root@localhost shell]# 
第一次執行時不存在,因此走第二個條件,建立文件
第二次執行時已存在,因此走一個條件,提示已存在

判斷是否可讀

[root@localhost shell]# vim file2.sh
[root@localhost shell]# sh file2.sh 
/tmp/test readable
[root@localhost shell]# cat !$
cat file2.sh
#!/bin/bash
f="/tmp/test"
if [ -r $f ]
then
        echo "$f readable"
fi
[root@localhost shell]#

if特殊用法

參數				含義
[ -z 「$a」 ]			表示當變量a的值爲空
[ -n 「$a」 ]			表示當變量a的值不爲空
[ ! -n 「$a」 ]	    表示當變量a的值爲空   ! 取反
中括號中不能使用<,>,==,!=,>=,<=這樣的符號

判斷變量是否不爲空

[root@localhost shell]# vim file3.sh
[root@localhost shell]# sh file3.sh 
ok
[root@localhost shell]# cat file3.sh 
#!/bin/bash
f=/tmp/test
if [ -n "$f" ]
then
        echo ok
fi
[root@localhost shell]#

使用命令來作判斷條件

[root@localhost shell]# if grep -wq 'root' /etc/passwd;then echo root exist;fi
root exist
[root@localhost shell]# 
grep -w 'root'只搜索root此單詞 -q 不在屏幕打印出來

case判斷

語法格式

case  變量名 in 
                     value1)
                          command
                          ;;
                     value2)
                          command
                          ;;
                      *)
                        commond
                            ;;
                      esac

在條件中使用 | 表示 或 的意思

2|3) 
    command
    ;;

判斷成績腳本

[root@localhost shell]# cat case1.sh 
#!/bin/bash
read -p "Please input number: " n
#讓用戶輸入數字
if [ -z "$n" ]
then
        echo "Please input number: "
        exit 1
fi
#判斷用戶是否沒填數字
n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z "$n1" ]
then
        echo "Please input number: "
        exit 1
fi
#判斷用戶是否填純數字
if [ $n -lt 60 ]
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
#給分數賦值給tag
case $tag in
        1)
        echo "rubbish"
        ;;
        2)
        echo "just so so"
        ;;
        3)
        echo "good"
        ;;
        4)
        echo "perfect"
        ;;
        *)
        echo "The number range is 0-100"
        ;;
esac
[root@localhost shell]# 

[root@localhost shell]# vim case1.sh
[root@localhost shell]# sh case1.sh 
Please input number: 50
rubbish
[root@localhost shell]# sh case1.sh 
Please input number: 70
just so so
[root@localhost shell]# sh case1.sh 
Please input number: 90
perfect
[root@localhost shell]# sh case1.sh 
Please input number: 88
good
[root@localhost shell]# sh case1.sh 
Please input number: 988
The number range is 0-100
[root@localhost shell]#

for循環

語法格式:

for 變量名 in 條件; do ...;done

計算0到100的總和

[root@localhost shell]# vim for1.sh
[root@localhost shell]# sh for1.sh 
5050
[root@localhost shell]# cat !$
cat for1.sh
#!/bin/bash
sum=0
for i in `seq 1 100`
do
        sum=$[$i+$sum]
done
echo $sum
[root@localhost shell]#

遍歷/data/目錄並把目錄列出來

[root@localhost shell]# vim for2.sh
[root@localhost shell]# ls /data/
mysql
[root@localhost shell]# sh for2.sh 
auto.cnf     ib_logfile1		localhost-relay-bin.000005  master.info		relay-log.info
ibdata1      lan			localhost-relay-bin.000006  mysql		test
ib_logfile0  localhost.localdomain.err	localhost-relay-bin.index   performance_schema	zrlog
[root@localhost shell]# cat !$
cat for2.sh
#!/bin/bash
cd /data
for i in `ls /data`
do
        [ -d $i ] && ls $i
done
[root@localhost shell]#

while循環

語法格式

while 條件; do … ; done

提示讓用戶只能輸入數字

[root@localhost shell]# vim while1.sh
[root@localhost shell]# sh while1.sh 
Please input a number: 1as
Please input number!
Please input a number: we
Please input number!
Please input a number: 
Please input sth!
Please input a number: 123
123
[root@localhost shell]# cat !$
cat while1.sh
#!/bin/bash
while :
do
        read -p "Please input a number: " n
        if [ -z "$n" ]
        then
        echo "Please input sth!"
        continue
        fi
##判斷用戶是否直接回車
        n1=`echo $n|sed 's/[0-9]//g'`  
        # echo $n|sed 's/[0-9]//g 把數字給過濾掉來判斷是否有其餘字符
        if [ -n "$n1" ]
        then
        echo "Please input number!"
        continue
        fi
#判斷用戶是否輸入純數字
echo $n
break
done
[root@localhost shell]#

break && continue && exit

break

[root@localhost shell]# vim break.sh
[root@localhost shell]# sh break.sh 
1
1
2
2
3
666
[root@localhost shell]# cat !$
cat break.sh
#!/bin/bash
for i in `seq 1 5`
do
        echo $i
        if [ $i -eq 3 ]
        then
        break
        fi
        echo $i
done
echo 666
[root@localhost shell]# 
$i 在 數字 1-5 內循環,一旦 $i 等於 3 ,那麼就跳出 循環

continue

[root@localhost shell]# vim continue.sh
[root@localhost shell]# sh continue.sh 
1
1
2
2
3
4
4
5
5
666
[root@localhost shell]# cat !$
cat continue.sh
#!/bin/bash
for i in `seq 1 5`
do
        echo $i
        if [ $i -eq 3 ]
        then
        continue
        fi
        echo $i
done
echo 666
[root@localhost shell]# 
忽略當次循環,進行下一次循環。 因此第二次打印 數字3 沒有打印,直接跳出了

exit

[root@localhost shell]# vim exit.sh
[root@localhost shell]# sh exit.sh 
1
1
2
2
3
[root@localhost shell]# cat !$
cat exit.sh
#!/bin/bash
for i in `seq 1 5`
do
        echo $i
        if [ $i -eq 3 ]
        then
        exit 1
        fi
        echo $i
done
echo 666
[root@localhost shell]# 
exit退出整個腳本,不在進行循環,

[root@localhost shell]# echo $?
1
[root@localhost shell]# 
退出腳本後,進行echo $? 返回值是1

小結

break,continue都是在for while循環中使用的
break出現時候會跳出本次循環 直接忽略掉了break後面的代碼
continue出現時候也會忽略掉了continue後面的代碼並從新再來執行循環
exit直接跳出腳本 通常exit 後面會跟一個數字 給用戶返回該值

擴展

select用法 http://www.apelearn.com/bbs/thread-7950-1-1.html
相關文章
相關標籤/搜索