Shell學習

Shell學習

shell概述

shell是一個命令行解釋器,它接收應用程序/用戶命令,而後調用操做系統內核。java

shell解釋器

  1. Linux提供的解釋器有
[shaofei@upuptop-pc ~]$ cat /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
  1. bash和sh的關係
[shaofei@upuptop-pc bin]$ ll | grep bash
-rwxr-xr-x    1 root root    964600 Aug  8  2019 bash
lrwxrwxrwx    1 root root         4 Oct 28  2019 sh -> bash
  1. Centos默認的解析器是bash
[shaofei@upuptop-pc bin]$ echo $SHELL
/bin/bash

Shell腳本入門

  1. 腳本格式
    腳本以 #!/bin/bash 開頭(指定解析器)面試

  2. 第一個shell腳本shell

[shaofei@upuptop-pc sh]$ touch helloworld.sh
[shaofei@upuptop-pc sh]$ vim helloworld.sh 


#!/bin/bash
echo "helloworld"
  1. 腳本的經常使用執行方式

(1) 採用bash或sh+腳本的相對路徑或絕對路徑(不用賦予腳本+x權限)vim

[shaofei@upuptop-pc sh]$ sh helloworld.sh 
helloworld

[shaofei@upuptop-pc sh]$ bash helloworld.sh 
helloworld

(2)採用輸入腳本的絕對路徑或相對路徑執行腳本(必須具備可執行權限+x)bash

[shaofei@upuptop-pc sh]$ chmod 777 helloworld.sh 
[shaofei@upuptop-pc sh]$ ./helloworld.sh 
helloworld
[shaofei@upuptop-pc sh]$ /home/shaofei/sh/helloworld.sh 
helloworld

注意:第一種執行方法,本質是bash解析器幫你執行腳本,因此腳本自己不須要執行權限。第二種執行方法,本質是腳本須要本身執行,因此須要執行權限。app

  1. 多命令處理
[shaofei@upuptop-pc sh]$ touch batch.sh
[shaofei@upuptop-pc sh]$ vim batch.sh 

#!/bin/bash
echo 'hello'
cd /home/shaofei/sh
echo 'cccc' > a.txt

Shell中的變量

系統變量

  1. 經常使用的系統變量

\(PWD,\)HOME,\(USER,\)SHELL等less

  1. 案例
[shaofei@upuptop-pc sh]$ echo $HOME
/home/shaofei
[shaofei@upuptop-pc sh]$ echo $PWD
/home/shaofei/sh
[shaofei@upuptop-pc sh]$ echo $USER
shaofei

顯示當前Shell中全部變量:set

[shaofei@upuptop-pc sh]$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()

………………

自定義變量

  1. 基本語法
    a. 定義變量: 變量名=變量值
    b. 撤銷變量: unset 變量名
    c. 聲明靜態變量: readonly 變量, 注意不能unset函數

  2. 定義規則
    a. 變量名可使用字母、數字、下劃線組成,可是不能以數字開頭。環境變量建議所有大寫
    b. 等號先後不能有空格
    c. 在bash中,變量類型默認是字符串類型,沒法直接進行數值計算
    d. 變量的值若是有空格必需要用"雙引號"引發來工具

  3. 案例學習

建立變量A並賦值爲5

[shaofei@upuptop-pc sh]$ A=5
[shaofei@upuptop-pc sh]$ echo $A
5

給變量A從新賦值爲9
[shaofei@upuptop-pc sh]$ A=9
[shaofei@upuptop-pc sh]$ echo $A
9

撤銷變量A
[shaofei@upuptop-pc sh]$ unset A
[shaofei@upuptop-pc sh]$ echo $A

建立靜態的變量B
[shaofei@upuptop-pc sh]$ readonly B=2
[shaofei@upuptop-pc sh]$ echo $B
2

靜態變量不能從新賦值
[shaofei@upuptop-pc sh]$ B=10
-bash: B: readonly variable

靜態變量不能unset
[shaofei@upuptop-pc sh]$ unset B
-bash: unset: B: cannot unset: readonly variable

在bash中,變量默認類型都是字符串類型,沒法直接進行數值運算
[shaofei@upuptop-pc sh]$ C=1+2
[shaofei@upuptop-pc sh]$ echo $C
1+2

