shell:從入門到掌握

一、什麼是shell編程

1、Shell命令解釋器

+ Bash        # Redhat
+ dash        # Ubuntu 
+ csh tcsh    # unix

在這裏插入圖片描述
2、 開發語言中程序代碼的分類

  • 編譯型: 開發書寫的代碼 無法直接運行 需要編譯(代碼––>命令) C語言
  • 解釋型: 書寫完成代碼,通過命令執行 (shell腳本) shell 書寫腳本 然後可以直接運行

3、書寫與執行腳本習慣

#指定腳本默認使用的命令解釋器 第1行(例如)
#!/bin/bash
#!/usr/bin/python

4、 如何執行編寫完的腳本

首先:簡單編寫一個腳本
[root@shell /server/scripts] vim test.sh
#!/bin/bash
echo test
方法1:直接運行,需要加上x權限
[root@shell /server/scripts]# ll test.sh 
-rw-r--r-- 1 root root 76 Aug 5 18:22 test.sh 
[root@shell /server/scripts]# chmod +x test.sh 
[root@shell /server/scripts]# ./test.sh 
[root@shell /server/scripts]# /server/scripts/test.sh
方法2:sh運行
[root@shell /server/scripts]# bash test.sh 
[root@shell /server/scripts]# ll `which sh bash` 
-rwxr-xr-x. 1 root root 964544 Apr 11 2018 /usr/bin/bash 
lrwxrwxrwx. 1 root root 4 Apr 29 19:16 /usr/bin/sh -> bash
方法3 source. 替你在當前環境中,執行1次腳本
[root@shell /server/scripts]# . oldboy.sh 
test
[root@shell /server/scripts]# source oldboy.sh 
test
方法4:不推薦,重定向給sh
[root@shell /server/scripts]# sh < test.sh
test

二、Shell編程基礎知識(習慣)

  • 書寫腳本第1行命令要指定解釋器
  • 不要使用中文和儘量不要使用拼音(反正我做不到)
  • 不要使用中文符號
  • 腳本書寫記得添加註釋信息,其確保其他人能看懂
  • 腳本文件名儘量不要包含服務名稱,腳本名稱進來跟業務有關

三、sh與source (. )區別

書寫一個簡單的例子
[root@shell /server/scripts]# cat test.sh 
#!/bin/bash 
echo $TEST
[root@shell /server/scripts]# TEST=666 
[root@shell /server/scripts]# sh show.sh 
[root@shell /server/scripts]# source test.sh 
666

爲什麼呢:請看圖
在這裏插入圖片描述

四、變量

一、什麼是變量
  • 變量本質:內存中的一塊區域,變量名,位置
  • 存放經常用到的內容
  • Linux下面的變量直接使用即可
  • 數據類型(變量在內存中 所佔多少空間 變量類型) 不同類型的變量存放不同類型的數據
  • 變量賦值(例子)
[root@shell /server/scripts]# test='test 666' 
[root@shell /server/scripts]# echo $test 
test 666
二、變量分類
  • 環境變量(全局變量)
  • 普通變量(局部變量)
  • 特殊變量

1、環境變量(全局變量 )

  • 系統定義
  • 大寫字母
  • 在系統中各個地方基本都是用
環境變量 含義
LANG 記錄系統字符集,語言zh_CN.UTF-8 en_US.UTF-8
PS1 命令行格式
PATH 命令路徑執行命令,系統會在PATH路徑中查找
UID 記錄用戶的UID信息
HOSTNAME 主機名
HISTCONTROL 控制history命令是否記錄以空格開頭的命令
HISTSIZE history命令記錄條數
HISTFILESIZE history文件記錄的最多條數
TMOUT 不進行操作,自動斷開時間

export創建或設置環境變量

[root@shell /server/scripts]# export TEST=666 
[root@shell /server/scripts]# cat test.sh 
#!/bin/bash 
echo $TEST
[root@shell /server/scripts]# sh TEST.sh 
666

2、普通變量(局部變量)
命名規則(習慣)

1、不能以數字開頭
2、不要佔用系統環境變量
3、駝峯:linuxTest=1 
4、下劃線:age_person = 1

3、特殊變量

1、特殊變量的作用

  • 提高書寫腳本 及腳本執行效率
  • 判斷服務狀態

2、 表示位置的特殊變量

