shell script的簡單使用_網頁設計

4{icon} {views}

台北網頁設計公司這麼多該如何選擇?

網動是一群專業、熱情、向前行的工作團隊,我們擁有靈活的組織與溝通的能力,能傾聽客戶聲音,激發創意的火花,呈現完美的作品

shell script的簡單介紹

shell變量

1.命名規則

  • 命名只能使用英文字母,数字和下劃線,首個字符不能以数字開頭
  • 中間不能有空格,可以使用下劃線(_)
  • 不能使用標點符號。
  • 不能使用bash里的關鍵字

2.定義變量:name=value
3.使用變量:$name
4.只讀變量:readonly name
5.刪除變量:uset name
6.變量類型

  • 局部變量:當前shell有效
  • 環境變量:所有的應用程序有效

shell字符串

  1. 雙引號:可解析變量,可以出現轉義字符
  2. 單引號:不解析變量,原樣輸出
  3. 字符串拼接:
name="hello"
# 使用雙引號拼接
greeting="hello, "$name" !"
greeting_1="hello, $name} !"
echo $greeting  $greeting_1
# 使用單引號拼接
greeting_2='hello, '$name' !'
greeting_3='hello, ${name} !'
echo $greeting_2  $greeting_3

輸出結果為:

網頁設計最專業,超強功能平台可客製化

窩窩以「數位行銷」「品牌經營」「網站與應用程式」「印刷品設計」等四大主軸,為每一位客戶客製建立行銷脈絡及洞燭市場先機。

hello, hello ! hello, hello} !
hello, hello ! hello, ${name} !
  1. 獲取字符串長度
string="abcd"
echo ${#string} #輸出 4
  1. 提取子字符串
string="hello world"
echo ${string:1:4}#輸出 ello
# 字符串下標從0開始
  1. 查找子字符串
string="hello world"
echo `expr index $string e`#輸出2

7.反引號和$()一樣,裏面的語句當作命令執行

shell數組

  1. 數組定義:arrname=(value0 value1 value3)
  2. 數組讀取:${arrname[]},中括號內為數組下標,從0開始
  3. 獲取數組長度:length=${#arrname[@]},*號也可以
  4. 或許數組單個元素的長度:lengthn=${#arrname[n]},n為數組下標

shell註釋

  1. 單行註釋:#
  2. 多行註釋:<<EOF以EOF結束,EOF可以是任意字母

shell傳遞參數

  1. 向腳本傳參
  • $0:執行的文件名
  • $1:為第一個參數
  • $2:為第二個參數 以此類推
  1. 特殊參數
  • $#:傳遞到腳本的參數個數
  • $$:腳本運行的當前進程ID
  • $!:後台運行的最後一個進程ID
  • $?:回傳碼,显示最後命令的退出狀態
  • $@,$*:显示所有傳遞的參數

shell運算符

  1. 算數運算符

假設變量a=10,b=20

運算符 說明 舉例
+ 加法 expr $a + $b 結果為 30。
減法 expr $a - $b 結果為 -10。
* 乘法 expr $a \* $b 結果為 200。
/ 除法 expr $b / $a 結果為 2。
% 取余 expr $b % $a 結果為 0。
= 賦值 a=$b 將把變量 b 的值賦給 a。
== 相等 用於比較兩個数字,相同則返回 true。 [ $a == $b ] 返回 false。
!= 不相等 用於比較兩個数字,不相同則返回 true。 [ $a != $b ] 返回 true。
  1. 關係運算符
運算符 說明 舉例
-eq 檢測兩個數是否相等,相等返回 true。 [ $a -eq $b ] 返回 false。
-ne 檢測兩個數是否不相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
-gt 檢測左邊的數是否大於右邊的,如果是,則返回 true。 [ $a -gt $b ] 返回 false。
-lt 檢測左邊的數是否小於右邊的,如果是,則返回 true。 [ $a -lt $b ] 返回 true。
-ge 檢測左邊的數是否大於等於右邊的,如果是,則返回 true。 [ $a -ge $b ] 返回 false。
-le 檢測左邊的數是否小於等於右邊的,如果是,則返回 true。 [ $a -le $b ] 返回 true。
  1. 邏輯運算符
運算符 說明 舉例
&& 邏輯的 AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| 邏輯的 OR [[ $a -lt 100 || $b -gt 100 ]] 返回 true
  1. 布爾運算符
運算符 說明 舉例
! 非運算,表達式為 true 則返回 false,否則返回 true。 [ ! false ] 返回 true。
-o 或運算,有一個表達式為 true 則返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a 與運算,兩個表達式都為 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false
  1. 字符串運算符
    假設變量a=hello,b=world
運算符 說明 舉例
= 檢測兩個字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 檢測兩個字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 檢測字符串長度是否為0,為0返回 true。 [ -z $a ] 返回 false。
-n 檢測字符串長度是否不為 0,不為 0 返回 true。 [ -n “$a” ] 返回 true。
$ 檢測字符串是否為空,不為空返回 true。 [ $a ] 返回 true
  1. 文件測試運算符
操作符 說明 舉例
-b file 檢測文件是否是塊設備文件,如果是,則返回 true。 [ -b $file ] 返回 false。
-c file 檢測文件是否是字符設備文件,如果是,則返回 true。 [ -c $file ] 返回 false。
-d file 檢測文件是否是目錄,如果是,則返回 true。 [ -d $file ] 返回 false。
-f file 檢測文件是否是普通文件(既不是目錄,也不是設備文件),如果是,則返回 true。 [ -f $file ] 返回 true。
-g file 檢測文件是否設置了 SGID 位,如果是,則返回 true。 [ -g $file ] 返回 false。
-k file 檢測文件是否設置了粘着位(Sticky Bit),如果是,則返回 true。 [ -k $file ] 返回 false。
-p file 檢測文件是否是有管道,如果是,則返回 true。 [ -p $file ] 返回 false。
-u file 檢測文件是否設置了 SUID 位,如果是,則返回 true。 [ -u $file ] 返回 false。
-r file 檢測文件是否可讀,如果是,則返回 true。 [ -r $file ] 返回 true。
-w file 檢測文件是否可寫,如果是,則返回 true。 [ -w $file ] 返回 true。
-x file 檢測文件是否可執行,如果是,則返回 true。 [ -x $file ] 返回 true。
-s file 檢測文件是否為空(文件大小是否大於0),不為空返回 true。 [ -s $file ] 返回 true。
-e file 檢測文件(包括目錄)是否存在,如果是,則返回 true。 [ -e $file ] 返回 true

流程控制

  1. if 語句語法格式:
if condition
then
    command1 
    command2
    ...
    commandN 
fi
  1. if else 語法格式:
if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi
  1. if else-if else 語法格式:
if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

for循環一般格式為:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
  1. while 循環一般格式
while condition
do
    command
done
  1. until 循環一般格式
until condition
do
    command
done
  1. case in格式
case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac
  1. 跳出循環:
  • break跳出所有循環
  • continue跳出當前循環

函數定義

輸入輸出重定向

  1. 文件描述符
  • 標準輸入 0
  • 標準輸出 1
  • 標準錯誤輸出 2
  1. stdin<file 標準輸入重定向
  2. stdout>file 標準輸出重定向
  3. stderr>>file 標準錯誤輸出重定向,以追加的方式

shell文件包含

  1. .filename
  2. source filename

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

※推薦評價好的iphone維修中心

擁有專業的維修技術團隊,同時聘請資深iphone手機維修專家,現場說明手機問題,快速修理,沒修好不收錢