變量的值若是有空格,須要使用雙引號或單引號括起來
[shaofei@upuptop-pc sh]$ D=I LOVE YOU
-bash: LOVE: command not found
[shaofei@upuptop-pc sh]$ D="I LOVE YOU"
[shaofei@upuptop-pc sh]$ echo $D
I LOVE YOU

可把變量提高爲全局環境變量,可供其餘Shell程序使用
[shaofei@upuptop-pc sh]$  vim helloworld.sh 

在helloworld.sh文件中增長echo $B
#!/bin/bash

echo "helloworld"
echo $B

沒有打印$B的值

[shaofei@upuptop-pc sh]$ sh helloworld.sh 
helloworld

修改B變量爲全局環境變量
[shaofei@upuptop-pc sh]$ export B
[shaofei@upuptop-pc sh]$ sh helloworld.sh 
helloworld
2

特殊變量:$n

  1. 基本語法

$n 功能描述:n爲數字,$0 表明該腳本名稱,$1-\(9表明第一到第九個參數,十之內的參數,十以上的參數須要用大括號包含,如\){10}

  1. 案例

輸出該腳本的文件名稱、輸入參數1和輸入參數2的值

[shaofei@upuptop-pc sh]$ touch param.sh
[shaofei@upuptop-pc sh]$ vim param.sh 

#!/bin/bash
echo $0  $1 $2

[shaofei@upuptop-pc sh]$ sh param.sh  1 2 3
param.sh 1 2

特殊變量:$#

  1. 基本語法

$# (獲取全部的參數個數,經常使用於循環)

  1. 案例
[shaofei@upuptop-pc sh]$ vim param.sh 
#!/bin/bash
echo $#
[shaofei@upuptop-pc sh]$ sh param.sh 1 2 3 4 5
5

特殊變量:$*$@

  1. 基本說法

$* (功能描述:這個變量表明命令行中全部的參數,$*把全部的參數看作一個總體)
$@ (功能描述: 這個變量表明命令行中全部的參數,不過$@把每一個參數區別對待)

  1. 案例
[shaofei@upuptop-pc sh]$ vim param.sh 

#!/bin/bash
echo $@
echo $*

[shaofei@upuptop-pc sh]$ sh param.sh 1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

特殊變量:$?

  1. 基礎語法

$? (功能描述:最後一次執行的命令的返回狀態。若是這個變量的值爲0,證實上一個命令正確執行;若是這個變量的值爲非0,則證實上一個命令執行不正確了)

  1. 示例:
[shaofei@upuptop-pc sh]$vim param.sh 
#!/bin/bash
echo $?

[shaofei@upuptop-pc sh]$ ./helloworld.sh 
helloworld

[shaofei@upuptop-pc sh]$ sh param.sh 
0

運算符

  1. 基礎語法

(1) $((運算式))$[運算式]
(2) expr +,-,*,/,% 加,減,乘,除,取餘

注意:expr 運算符之間要有空格

  1. 實例
    (1)計算3+2的值
[shaofei@upuptop-pc sh]$ expr 3 + 2
5

(2)計算3-2的值

[shaofei@upuptop-pc sh]$ expr 3 - 2  
1

(3)計算(2+3)* 4的值

第一種方式
[shaofei@upuptop-pc sh]$ expr `expr 2 + 3 ` \* 4
20

第二種方式
[shaofei@upuptop-pc sh]$ echo $(((3+2)*4))
20

第三種方式  
[shaofei@upuptop-pc sh]$ echo $[(2+3)*4]
20

條件判斷

  1. 基本語法
    [ condition ] 注意:condition先後有空格

  2. 經常使用的判斷條件

(1) 兩個整數之間比較

= 字符串比較
-lt 小於(less than)
-le 小於等於(less equal)
-eq 等於(equal)
-gt 大於(greater)
-ge 大於等於(greater equal)
-ne 不等於(Not equal)

(2) 按照文件權限進行比較
-r 有讀的權限(read)
-w 有寫的權限(write)
-x 有執行的權限(execute)

(3) 按照文件類型進行判斷

-f 文件存在而且是一個常規的文件(file)
-e 文件存在(existence)
-d 文件存在且是一個目錄(directory)

案例:

  1. 23 是否大於等於 22