符號 作用
$0 腳本執行錯誤,給出錯誤提示
$n 命令行中參數,傳遞給腳本,在腳本中使用
$# 腳本參數的個數
$* 取出所有的參數 加上雙引號:相當於是1個整體1個參數
$@ 取出所有的參數 加上雙引號:就是每個都是獨立
[root@shell /server/scripts]# vim good01.sh
#!/bin/bash
echo "number:$#"
echo "scname:$0"
echo "first :$1"
echo "second:$2"
echo "argume:$@"
echo "show parm list:$*"


[root@shell /server/scripts]# sh  good01.sh  1 2
number:2
scname:good1.sh
first :1
second:2
argume:1 2
show parm list:1 2


[root@shell /server/scripts]# sh -x good01.sh  1 2
+ echo number:2
number:2
+ echo scname:good1.sh
scname:good1.sh
+ echo 'first :1'
first :1
+ echo second:2
second:2
+ echo argume:1 2
argume:1 2
+ echo 'show parm list:1 2'
show parm list:1 2

3、 表示狀態變量

符號 作用
$$ 這個程式的PID(腳本運行的當前進程ID號)
$! 執行上一個背景指令的PID(後臺運行的最後一個進程的進程ID號)
$? 執行上一個指令的返回值 (顯示最後命令的退出狀態。0表示沒有錯誤,其他任何值表明有錯誤)
$_ 上1個命令的最後1個參數,與 ESC+ . 組合鍵類似
[root@shell /server/scripts]# vim  good02.sh 
#!/bin/bash
echo "show process id:$$"
echo "show precomm stat: $?"

[root@shell /server/scripts]# sh  good02.sh
#!/bin/bash
show process id:1077
show precomm stat: 0

[root@shell /server/scripts]# sh -x good02.sh
#!/bin/bash 
+ echo 'show process id:1077'
show process id:1076
+ echo 'show precomm stat: 0'
show precomm stat: 0

4、 變量子串

