程序的三大結構: 順序、循環、分支
shell是一個用C語言編寫的程序,他是用戶使用LInux的橋梁,shell既是一種明林關于楊,又是一種程序設計語言。
Shell有兩種執行命令的方式:
- 交互式(Interactive):解釋執行用戶的命令,用戶輸入一條命令,Shell就解釋執行一條。
- 批處理(Batch):用戶事先寫一個Shell腳本(Script),其中有很多條命令,讓Shell一次把這些命令執行完,而不必一條一條地敲命令。
類型:
自定義變量: 局部變量在腳本或命令中定義,僅在當前shell實例中有效,其他shell啟動的程序不能訪問局部變量
環境變量(PATH): 所有的程序,包括shell啟動的程序,都能訪問環境變量,有些程序需要環境變量來保證其正常運行。必要的時候shell腳本也可以定義環境變量。
特殊變量:shell變量是由shell程序設置的特殊變量。shell變量中有一部分是環境變量,有一部分是局部變量,這些變量保證了shell的正常運行
echo
1 echo
[root@shell ~]# echo "請輸入你的選擇:" 默認會打印換行符
請輸入你的選擇:
[root@shell ~]# echo -n "請輸入你的選擇:"
請輸入你的選擇:[root@shell ~]#
[root@shell ~]# echo -e "a\nbb\nccc" \n:回車
a
bb
ccc
[root@shell ~]# echo -e "a\tbb\tccc" \t tab鍵
a bb ccc
[root@shell ~]#
shell 變量
變量
增加腳本的靈活性、實適用性
變量類型
自定義變量
環境變量(PATH)
特殊變量
自定義變量
- 聲明變量
變量名稱=變量值
然后要知道的一些變量命名規則:
1,首個字符必須為字母(a-z,A-Z)。
2,中間不能有空格,可以使用下劃線(_)。
3,不能使用標點符號。
4,不能使用bash里的關鍵字(可用help命令查看保留關鍵字)。
- 調用變量
$變量名字
${變量名稱} 變量名稱后緊跟數字,字符的時候
student@student-VirtualBox:~$ name=cat
student@student-VirtualBox:~$ echo "this is a $name"
this is a cat
雙引號和單引號的區別:
單引號: 所有字符會失云原有的含義
雙引號: 特殊字符會被轉義
student@student-VirtualBox:~$ echo "${name}s"
cats
student@student-VirtualBox:~$ echo 'this is ${name}s'
this is ${name}s
SHELL變量的值默認全都作為字符處理
student@student-VirtualBox:~$ a=10
student@student-VirtualBox:~$ b=20
student@student-VirtualBox:~$ c=a+b
student@student-VirtualBox:~$ echo $c
a+b
student@student-VirtualBox:~$ c=$a+$b
student@student-VirtualBox:~$ echo $c
10+20
3.如何使用變量的值作為數學運算
方法1: $((EXPRESSION))
student@student-VirtualBox:~$ a=10
student@student-VirtualBox:~$ b=20
student@student-VirtualBox:~$ c=$((a+b))
student@student-VirtualBox:~$ echo $c
30
方法2: 關鍵字let
student@student-VirtualBox:~$ a=10
student@student-VirtualBox:~$ b=20
student@student-VirtualBox:~$ let c=a+b
student@student-VirtualBox:~$ echo $c
30
方法3:關鍵字 declare
* -r 只讀
* -i 整數
student@student-VirtualBox:~$ a=10
student@student-VirtualBox:~$ b=10
student@student-VirtualBox:~$ declare -i c=a+b
student@student-VirtualBox:~$ echo $c
20
生成隨機數
在shell中有一個環境變量RANDOM,它的范圍是0--32767
如果我們想要產生0-25范圍內的數,如何做呢?如下:
student@student-VirtualBox:~$ echo $((RANDOM%26))
24
4.命令引用
反引號 `COMMAND`
$(COMMAND)
student@student-VirtualBox:~$ a=`ls -ldh /etc/`
student@student-VirtualBox:~$ echo $a
drwxr-xr-x 135 root root 12K 9月 18 08:34 /etc/
student@student-VirtualBox:~$ b=$(ls -ldh /etc/)
student@student-VirtualBox:~$ echo $b
drwxr-xr-x 135 root root 12K 9月 18 08:34 /etc/
- 刪除變量
unset 變量名稱
環境變量
- 查看環境變量
student@student-VirtualBox:~$ env
PYENV_ROOT=/home/student/.pyenv
TERM=vt100
SHELL=/bin/bash
XDG_SESSION_COOKIE=57d184b41fe9d3d09850502c00000003-1505731690.718314-1315926397
SSH_CLIENT=10.0.167.238 8902 22
SSH_TTY=/dev/pts/1
USER=student
2.定義環境變量,修改環境變量的值
# export 變量名稱=變量值
3.特殊變量
參數處理 | 說明 |
---|---|
$# | 傳遞到腳本的參數個數。 |
$* | 以一個單字符串顯示所有向腳本傳遞的參數。如"$*"用「"」括起來的情況、以"$1 $2 … $n"的形式輸出所有參數。 |
$$ | 腳本運行的當前進程ID號 |
$! | 后臺運行的最后一個進程的ID號 |
$@ | 與$*相同,但是使用時加引號,并在引號中返回每個參數。如"$@"用「"」括起來的情況、以"$1" "$2" … "$n" 的形式輸出所有參數。 |
$- | 顯示Shell使用的當前選項,與[set命令]功能相同。 |
$? | 顯示最后命令的退出狀態。0表示沒有錯誤,其他任何值表明有錯誤。 |
$* 與 $@ 區別:
相同點:都是引用所有參數。
不同點:只有在雙引號中體現出來。假設在腳本運行時寫了三個參數 1、2、3,,則 " * " 等價于 "1 2 3"(傳遞了一個參數),而 "@" 等價于 "1" "2" "3"(傳遞了三個參數)。
字符串
獲取字符串長度
student@student-VirtualBox:/tmp$ s=abcd
student@student-VirtualBox:/tmp$ echo ${#s}
4
提取子字符串
student@student-VirtualBox:/tmp$ s=abceef
student@student-VirtualBox:/tmp$ echo ${s:1:3}
bce
數組
定義數組:
變量=(a b c d)
student@student-VirtualBox:/tmp$ arr=(1 2 3 4 5)
讀取數組
${array_name[index]}
student@student-VirtualBox:/tmp$ echo ${arr[0]}
1
獲取數組中的所有元素
使用@ 或 * 可以獲取數組中的所有元素,例如:
#!/bin/bash
my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D
echo "數組的元素為: ${my_array[*]}"
echo "數組的元素為: ${my_array[@]}"
執行腳本,輸出結果如下所示:
student@student-VirtualBox:/tmp$ ./t1.sh
數組的元素為: A B C D
數組的元素為: A B C D
獲取數組的長度
獲取數組長度的方法與獲取字符串長度的方法相同,例如:
#!/bin/bash
my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D
echo "數組的元素為: ${#my_array[*]}"
echo "數組的元素為: ${#my_array[@]}"
執行腳本,輸出結果如下所示:
student@student-VirtualBox:/tmp$ ./t1.sh
數組的元素為: 4
數組的元素為: 4