shell腳本學習(二)

shell腳本學習基礎知識、接上一篇。

字符串

變量聲明可以使用單引號''或者雙引號"".

  1. 單引號的限制:
    . 按原字符輸出,單引號中的變量是無效的。
    . 單引號中不能出現單引號(轉義也不行)

  2. 雙引號
    . 可以有變量
    . 可以出現轉義

    對字符串的各種操作。

    # join the str
    a="hello"
    b=""$a"world"       ==   "${a}world"
    # get the str length 	
    echo "${#a}"                 // 5
    # split sub str
    echo "${a:1:2}"               // "el"
    # find  sub str  返回字符串中第一個字符第一次出現的位置
    echo `expr index "${a}" e`         // 1
    
數組

支持一維數組,不限定大小。

  1. 定義
    用括號定義,使用空格分割
    # define
    arr=(1 2 4 5)
    # single define & the index could not continue 
    # if the index is not exist ,the value is empty
     arr[0]=3
     arr[1]=4
     arr[2]=6
     arr[6]=9
    
  2. 獲取
    # ouput arr
    echo "arr ${arr[2]}"       // 6
    # using * | @ output All
    echo "${arr[*]}"            // 3 4 6 9
    echo "${arr[5]}"           //
    # get the arr length using the char * | @
    echo "${#arr[*]}"              // 4
    # get the arr of the element length
    echo "${#arr[5]}"           // 0
    

數組下標不限,數組的長度爲實際存在值的元素數量。

echo /printf

輸出指令,打印字符串。

  1. 顯示轉義

    echo "\"hello wrold \""                     // "hello world"
    # 顯示指定換行(默認換行)                         ** 測試無用、打印出了 \n
    echo "hello \n world"
    # 指定不換行                         ** 測試無用、打印出了 \c
    echo "hello \c"
    echo "world"
    
  2. 輸出結果重定向到文件

    echo  "hello world" > log 
    

    > 覆蓋文件內容 >> 追加內容,

  3. printf 打印字符、需要手動加換行符

    printf "hello \n"                          
    printf "world"                   
    # format string
    printf "%d %s" 4 "name"          
    
    // 輸出
    hello
    world4 name    
    

    %d格式數值,默認 0 %s格式字符串 默認爲空

test

檢測某個條件是否成立,可進行數值、文件、字符三個方面。

  1. 數值測試,使用關係運算符
    關係運算符

    a=10
    b=24
    if test ${a} -eq ${b}
    then
    	echo 'a eq b
    else 
    	echo 'a not eq b'
    fi
    
  2. 字符串測試、
    在這裏插入圖片描述

    if test ${a} = ${b}
    then	
    	echo 'a = b'
    else
    	echo 'a!=b'
    fi
    
  3. 文件測試

    檢測參數 說明
    -e [filename] 文件存在爲 true
    -r [filename] 文件存在且可讀 true
    -w [filename] 文件存在且可寫 true
    -x [filename] 文件存在可執行true
    -s [filename] 文件存在且 至少有一個字符 true
    -d [filename] 文件存在且爲目錄 true
    -f [filename] 文件存在且 爲普通文件 true
    -c [filename] 文件存在且 爲字符型特殊文件 true
    -b [filename] 文件存在且 爲塊特殊文件 true
    if test -e ./008.sh
    then 
    	echo 'exit'
    fi
    

    提供 !(與) -a(非) -o(或)多個條件連接。

函數
  1. 函數定義

    # define
    function getName(){
    	echo 'hahahha'
    }
    # Invoke your function
    getName
    echo $?      // 0
    

    調用函數直接寫函數名。
    使用$? 來獲取函數調用執行的返回值。
    函數刪除,使用unset命令,需加上.f選項。

    函數默認返回 0 ,表示調用成功。
    只能返回數值,不能返回字符串,會報錯。

  2. 函數參數
    在調用函數時,參數以空格分隔匝函數名後。

    function getName(){
    	echo $1
    	echo $2
    }
    getName 2 3 4 5
    // 2 3
    

    使用 $n 來獲取第幾個參數。n>=10 書寫爲 ${10}

    變量 說明
    $# 參數個數
    $* 顯示所有傳入的參數
    $@ 與$* 同
    $? 函數的返回值
輸入輸出重定向

command > [file] 覆蓋文件內容
command >> [file]追加到文件
command < [file]獲取文件內容

感謝作者:shell教程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章