內容 含義
${parameter} 返回變量的內容
${#parameter} 返回變量$parameter的長度
${parameter:offset} 在$parameter中, 從位置offset之後開始提取子串
${parameter:offset:length} 在$parameter中, 從位置offset之後開始提取長度爲length的子串
${parameter#word} 從變量$parameter的開頭,刪除最短匹配word的子串
${parameter##word } 從變量$parameter的開頭,刪除最長匹配word的子串
${parameter%word} 從變量$parameter的結尾,刪除最短匹配word的子串
${parameter%%word} 從變量$parameter的結尾,刪除最長匹配word的子串
${parameter/pattern/string} 使用string來代替第一個匹配的pattern
${parameter//pattern/string} 使用string代替所有匹配的pattern
簡單用法舉例:
${變量}
${#變量} 統計變量中內容的字數(字符數)
[root@shell /server/scripts]# test=goodnb
[root@shell /server/scripts]# echo ${test} 
goodnb
[root@shell /server/scripts]# echo ${#test} 
6
使用環境:統計字符數方法
[root@shell /server/scripts]# test='www.thinkqiu.com'
傳統:wc|awk
1、wc效率低
[root@shell /server/scripts]# echo ${test}|wc -L 
16
2、awk一般用於配合awk其他用法使用 
[root@shell /server/scripts]# echo ${test}|awk '{print length()}' 
16
3、shell變量高效,快捷
[root@shell /server/scripts]# echo ${#test} 
16
簡單用法舉例:
${變量:起點}
${變量:起點:長度}
[root@shell /server/scripts]# test='goodnb123'
[root@shell /server/scripts]# echo ${test:4:5}
nb123
1、傳統的cut
[root@shell /server/scripts]# echo $test |cut -c5-9
think
2、shell截取
[root@shell /server/scripts]# echo ${test:4:5}
think
簡單用法舉例:
${變量#內容} 左邊開始(開頭)
${變量%內容} 右邊開始(結尾)
[root@shell /server/scripts]# test='goodnb123 nb'
[root@shell /server/scripts]# echo "${test#good}"
nb123 nb
[root@shell /server/scripts]# echo "${test%nb}"
goodnb123 
可能用到的示例:
[root@shell /server/scripts]# test='goodnb123 nb'
1、取o之後的字符,從左最小
[root@shell /server/scripts]# echo "${test#*o}"
odnb123 nb
2、取o之後的字符,從左最大
[root@shell /server/scripts]# echo "${test##*o}"
dnb123 nb
3、取o之前的字符,從右最小
[root@shell /server/scripts]# echo ${test%o*}
go
4、取o之前的字符,從右最大
[root@shell /server/scripts]# echo ${test%%o*}
g
5、取網卡文件路徑中某個文件名
[root@shell /server/scripts]# name=/etc/sysconfig/network 
[root@shell /server/scripts]# echo ${name##*/} 
network 
[root@shell /server/scripts]# echo ${name%/*} 
/etc/sysconfig
簡單用法舉例:
${變量/內容/要替換成的內容}
${變量//內容/要替換成的內容}
[root@shell /server/scripts]# test='goodnb123 nb'
[root@shell /server/scripts]# echo ${test/123/ znb}
goodnb znb nb
[root@shell /server/scripts]# echo ${test}
goodnb123 nb
[root@shell /server/scripts]# echo ${test//nb/ wnb}
good wnb123 wnb
[root@shell /server/scripts]# echo ${test//nb/}
good123

5、 變量擴展

給變量設置默認值
內容 含義
${parameter:-word} 如果parameter沒有被賦值或者其值爲空,那麼就以word作爲其值
${parameter:=word} 如果parameter沒有被賦值或者其值爲空,那麼就以word作爲其值,並且將word賦值給parameter
${parameter:?word} 如果parameter沒有被賦值或者其值爲空,那麼就把word作爲錯誤輸出.否則顯示parameter內容
${parameter:+word} 如果parameter沒有被賦值或者其值爲空,就什麼都不做.否則用word替換變量內容
實驗前確保test變量不存在
[root@vpn 2.5]# unset test
注意:如果test變量已經賦值,則輸出變量所擁有的值
${parameter:-word}簡單示例:
[root@shell /server/scripts]# echo ${test}
 
[root@shell /server/scripts]# echo ${test:-goodnb}
goodnb
[root@shell /server/scripts]# echo ${test}
 
實驗前確保test變量不存在
[root@shell /server/scripts]# unset test
${parameter:=word}簡單示例:
[root@shell /server/scripts]# echo ${test}

[root@shell /server/scripts]# echo ${test:=goodnb}
goodnb
[root@shell /server/scripts]# echo ${test}
goodnb
實驗前確保test變量不存在
[root@shell /server/scripts]# unset test
${parameter:+word}簡單示例:
注意:只改變賦值過的變量,如果賦值爲空或不存在改變量則不添加變量和賦值,另外改參數只改變輸出結果變量的實際賦值則不改變
[root@shell /server/scripts]# echo ${test}

[root@shell /server/scripts]# echo ${test+goodnb}

[root@shell /server/scripts]# echo ${test}

[root@shell /server/scripts]# test=good
[root@shell /server/scripts]# echo ${test}
good
[root@shell /server/scripts]# echo ${test+goodnb}
goodnb
[root@shell /server/scripts]# echo ${test}
good
${parameter:?word}簡單示例:
[root@shell /server/scripts]# echo ${test:=goodnb}
goodnb
[root@shell /server/scripts]# echo ${test}
goodnb
[root@shell /server/scripts]# echo ${test?NULL}
goodnb
[root@shell /server/scripts]# unset test
[root@shell /server/scripts]# echo ${test?NULL}
-bash: test: NULL

五、與變量有關的文件

文件(每次用戶登錄系統執行)
/etc/profile 存放全局環境變量
/etc/bashrc 存放全局別名
~/.bashrc 局部別名
~/.bash_profile 存放全局環境變量
/etc/profile.d/xxxx.sh 用戶登錄系統後,系統自動執行該目錄下以.sh結尾

六、運算符

算術運算符 意義(表示常用)
+、- 加法(或正號)、減法(或負號)
*、/、% 乘法、除法、取餘
^ 、** 冪運算
++、- - 增加及減少,可前置也可放在變量結尾,默認步長爲1
!、&&、|| 邏輯非(取反)、邏輯與(and)、邏輯或(or)
<、<=、>. >= 比較符號(小於、小於等於、大於、大於等於)
==、!=、= 比較符號(相等、不相等,對於字符串“=” 也可以表示相
~、|、 &、 按位取反、按位異或、按位與、按位或
=、+=、-=. *=、/=、%= 賦值運算符,例如a+=1 相當a=a+1, a-=1 相當a=a-1
i=i+1 i++ 計數 統計次數
sum=sum+xx、sum+=xxxx 累加 求和

運算方式

例子:(())
[root@shell ~]# a=1 
[root@shell ~]# b=2 
[root@shell ~]# echo $a+$b 
1+2 
[root@shell ~]# ((c=a+b)) 
[root@shell ~]# echo $c 
3
例子:let
[root@shell ~]# let c=a+b 
[root@shell ~]# let c=a+b+b+b 
[root@shell ~]# echo $c 
7
例子:expr
[root@shell ~]# expr 1 + 2 
3 
[root@shell ~]# expr 2 * 3 
expr: syntax error 
[root@shell ~]# expr 2 \* 3 
6
例子:bc
[root@shell ~]# echo 10/3 |bc -l 
3.33333333333333333333
例子:$[]
[root@shell ~]# echo $[1/3] 
0 
[root@shell ~]# echo $[10**3] 
1000
例子:awk
[root@shell ~]# awk 'BEGIN{print 1/3}' 
0.333333

七、條件測試語句

名稱 含義
-d directory 目錄是否存在
-f file 文件是否存在
-e exist 是否存在
-r read 文件是否存在 並且 是否有r權限
-w write 文件是否存在 並且 是否有w權限
-x execute 文件是否存在 並且 是否有x權限
-s size文件是否存在並且是否爲空大於0 成立
-L Symlink (symbolic link) 判斷文件是否存在並且是否爲軟連接

注意:&&邏輯與(and)、||邏輯或(or)

例子:
[root@shell ~]# test -f /etc/hosts && echo 1 || echo 0 
1 
[root@shell ~]# test -f /etc/ho && echo 1 || echo 0 
0
[root@m01 ~]# test -x /etc/rc.d/rc.local && echo 1 || echo 0 
1 
[root@m01 ~]# test -r /etc/shadow && echo 1 || echo 0 
1
系統腳本中的判斷案例: 
if [ ! -f /etc/sysconfig/network ]; then 
 exit 6 
fi 
[ -x /sbin/ip ] || exit 1 
if [ -x /sbin/ip ] 
then 
 exit 1 
fi

補充:詳解邏輯與,邏輯或

邏輯與:
滿足條件 執行某個命令
[ 條件 ] && 命令

滿足條件 執行多個命令 
[ 條件 ] && { 
cmd1 
cmd2 
}
邏輯與簡單演示:
注意:邏輯與滿足第一個條件則執行第二個命令,如果第一個不滿足則直接報錯
[root@shell ~]# echo 1 && echo 2
1
2
[root@shell ~]# echo 1 && ech 2
1
bash: ech: command not found
[root@shell ~]# ech 1 && ech0 2
bash: ech: command not found
邏輯或:
[ 條件 ] || 命令

[ 條件 ] || { 
cmd1 
cmd2 
}
邏輯與簡單演示:
注意:邏輯與是第一個條件滿足,則不執行第二個條件(不管第二個條件是否正確錯誤),如果第一個條件不成立,纔會執行第二個條件
[root@shell ~]# echo 1 || echo 2
1
[root@shell ~]# echo 1 || ech 2
1
[root@shell ~]# ech 1 || echo 2
bash: ech: command not found
2
名稱 含義
-n not zero 如果變量/字符串不是空的
-z zero 如果變量/字符串是空
“串1” = “串2” 判斷兩個字符串 是否相等 如果相等則真
“串1” != “串2” 判斷兩個字符串 是否相等 如果不相等則真
判斷字符串是否相等:
[root@shell ~]#  [ "test" = "good" ] && echo 等於 ||echo 不等於
不等於
[root@shell ~]#  [ "test" = "test" ] && echo 等於 ||echo 不等於
等於
判斷變量賦值是否爲空:
注意:在使用字符串,變量比較時候要使用雙引號,把字符串或變量引起來!
[root@shell ~]# unset test
[root@shell ~]# echo $test

[root@shell ~]# [ -z "$test" ] && echo 空 || echo 不空[root@shell ~]# [ -n "$test" ] && echo 不空 || echo 空[root@shell ~]# test=goodnb
[root@shell ~]# [ -z "$test" ] && echo 空 || echo 不空
不空
[root@shell ~]# [ -n "$test" ] && echo 不空 || echo 空
不空

八、比較大小

含義 [] 用法 或 test (()) 或 [[ ]]
等於 equal -eq ==
不等於 not equal -ne !=
大於 great than -gt >
大於等於 great equal -ge >=
小於 less than -lt <
小於等於 less equal -le <=
[root@shell ~]# [ 1 -eq 1 ] && echo 真 ||echo 假[root@shell ~]# [ 99 -gt 1 ] && echo 真 ||echo 假[root@shell ~]# [ 1 -gt -10 ] && echo 真 ||echo 假[root@shell ~]# [ 1.1 -gt -10 ] && echo 真 ||echo 假
-bash: [: 1.1: 期待整數表達式
假
[root@shell ~]# [ 1 -gt -10 ] && echo 真 ||echo 假[root@shell ~]# test 1 -gt -10   && echo 真 ||echo 假[root@shell ~]# [[ 2 > 10 ]] && echo 真 ||echo 假[root@shell ~]# (( 2 >= 10 )) && echo 真 ||echo 假[root@shell ~]# (( 2 <= 10 )) && echo 真 ||echo 假
注:這是一個簡單比較數字大小腳本,bug很多(別噴我)
[root@shell 2.5]# cat sb1.sh 
#!/bin/bash
. /etc/init.d/functions

#str1=$1
#str2=$2
#[ $# -ne 2 ] && echo "請查看幫助" && exit

#下面有read的講解
read -p '輸入:' str1
read -p '輸入:' str2

[[ "$str1" =~ ^[0-9]+$ ]]  || {
       	echo "請參數1輸入數字"
        exit 1
}
[[ "$str2" =~ ^[0-9]+$ ]]  || { 
	echo "請參數2輸入數字"
	exit 
}

if [ $str1 -eq $str2 ]
then
	action "$str1 = $str2   相等" /bin/true
fi

if [ $str1 -gt $str2 ]
then
	action "$str1 > $str2   大於" /bin/true
fi

if [ $str1 -lt $str2 ]
then
	action "$str1 < $str2   小於" /bin/true
fi
參數 含義
-a and 並且
-o or 或者
! ! 取反
簡單用法:
[root@shell ~]# [ -d /etc  -a -f /etc/hosts ] && echo 真 || echo 假
真
[root@shell ~]# [ -d /et  -a -f /etc/hosts ] && echo  真 || echo 假
假
[root@shell ~]# [ -d /et  -o -f /etc/hosts ] && echo 真 || echo 假
真
[root@shell ~]# [ ! -d /et ] && echo 真 || echo 假
真
[root@shell ~]# num=4
[root@shell ~]# [ $num -ge 10 -a $num -le 100 ] && echo 1 || echo 0
1
[root@shell ~]# [[ "12345" =~ [0-9] ]] && echo 1 || echo 0
1
[root@shell ~]# [[ "oldboy" =~ [0-9] ]] && echo 1 || echo 0
0
[root@shell ~]# [[ "oldboy123" =~ [0-9] ]] && echo 1 || echo 0
1
[root@shell ~]# [[ "oldboy123" =~ ^[0-9]+$ ]] && echo 1 || echo 0
0

在這裏插入圖片描述

九、變量賦值方式

賦值方式 案例
直接賦值 test=goodnb、name=`hostname`
參數傳遞(腳本傳參) test=$1 sh test.sh 1
通過read賦值 交互式賦值 read -p ‘input password:’ pass(也可以看上面比較大小的腳本)
1、單一賦值
[root@shell ~]# read -p '輸入:' str1
輸入:goodnb
[root@shell ~]# echo $str1
goodnb
2、多個賦值
[root@shell ~]# read -p '輸入:' str1 str2
輸入:goodnb01 goodnb02
[root@shell ~]# echo $str1
goodnb01
[root@shell ~]# echo $str2
goodnb02

十、 if條件語句

  • 單分支
  • 雙分支
格式:單分支
if [ 條件 ]
then
命令
fi
格式:雙分支
if [ 條件 ]
then
命令
else     
命令    
fi

注意:elseif 和 else的區別

if (條件句)+ 執行語句1
else + 執行語句2
如果條件句成立,則執行語句1。如果不成立,則執行語句2;

if (條件句1)+ 執行語句1
elseif (條件句2)+ 執行語句2
else + 執行語句n
end
如果條件句1成立,則執行語句1,如果條件句1不成立,然後條件句2成立,則執行語句2,如果所有條件都不成立,則執行執行語句n

十一、case語句

分支結構 條件選擇語句
應用場景: 服務管理腳本(start|stop|restart) 菜單功能

格式:
case "變量" in 
 值1) 
 指令1... 
 ;; 
 值2) 
 指令2... 
 ;; 
 *) 
 指令3... 
esac
case "你有什麼條件" in 
 有錢) 
 白富美結婚
 ;; 
 有權) 
 白富美結婚
 ;; 
 有爹) 
 白富美結婚
 ;; 
 有才有能力有潛力) 
 先談個對象看看
 ;; 
 *) 
 洗洗睡吧
esac

十二、添加一些輸出的顏色

[root@shell ~]#  echo -e "\E[1;31m紅色字goodnb\E[0m"
紅色字goodnb
例子:
[root@shell ~]#  vim good02.sh
#!/bin/bash 
red='\e[1;31m' 
green='\e[1;32m' 
blue='\e[1;34m' 
end='\e[0m' 
case "$1" in 
 red) 
 echo -e "${red}$1${end}" 
 ;; 
 green) 
 echo -e "${green}$1${end}" 
 ;; 
 blue) 
 echo -e "${blue}$1${end}" 
 ;; 
 *) 
 echo "Usage: $0 {red|green|blue}" 
