shell腳本基礎與變量

          當命令或者程序不再命令行執行,而是通過程序文件來執行,這個程序就被稱爲shell腳本。shell編程屬於腳本語言,是相對與編譯型語言(如C\C++,java,C#等 )而言的,腳本語言往往是解釋運行,而非編譯,它由編譯器讀入腳本程序代碼,轉化成內部的形式執行,腳本語言的特性(結構簡單,使用方便,容易修改,開放產能好)。

 1.腳本內容簡介

              打開文本編輯器,可以使用vim命令來創建腳本文件,新建一個文件test.sh,擴展名爲sh,也可以使用其他,但是爲了方便識別,sh表明此文件是shell腳本文件。#! 是一個約定的標記,它告訴系統這個腳本需要什麼解釋器來執行,即使用哪一種Shell,一般的解釋器是 bash。

#!/bin/bash
date

查看系統默認shell。 echo $SHELL

[root@foundation77 mnt]# echo $SHELL
/bin/bash

     2.腳本執行方法
sh test.sh | bash test.sh    腳本文件沒有執行權限時使用

[root@foundation77 mnt]# ll test.sh 
-rw-r--r--. 1 root root 17 Dec 27 13:00 test.sh
[root@foundation77 mnt]# sh test.sh 
Thu Dec 27 13:10:54 CST 2018
[root@foundation77 mnt]# bash test.sh 
Thu Dec 27 13:10:57 CST 2018

/mnt/test.sh | ./test.sh    絕對路徑 | 當前目錄下

[root@foundation77 mnt]# ls -l test.sh 
-rwxr--r--. 1 root root 17 Dec 27 13:00 test.sh
[root@foundation77 mnt]# ./test.sh
Thu Dec 27 13:51:24 CST 2018
[root@foundation77 mnt]# /mnt/test.sh 
Thu Dec 27 13:51:28 CST 2018

source .sh | . script.sh    sourec與. 執行腳本時,會把shell中的變量值或函數返回給父shell繼續使用,如圖中test.sh 中存在

cd /tmp 的一條內容,使用sh 執行使只會執行完畢後顯示結果,  sourec與. 執行後會保留腳本執行後的目錄 tmp。

修改test.sh 內容如下。

#!/bin/bash
cd /tmp
echo hello 
[root@foundation77 mnt]# vim test.sh 
[root@foundation77 mnt]# sh test.sh 
hello
[root@foundation77 mnt]# source test.sh 
hello
[root@foundation77 tmp]# cd /mnt
[root@foundation77 mnt]# . test.sh 
hello
[root@foundation77 tmp]# 

  3.定義變量與替換變量

變量用於保存有用信息,如路徑,文件名,數字等,用戶可以使用變量定製其工作環境。
變量賦值  :如下所示,給變量a賦值爲hello ,b賦值爲 obama-$a。

替換變量 :$ 符號爲變量替換符號,a 爲hello ,$a 就表示變量值。

(1 賦的值內部的空格必須用雙引號括起來 (沒有特別要求時,字符串都加雙引號較好,否則賦值中存在空格時無法識別,需要原樣輸出就加單引號)

(2 變量只能包括大小寫字母,數字,下劃線 等特定符號,並且不能用數字開頭,否則無效。

[root@foundation77 mnt]# a=hello
[root@foundation77 mnt]# echo $a
hello
[root@foundation77 mnt]# b=obama-$a
[root@foundation77 mnt]# echo $b
obama-hello
[root@foundation77 mnt]# echo $b $a
obama-hello hello
[root@foundation77 mnt]# 1=westos
bash: 1=westos: command not found...
[root@foundation77 mnt]# a1=westos
[root@foundation77 mnt]# echo $a1
westos
[root@foundation77 mnt]# 

4.   特殊變量
$0:獲取腳本文件名,如果執行時包含路徑,則輸出腳本路徑

[root@foundation77 /]# cat /mnt/test.sh

#!/bin/bash
cd /tmp
echo hello 
echo $0   
      
[root@foundation77 /]# sh /mnt/test.sh 
hello
/mnt/test.sh

[root@foundation77 /]# cd /mnt/
[root@foundation77 mnt]# sh test.sh 
hello
test.sh

$n  傳遞參數給腳本變量

[root@foundation77 mnt]# cat test.sh 
#!/bin/bash
echo $1 $2
[root@foundation77 mnt]# sh test.sh  sos qvq
sos qvq
[root@foundation77 mnt]# sh test.sh hello obama
hello obama
[root@foundation77 mnt]# 

[root@foundation77 mnt]# cat test.sh 
echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@foundation77 mnt]# sh test.sh {1..10}
1 2 3 4 5 6 7 8 9 10
[root@foundation77 mnt]# sh test.sh {1..20}
1 2 3 4 5 6 7 8 9 10
[root@foundation77 mnt]# sh test.sh {a..z}
a b c d e f g h i a0
[root@foundation77 mnt]# 

$#:傳遞到腳本的參數數量

[root@foundation77 mnt]# cat test.sh 
#!/bin/bash 
echo $#
[root@foundation77 mnt]# sh test.sh {a..z}
26


$* 和$@: 傳遞到腳本的所有參數

[root@foundation77 mnt]# cat test.sh 
#!/bin/bash 
echo $#
echo $*
[root@foundation77 mnt]# sh test.sh {a..z}
26
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@foundation77 mnt]#

$?:表示上條命令執行結果的返回值
0表示執行成功
非0表示有錯誤

將命令結果賦值給變量

[root@foundation77 mnt]# CMD=$(ls -l)
[root@foundation77 mnt]# echo $CMD
read.sh rhel-server-7.2-x86_64-dvd.iso sos.sh test.sh

 

 read 命令的用法

基本讀取

[root@foundation77 mnt]# read a
westos
[root@foundation77 mnt]# echo $a
westos

-p  可在命令行中輸入提示符

[root@foundation77 mnt]# read -p "請輸入密碼:"
請輸入密碼:
[root@foundation77 mnt]# vim read.sh
[root@foundation77 mnt]# cat read.sh 
#!/bin/bash
read -p  "輸入你的姓名:" a
echo “出門右轉 $a”
exit 0 
[root@foundation77 mnt]# sh read.sh 
輸入你的姓名:張
“出門右轉 張”
[root@foundation77 mnt]#

-t  等待的時間 ,等待多久沒有輸入後退出,不會一直等待

[root@foundation77 mnt]# read -p "請輸入密碼:"
請輸入密碼:^C                           # 此時如果不輸入內容會一直等待
[root@foundation77 mnt]# read -t 3 -p "請輸入密碼:"       #只等待3秒,3秒後自動退出
請輸入密碼:[root@foundation77 mnt]# 

-s 隱藏輸入:如果輸入的內容需要隱藏,可以加-s 不顯示。

[root@foundation77 mnt]# read  -p "請輸入密碼:"
請輸入密碼:1234
[root@foundation77 mnt]# read  -s -p "請輸入密碼:"
請輸入密碼:[root@foundation77 mnt]# 

測試網絡

[root@foundation77 mnt]# cat test.sh 
#!/bin/bash 
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down

[root@foundation77 mnt]# sh test.sh 107
172.25.254.107 is up
[root@foundation77 mnt]# sh test.sh 177
172.25.254.177 is down

 

 

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