在Linux系統介紹中,介紹了shell的多個版本,現在的Linux發行版基本都默認使用bash(Bourne Again shell),兼容Bourne shell (sh),本文將簡要介紹Bash編程語法。
變量
命名規則
- 只能使用英文字母,數字和下划線,首個字符不能以數字開頭
- 中間不能有空格,可以使用下划線(_)
- 不能使用標點符號
- 不能使用bash里的關鍵字(可用help命令查看保留關鍵字)

定義與使用變量
定義變量
your_name="abc"
echo $your_name
拼接字符串
your_name="world"
your_name2="hello,$your_name!"
echo $your_name2
數組
array_name=(value0 value1 value2 value3)
valuen=${array_name[n]} # 數組取值
array_name[0]=value0 # 賦值
length=${#array_name[@]} # 獲取數組長度
數組實例:
my_array=(A B "C" D)
echo "第一個元素為: ${my_array[0]}"
my_array[1]=b
echo "數組的元素為:${my_array[*]}" # 打印所有元素
echo "數組的元素為:${my_array[@]}"
輸出:
第一個元素為: A
數組的元素為:A b C D
數組的元素為:A b C D
只讀變量
a="123"
readonly a
刪除變量
unset variable_name #不能刪除只讀變量
不能刪除只讀變量
# b=10
# readonly b
# echo $b
10
# unset b
-bash: unset: b: cannot unset: readonly variable
#
環境變量
顯示所有環境變量
env
# 或者
printenv
顯示環境變量值
printenv LANG
# 或者
echo $LANG
控制語句
條件分支:if
if定義
if condition
then
command1
command2
...
commandN
fi
if和then寫在同一行時,用分號分隔。
if [ 2==2 ]; then
echo "true";
else
echo "false";
fi
判斷條件寫法
# 寫法一
test expression
# 寫法二
[ expression ]
# 寫法三
[[ expression ]]
if test 2==2; then echo "true"; fi
if [ 2>1 ]; then echo "true"; fi
if [[ 2>1 ]]; then echo "true"; fi
實例
比較兩個變量的大小
a=10
b=20
if [ $a -eq $b ]; then
echo"equal";
elif [ $a -lt $b ]; then
echo "small";
elif [ $a -gt $b ]; then
echo "big";
fi
循環:for
for定義
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
實例
for和do寫在同一行時,用分號分隔。
for Ioop in 1 2 3 4 5
do
echo "hello"
done
for Ioop in 1 2 3 4 5;do
echo "hello"
done
循環讀取文件內容並輸出
for i in $(cat test.txt); do echo $i; done
循環遍歷列表
list=(value1 value2 value3)
for i in ${list[*]}; do echo $i; done
# 或者
for i in ${list[@]}; do echo $i; done
循環: while
while定義
while condition
do
command
done
實例
int=1
while(( $int<=5))
do
echo $int
let "int++"
done
循環讀取文件內容並輸出
while read line; do echo $line; done<test.txt
輸出:
test1
test222
test3
test4
test5
read命令
- read命令是用於從終端或者文件中讀取輸入的內部命令
- 讀取整行輸入
- 每行末尾的換行符不被讀入
read命令使用
從標准輸入讀取輸入並賦值給變量
read var
從標准輸入讀取多個內容
read varl var2 var3
不指定變量(默認賦值給 REPLY)
read
實例
# read a
123
# echo $a
123
# read a b c
1 2 3
# echo $a
1
# echo $b
2
# echo $c
3
#
默認變量
# read
456
# echo $REPLY
456
#
注釋
# 注釋
# 多行注釋
:<<EOF
內容
.......
EOF
腳本參數傳遞
- $0 腳本名稱
- $1~$n 獲取第n個參數:
- $# 傳遞到腳本的參數個數
- $$ 腳本運行的當前進程ID號
- $* 以一個單字符串顯示所有向腳本傳遞的參數
- $? 顯示最后命令的退出狀態。0表示沒有錯誤,其他任何值表明有錯誤
vim param.sh:
#!/bin/bash
echo "腳本名稱:$0"
echo "腳本運行的當前進程ID號:$$"
echo "參數個數:$#"
echo "所有參數:$*"
echo "第1個參數:$1"
echo "第10個參數:${10}"
echo "return "$?
執行:
# chmod +x param.sh
# ./param.sh 1 2 3 4 5 6 7 8 9 10 1
腳本名稱:./param2.sh
腳本運行的當前進程ID號:21097
參數個數:11
所有參數:1 2 3 4 5 6 7 8 9 10 1
第1個參數:1
第10個參數:10
return 0
#
基本運算
bash會把反引號里面當作一條命令來執行
In:
# echo `date +%y/%m/%d`
20/12/27
# echo `expr 2 + 2`
4
# a=10
# b=20
# echo expr $a + $b
30
# echo $(($a+$b))
30
# echo expr $a - $b
-10
# echo expr $a \* $b
200
# echo expr $b / $a
2
#
- % 取余
- = 賦值 a=$b 將把變量b的值賦給a
- == 相等 相同則返回true
- != 不相等 不相同則返回true
# a=10
# b=20
# echo `expr $b % $a`
0
# echo $[$a == $b]
0
# echo $[$a != $b]
1
#
- -eq 檢測相等
- -ne 檢測不相等
- -gt 檢測左邊是否大於右邊
- -lt 檢測左邊是否小於右邊
- -ge 檢測左邊是否大於等於右邊
- -le 檢測左邊是否小於等於右邊
# vim test.sh
# cat test.sh
#!/bin/bash
a=10
b=20
if [ $a -lt $b ]
then
echo "equal"
fi
# chmod +x test.sh
# ./test.sh
equal
#
其它實例
內存統計
#!/bin/bash
# 內存使用百分比
free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/'
# 統計內存
for i in `ps aux | awk '{print $6}' | grep -v 'RSS'`; do
count=$[$count+$i]
done
echo "$count/kb"
# ./test.sh
16%
474608/kb
求階乘
vim test.sh
read -p "Enter a number:"
factorial=1
for (( count=1; count<=$REPLY; count++))
do
factorial=$[ $factorial * $count ]
done
echo "The factorial of $REPLY is $factorial"
# chmod +x test.sh
# ./test.sh
Enter a number:6
The factorial of 6 is 720
文章標題:Linux Bash編程
本文作者:hiyo
本文鏈接:https://www.cnblogs.com/hiyong/p/14238495.html
歡迎關注公眾號:「測試開發小記」及時接收最新技術文章!