esac

十三、函數

給一段代碼設置1個名稱 ,名稱:函數名字讓腳本整齊,更規範規範

格式:
函數名() {
函數體(命令)
函數體(命令)
}
注意:最後記得調用函數
簡單示例:
[root@shell ~]# sh test01.sh 
goodnb01
goodnb02
goodnb03
[root@shell ~]# cat test01.sh 
#!/bin/bash
#author:oldboy
goodnb() {
	echo goodnb01
	echo goodnb02
	echo goodnb03
}

goodnb
函數傳參簡單示例:
[root@shell ~]# sh test01.sh linux
linux_goodnb01
linux_goodnb02
linux_goodnb03
[root@shell ~]# cat test01.sh 
#!/bin/bash
#author:oldboy

goodnb() {
	shell=$1
	echo ${shell}_goodnb01
	echo ${shell}_goodnb02
	echo ${shell}_goodnb03
}

goodnb $1

十四、流程控制語句

命令 含義
return 函數中 在函數執行完成後 給函數返回值
exit 直接退出腳本 返回值
continue 控制循環,結束本次循環,進行下次循環(但不是結束整個循環)
break 終止包含它的循環,並進行程序的下一階段(整個循環後面的語句)

注意:c7和c8在shell方面進行了部分符合邏輯的調整,本部分可能c7和c8可能存在差異,此文檔以c7爲標準。

