Linux的Shell編程入門

WHAT IS SHELL?

      Shell 是一個命令行解釋器,爲用戶提供了一個向Linux內核發送請求以便運行程序的界面系統級程序,用戶可以用Shell來啓動、掛起、停止甚至是編寫一些程序。

      Shell是一個功能強大的語言,易編寫、易調試、靈活性強。是解釋執行的腳本語言,在Shell中可以調用Linux命令。

HELLOWORLD!

     Shell文件的後綴是   .sh 。我們直接創建helloworld.sh  編寫如下代碼並保存:

#!/bin/bash
echo "helloworld"

     怎麼執行呢?直接運行helloworld.sh 文件啊,使用相對路徑 ./helloworld.sh 和絕對路徑都是OK的,也可以使用如下的命令組合啊,但需要注意的是,我們創建的文件是沒有操作權限的,暴力給一下“chmod 777 helloworld.sh”,再執行:

sh ./helloworld.sh
bash ./hellworld.sh

那種路徑你自己選啊,咋舒服咋來。

Shell中的變量

      shell中的變量分爲系統變量和用戶自定義變量,可以通過set指令查看所有的系統變量,set指令會以“指令名=值的形式列出所有系統變量”,下列指令展示查看部分系統變量:

[root@hadoop0522 helloworld]# echo $HOME
/root
[root@hadoop0522 helloworld]# cd ~
[root@hadoop0522 ~]# echo $HOME
/root
[root@hadoop0522 ~]# echo $PWD
/root
[root@hadoop0522 ~]# cd /opt/helloworld/
[root@hadoop0522 helloworld]# echo $PWD
/opt/helloworld
[root@hadoop0522 helloworld]# echo $SHELL
/bin/bash
[root@hadoop0522 helloworld]# echo $USER
root

      Shell中定義變量無數據類型,使用下面方式定義、撤銷、定義靜態變量:

[root@hadoop0522 helloworld]# ALOHA=zhangyifei
[root@hadoop0522 helloworld]# echo $ALOHA
zhangyifei
[root@hadoop0522 helloworld]# unset ALOHA
[root@hadoop0522 helloworld]# echo $ALOHA

[root@hadoop0522 helloworld]# readonly ALOHA=199651
[root@hadoop0522 helloworld]# echo $ALOHA
199651
[root@hadoop0522 helloworld]# unset ALOHA
-bash: unset: ALOHA: cannot unset: readonly variable

可以看到,靜態變量甚至無法刪除,那麼怎麼刪除靜態變量呢?參考重啓Shell。

PS:在定義變量時注意:需要遵循定義的規範(字母數字下劃線);等號兩側不能有空格;變量名大寫;單引號將所有特殊字符脫意,雙引號僅將特殊字符脫意。脫意這個我沒驗證成功~暫且無視,用 echo -e 不脫意~

      反引號(一般是波浪線那個鍵)方式定義的變量是可執行的指令:

[root@hadoop0522 helloworld]# Q=`ls -la`
[root@hadoop0522 helloworld]# echo $Q
total 12 drwxr-xr-x. 2 root root 4096 May 31 20:18 . drwxr-xr-x. 4 root root 4096 May 31 20:06 .. -rwxrwxrwx. 1 root root 30 May 31 20:07 helloworld.sh

可見,ls -la 已經執行了。也可以這樣定義: Q=$(ls -la)效果相同。

        操作參數

編寫 param.sh文件,如下:

#!/bin/bash 

echo "$1"
echo "$3"
echo "$*"
echo "$@"
echo "$#"

$1:獲取第一個參數;$3:獲取第三個參數;$*:獲取所有參數,所有參數看成一個整體;$@:獲取所有參數,每個參數分開對待;$#:獲取參數的個數。

執行param.sh文件並傳遞5個參數:1 2 3 4 5 

[root@hadoop0522 helloworld]# ./param.sh 1 2 3 4 5
1
3
1 2 3 4 5
1 2 3 4 5
5

另外還有$$:獲取當前進程號;$!:獲取後臺最後一個進程的進程號;$?:最後一次執行命令的返回狀態,0表示執行正確,非0錯誤。

運算

格式:$((算式)) 或 $[算式] ; expr m+n,不能定義變量 ; 

運算符: +   -   /*(乘) /  %。

怎樣計算一個二加三乘四?

expr `expr 2 + 3` \* 4

條件判斷

語法:[ 判斷內容 ]  判斷內容和括號間需要跟空格,正確返回true,錯誤或無判斷邏輯返回false。

= -lt -le -gt -ge -eq -ne -r -w -x -f -e -d
字符串比較 小於 小於等於 大於 大於等於 等於 不等於 讀權限 寫權限 操作權限 文件存在且常規 文件存在 文件存在且是目錄

 

一個if判斷語句:

if [ 判斷條件 ] 
 then 
 這裏是執行的代碼
elif [ 判斷條件 ]
 then
 這裏是執行的代碼
fi

選擇語句:

    直接上case,輸入一個參數,是1打印1,是2打印2,其他打印other:

#!/bin/bash

case $1 in 
"1")
echo "1"
;;
"2")
echo "2"
;;
*)
echo "other"
;;
esac

結果:

循環語句:

     直接上case:

#!/bin/bash
#for in bianli
for i in "$*"
do
        echo "$i"
done

for j in "$@"
do
        echo "$j"
done

輸入及輸出結果:

   使用for循環求和1+2+...+100:

#!/bin/bash

s=0
for((i=1;i<=100;i++))
do
        s=$[$s+$i]
done

echo $s

結果:

使用while循環計算1+2+...+100:

#!/bin/bash

s=0
i=0

while [ $i -le 100 ]
do
        s=$[$s+$i]
        i=$[$i+1]
done

echo $s

打印結果:

函數

直接上demo,輸入兩個數字並求和:

#!/bin/bash

function sum()
{
        s=0
        s=$[$1 +$2]
        echo "$s"
}

read -p "please input the number1:" n1;
read -p "please input the number2:" n2;
sum $n1 $n2;

結果:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章