[shaofei@upuptop-pc ~]$ [ 22 -ge 23 ]
[shaofei@upuptop-pc ~]$ echo $?      
1
[shaofei@upuptop-pc ~]$ [ 23 -ge 23 ] 
[shaofei@upuptop-pc ~]$ echo $?      
0
  1. HelloWorld.sh 是否有寫的權限
-rw-rw-r-- 1 shaofei shaofei  5 May  8 23:02 a.txt
-rw-rw-r-- 1 shaofei shaofei 65 May  8 23:01 batch.sh
-rwxrwxrwx 1 shaofei shaofei 38 May  8 23:36 helloworld.sh
-rw-rw-r-- 1 shaofei shaofei 31 Dec  8 01:01 k.sh.template
-rw-rw-r-- 1 shaofei shaofei 22 May  9 21:56 param.sh
-rw-rw-r-- 1 shaofei shaofei 59 Dec  8 01:01 start.sh.template
-rwxrwxrwx 1 shaofei shaofei 21 Nov 20 09:58 test1.sh
[shaofei@upuptop-pc sh]$ [ -r helloworld.sh ]
[shaofei@upuptop-pc sh]$ echo $?
0
[shaofei@upuptop-pc sh]$ [ -x batch.sh ]
[shaofei@upuptop-pc sh]$ echo $?
1
  1. /home/shaofei/aaa.txt 是否存在
[shaofei@upuptop-pc sh]$ [ -e /home/shaofei/aaa.txt ]
[shaofei@upuptop-pc sh]$ echo $?
1
  1. 多條件判斷(&& 表示前一條命令執行成功時,才執行後一條命令,|| 表示上一條命令執行失敗後,才執行下一條命令)
[shaofei@upuptop-pc sh]$ [ -e /home/shaofei/aaa.txt ] || echo false
false
[shaofei@upuptop-pc sh]$ [ -e /home/shaofei/aaa.txt ] && echo false  
[shaofei@upuptop-pc sh]$

流程語句(重點)

if判斷

  1. 基本語法
if [ 條件判斷式 ]; then

   程序代碼

fi

或者

if [ 條件判斷式 ]

then 
    程序代碼

fi

注意:

  • [ 條件表達式 ] 中括號和條件判斷式之間必須有空格
  • if後面要有空格
  • 第一種方式 then 前面要有分號
  1. 案例

輸入一個數字,若是是1 則輸出 true 若是是2 則輸出 false 若是是其餘數字則不作任何操做

[shaofei@upuptop-pc sh]$ vim if.sh 

#!/bin/bash

if [ $1 -eq 1 ]; then
        echo true
fi

if [ $1 -eq 2 ]; then
        echo false
fi

[shaofei@upuptop-pc sh]$ sh if.sh 1
true
[shaofei@upuptop-pc sh]$ sh if.sh 2
false

[shaofei@upuptop-pc sh]$ sh if.sh 123
[shaofei@upuptop-pc sh]$

case語句

  1. 基礎語法
case $變量名 in
    "value1")
        若是變量等於value1,執行程序
    ;;
   
    "value2")
        若是變量等於value2,執行程序
    ;;
   
   ……省略其餘分支……

esac

注意

  • case行尾必須爲單詞 in,每個模式匹配必須以)結束。
  • 雙分號;;表示命令序列結束,至關於java中的break
  • 最後可使用*)表示默認模式,至關於java中的break
  • 最後以esac結束
  1. 案例

輸入一個數字,若是是1 則輸出 true 若是是2 則輸出 false 若是是其餘數字輸出default

[shaofei@upuptop-pc sh]$ vim case.sh 
#!/bin/bash

case $1 in
        1)
        echo true
;;
        2)
        echo false
;;
        *)
        echo default
;;

esac



[shaofei@upuptop-pc sh]$ sh case.sh 1
true
[shaofei@upuptop-pc sh]$ sh case.sh 2
false
[shaofei@upuptop-pc sh]$ sh case.sh 3
default
[shaofei@upuptop-pc sh]$

for循環

  1. 基本語法
  • 第一種方式
for  (( 初始值;循環控制條件;變量變化 ))

    do
        程序
    done
  • 第二種方式
for  變量 in 變量1,變量2,變量

    do
        程序
    done
  1. 實例

計算1-100的和

[shaofei@upuptop-pc sh]$ vim for1.sh 

#!/bin/bash

sum=0
for ((i=1;i<=100;i++))
    do
        sum=$[$sum+$i] # or sum=$(( $sum+$i ))
    done