復一份c8系統的shell腳本:
[root@shell 2.5]# cat sb2.sh
#!/bin/bash
. /etc/init.d/functions

i1() {
read -p '輸入:' str1
read -p '輸入:' str2
}

check() {
if [[  "$str1" =~ ^[0-9]+$  ]] && [[  "$str2" =~ ^[0-9]+$  ]] ;then
	:
else
       	echo "請參數輸入數字"
        break
		#continue (c7推薦使用該參數終端循環,break在c7則會把所有循環中斷)
fi
}

p1() {
if [ $str1 -eq $str2 ]
then
	action "$str1 = $str2   相等" /bin/true
fi

if [ $str1 -gt $str2 ]
then
	action "$str1 > $str2   大於" /bin/true
fi

if [ $str1 -lt $str2 ]
then
	action "$str1 < $str2   小於" /bin/true
fi
}

while true
do
	i1
	check
	p1
done

注意:(c8上運行該腳本)該腳本的check函數中出現錯誤則無法使用break直接跳出循環,因爲break只是終止了自己的(check)循環,而which循環在break終止自己(check)循環後仍認爲(check)執行完畢了,則繼續進行繼續進行循環(而c7不會出現上述問題,所以大膽猜測,c8在shell方面進行了部分符合邏輯的調整)

十五、while當型循環

格式:示例可以參考上面的c8腳本最後一段的while循環
while  條件    
do
     命令
done
while 當型循環,讀取文件格式
例子:
[root@shell 2.5]# cat sb3.sh
#!/bin/bash
file=/etc/passwd
while read test
do
   echo $test
done<$file

十六、for循環

格式:
for n in 列表(名單)
do 
命令
done

十七、shell數組

存放或使用,沒有太多規律的信息,則使用shell數組
注意:自動賦值和文件創建的數組默認下標從0開始

方法 示例
直接賦值 test[1]=goodnb01
自動賦值 test=(goodnb01 goodnb02)
通過文件創建 array=(`cat /etc/passwd `)
1、叫法:數組名稱[下標] 或 數組名稱[元素名稱] 
[root@db01 ~]# test[1]=goodnb01
[root@db01 ~]# test[2]=goodnb02
[root@db01 ~]# echo ${test[1]}
goodnb01

2、取出數組所有內容
[root@db01 ~]# echo ${test[*]}
goodnb01 goodnb02

3、取出數組元素的個數
[root@db01 ~]# echo ${#test[*]}
2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章