在Linux系統介紹中,介紹了shell的多個版本,如今的Linux發行版基本都默認使用bash(Bourne Again shell),兼容Bourne shell (sh),本文將簡要介紹Bash編程語法。html
定義變量shell
your_name="abc" echo $your_name
拼接字符串express
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 # 賦值
數組實例:vim
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 #不能刪除只讀變量
不能刪除只讀變量bash
# b=10 # readonly b # echo $b 10 # unset b -bash: unset: b: cannot unset: readonly variable #
顯示全部環境變量oop
env # 或者 printenv
顯示環境變量值測試
printenv LANG # 或者 echo $LANG
if condition then command1 command2 ... commandN fi
if和then寫在同一行時,用分號分隔。.net
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 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
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 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
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=10 # b=20 # echo `expr $b % $a` 0 # echo $[$a == $b] 0 # echo $[$a != $b] 1 #
# 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 歡迎關注公衆號:「測試開發小記」及時接收最新技術文章!