日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

shell 脚本简单入门

發布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shell 脚本简单入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

好久不寫shell腳本,有些生疏。總結下shell的語法,以便后續參考,快速撿起來。

  1. shell 腳本執行的3種方式:
    1). ./xx.sh (xx.sh 需要有執行權限)
    2). source xx.sh
    3). bash xx.sh

  2. 變量定義
    var=2 //注意:=左右沒有空格

  3. 變量引用的2種方式
    $符號就是變量解引用符號

       $var    ${var}
    
  4. 字符串初始化

       string=hello worldstring="hello world"
    
  5. 單引號、雙引號
    單引號中:完全字面替換(不可包含單引號本身)

          1 #!/bin/bash2 3 string="hello world"4 `echo '$string\\'
    

    `
    運行:

    ricky@ubuntu:~/code/shell$ ./1.sh
    $string\\
    
     雙引號中:轉義字符/$加變量名可以取變量的值反引號仍表示命令替換\$表示$的字面值輸出$符號\`表示`的字面值\"表示"的字面值\\表示\的字面值1 #!/bin/bash2 string="hello world"3 echo "$string\\"
    

    運行:

    ricky@ubuntu:~/code/shell$ ./1.sh
    hello world\
    
  6. 調用linux 命令
    1)直接執行 pwd

  1. 用反引號(和~位于同一個按鍵)括起來
````pwd`
```
  1. 分支
    注意[ 表達式 ]里面表達式前后各有一個空格
    if [ 表達式 ]; then
    xxx
    else
    xxx
    fi

    1).判斷文件是否存在 -f

        if [ -f 1.sh ]; thenecho "file exist"elseecho "file does not exist"fi
    

    2). 判斷目錄是否存在 -d

    	if [ -d `pwd` ]; thenecho "`pwd` exist"elseecho "dir does not exist"fi
    
     //輸出:/home/ricky/code/shell exist
    

    3). 判斷字符串是否相等: str1 = str2 一個等號,和C語言不一樣

          1 #!/bin/bash2 string="hello world"3 4 if [ "$string" = "hello world" ]; then5     echo "the same string"6 else7     echo "dif string"8 fi
    

    4). 數字比較
    相等(-eq)
    大于(-gt)
    小于(-lt)
    大于等于(-ge)
    小于等于(-le)

        1 #!/bin/bash2 3 n=104 5 if [ $n -gt 5 ]; then6     echo "n is grate than 5"7 fi
    

    5). 判斷字符串是否為空(-z)

          1 #!/bin/bash2 3 string="hello"4 5 if [ -z $string ]; then6     echo "null string"7 else8     echo "no null string"9 fi
    

    6). 邏輯或(-o) 邏輯與(-a)

          1 #!/bin/bash2 3 string="hello"4 n=105 6 if [ -z $string -o $n -gt 5 ]; then7     echo "null string or n is greate than 5"8 else9     echo "no null string"10 fi
    

    7). if…else…快速寫法:用&&或者||表示

    if [ A ]; then
    do B
    fi
    可以用 [ A ] && do B 表示

       1 #!/bin/bash2 3 n=104 5 if [ $n -gt 5 ]; then6     echo "n is great than 5"7 fi8 9 [ $n -gt 5 ] && echo "n is great than 5"
    
  運行
ricky@ubuntu:~/code/shell$ ./1.sh
n is great than 5
n is great than 5
  if [ A ]; thendo Bfi可以用 [ ! A ] || do B 表示```1 #!/bin/bash2 3 n=104 5 if [ $n -gt 5 ]; then6     echo "n is great than 5"7 fi8 9 [ ! $n -gt 5 ] || echo "n is great than 5"

運行:

 	ricky@ubuntu:~/code/shell$ ./1.shn is great than 5n is great than 5

&& 和 || 還可以執行命令
cmd1 && cmd2 :
若cmd1執行完畢且正確執行($?=0),則開始執行cmd2
若cmd1執行完畢且返回出錯($?≠0),則不執行cmd2
1 #!/bin/bash 2 3 pwd && ls
運行:
ricky@ubuntu:~/code/shell$ ./1.sh /home/ricky/code/shell 1.sh

   1 #!/bin/bash2 3 ! pwd && ls```運行:```		 ricky@ubuntu:~/code/shell$ ./1.sh/home/ricky/code/shell```cmd1 || cmd2		若cmd1執行完畢且正確執行(`$?=0`),則不執行cmd2若cmd1執行完畢且返回出錯(`$?≠0`),則執行cmd2
  1 #!/bin/bash2 3 ! pwd || ls
	運行:ricky@ubuntu:~/code/shell$ ./1.sh/home/ricky/code/shell1.sh
8.  for 循環```1 #!/bin/bash2 3 for i in 1 2 3 44 do5     echo "$i"6 done```輸出:12349.  while 循環```1 #!/bin/bash2 3 i=04 while [ $i -le 4 ]5 do6     echo "$i"7     let i++8 done```輸出:01234	10. switch case```1 #!/bin/bash2 3 cond="in"4 5 case $cond in6 in) echo "$cond" ;;7 out) echo "out"  ;;8 esac```//注意,和C語言不同,沒有break11. 注釋多行(:<<!和!):<<!line1line2...!12. shell程序的參數:```$0: shell 腳本的名字$1 $2 $3: 第1/2/3參數 $#: 參數的個數(不包含腳本的名字)$@: 所有的參數``````1 #!/bin/bash2 3 echo "para_0: $0"4 echo "para_1: $1"5 echo "para_2: $2"6 echo "para num: $#"7 echo "para list: $@"```1)運行: ```ricky@ubuntu:~/code/shell$ ./1.shpara_0: ./1.shpara_1: para_2: para num: 0para list:```2)運行:```ricky@ubuntu:~/code/shell$ ./1.sh a b cpara_0: ./1.shpara_1: apara_2: bpara num: 3para list: a b c```15. shift 指令
shift可以用來向左移動位置參數```1 #!/bin/bash2 3 while [ $# -gt 0 ]4 do5     echo $@6     shift  #shift para rightward7 done```運行:```ricky@ubuntu:~/code/shell$ ./1.sh a b ca b cb cc```

總結

以上是生活随笔為你收集整理的shell 脚本简单入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。