shell 脚本简单入门
好久不寫shell腳本,有些生疏。總結下shell的語法,以便后續參考,快速撿起來。
-
shell 腳本執行的3種方式:
1). ./xx.sh (xx.sh 需要有執行權限)
2). source xx.sh
3). bash xx.sh -
變量定義
var=2//注意:=左右沒有空格 -
變量引用的2種方式
$符號就是變量解引用符號$var ${var} -
字符串初始化
string=hello worldstring="hello world" -
單引號、雙引號
單引號中:完全字面替換(不可包含單引號本身)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\ -
調用linux 命令
1)直接執行pwd
- 用反引號(和~位于同一個按鍵)括起來
````pwd`
```
-
分支
注意[ 表達式 ]里面表達式前后各有一個空格
if [ 表達式 ]; then
xxx
else
xxx
fi1).判斷文件是否存在 -f
if [ -f 1.sh ]; thenecho "file exist"elseecho "file does not exist"fi2). 判斷目錄是否存在 -d
if [ -d `pwd` ]; thenecho "`pwd` exist"elseecho "dir does not exist"fi//輸出:/home/ricky/code/shell exist3). 判斷字符串是否相等: 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 fi4). 數字比較
相等(-eq)
大于(-gt)
小于(-lt)
大于等于(-ge)
小于等于(-le)1 #!/bin/bash2 3 n=104 5 if [ $n -gt 5 ]; then6 echo "n is grate than 5"7 fi5). 判斷字符串是否為空(-z)
1 #!/bin/bash2 3 string="hello"4 5 if [ -z $string ]; then6 echo "null string"7 else8 echo "no null string"9 fi6). 邏輯或(-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 fi7). 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 脚本简单入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个好听的谐音名字。
- 下一篇: linux kernel内存回收机制