echo $sum

[shaofei@upuptop-pc sh]$ sh for1.sh

打印全部的輸入參數 比較$* 和 $@

  • $*$@ 都不被雙引號""包括的時候,沒有區別,$*$@都表示傳遞給函數或腳本的全部參數,不被雙引號""包含時,都以$1 $2 …$n的形式輸出全部參數。
[shaofei@upuptop-pc sh]$ vim  for2.sh

#!/bin/bash

echo ---------$*

for i in $*
        do
                echo $i
        done


echo --------$#

for j in $@
        do
                echo $j
        done

echo --------end

[shaofei@upuptop-pc sh]$ sh for2.sh 1 2 3 4
---------1 2 3 4
1
2
3
4
--------4
1
2
3
4
--------end
  • 當它們被雙引號""包含時,"$*"會將全部的參數做爲一個總體,以"$1 $2 …$n"的形式輸出全部參數;"$@"會將各個參數分開,以"$1" "$2"…"$n"的形式輸出全部參數。
[shaofei@upuptop-pc sh]$ vim  for3.sh

#!/bin/bash

echo ---------"$*"

for i in "$*"
        do
                echo $i
        done


echo --------$#

for j in "$@"
        do
                echo $j
        done

echo --------end


[shaofei@upuptop-pc sh]$ sh for3.sh 1 2 3 4
---------1 2 3 4
1 2 3 4
--------4
1
2
3
4
--------end

while循環

  1. 基本語法
while [ 條件表達式 ] 
    do
        程序
    done
  1. 案例

計算1-100的和

[shaofei@upuptop-pc sh]$ vim while.sh 
#!/bin/bash

sum=0
i=0
while [ $i -le 100 ]
    do
        sum=$(( $sum+$i ))
        i=$[$i+1]
    done

echo  $sum

[shaofei@upuptop-pc sh]$ sh while.sh  
5050

注意: while後面有空格.

read(讀取用戶輸入)

  1. 基本語法
read(選項)(參數)
	選項:
        -p:指定讀取值時的提示符;
        -t:指定讀取值時等待的時間(秒)。
    參數
    	變量:指定讀取值的變量名
  1. 實例
[shaofei@upuptop-pc sh]$ vim read.sh 

#!/bin/bash

read -p "input your name: " -t 3 NAME

echo "Your Name is $NAME !"



[shaofei@upuptop-pc sh]$ sh read.sh 
input your name: shaofeer
Your Name is shaofeer !

函數

系統函數

  • basename
  1. basename基本語法
basename [string/pathname][suffix]


(功能描述:basename命令會刪掉全部的前綴包括最後一個(‘/’)字符,而後將字符串顯示出來。
選項:
    suffix爲後綴,若是suffix被指定了,basename會將pathname或string中的suffix去掉。
  1. 案例實操
[shaofei@upuptop-pc sh]$ basename /home/shaofei/123.txt 
123.txt

[shaofei@upuptop-pc sh]$ basename /home/shaofei/123.txt .txt
123
  • dirname
  1. dirname基本語法
dirname 文件絕對路徑	
    (功能描述:從給定的包含絕對路徑的文件名中去除文件名(非目錄的部分),而後返回剩下的路徑(目錄的部分))
  1. 案例實操

獲取a.txt文件的路徑

[shaofei@upuptop-pc sh]$ dirname /home/shaofei/sh/a.txt 
/home/shaofei/sh

自定義函數

  1. 基本語法
[ function ] funname[()]
{
	Action;
	[return int;]
}
funname
  1. 經驗技巧
    -(1)必須在調用函數地方以前,先聲明函數,shell腳本是逐行運行。不會像其它語言同樣先編譯。
    -(2)函數返回值,只能經過$?系統變量得到,能夠顯示加:return返回,若是不加,將以最後一條命令運行結果,做爲返回值。return後跟數值n(0-255)

3.案例實操
(1)計算兩個輸入參數的和

[shaofei@upuptop-pc sh]$ vim fun.sh 

#!/bin/bash

function sum(){
        sum=$[$1+$2]
        return $sum

}


sum 1 2

echo $?


[shaofei@upuptop-pc sh]$ sh fun.sh 
3

shell工具(重點)

待更新……

面試題

待更新……

我的學習總結

相關文章
相關標籤/搜索