一、第一個shell腳本
1、什么是shell
Shell 是操作系統的最外層,Shell 可以合並編程語言以控制進程和文件,以及啟動和控制其它程序。shell 通過提示輸入,向操作系統解釋該輸入,然后處理來自操作系統的任何結果輸出來管理您與操作系統之間的交互。簡單來說Shell 就是一個用戶跟操作系統之間的一個命令解釋器。
Shell 是用戶與 Linux 操作系統之間溝通的橋梁。用戶可以輸入命令執行,又可以利用 Shell 腳本編程去運行。
2、shell的種類
Linux Shell 種類非常多,常見的有:
Bourne Shell(/usr/bin/sh或/bin/sh)
Bourne Again Shell(/bin/bash)
C Shell(/usr/bin/csh)
K Shell(/usr/bin/ksh)
Shell for Root(/sbin/sh)等。
不同的 Shell 語言的語法有所不同,所以不能交換使用
最常用的shell是 Bash,也就是 Bourne Again Shell,由於易用和免費,Bash 在日常工作中被廣泛使用,也是大多數 Linux 系統默認的 Shell。
接下來我們來寫一個簡單的 shell 腳本。(shell 腳本一般文件名以.sh 結尾,同時文件第一行定義該腳本為 shell 腳本)
3、第一個shell腳本
執行:vi first_shell.sh
輸入: #!/bin/bash #This is my First shell echo "Hello world"
保存退出,shell腳本寫入完成
輸入命令執行:

編輯模式輸入:

然后執行:

解釋:
#! /bin/bash 表示定義該腳本是一個 shell 腳本(固定格式)。 #This is my First shell 這里的#號屬於注解,沒有任何的意義,shell 不會解析它。 echo "Hello world" shell 腳本主命令,我們執行這個腳 本講看到: Hello World 信息。 腳本編寫完畢,如何來執行呢,首先執行 shell 腳本需要執行權限,賦予執行權限:chmod o+x first_shell.sh 然后執行:./first_shell.sh 也可以直接使用bash環境命令執行: /bin/sh first_shell.sh,顯示效果一樣。
二、shell編程之變量
1、定義
Shell 編程語言是非類型的解釋型語言,不像 C++/JAVA 語言編程時需要事先聲明變量,shell 給一個變量賦值,實際上就是定義了變量,在Linux支持的所有shell中,都可以用賦值符號(=)為變量賦值。SHELL 變量可分為兩類:局部變量和環境變量。局部變量只在創建它們的 shell 腳本中使用。而環境變量則可以在創建它們的 shell及其派生出來的任意子進程中使用。有些變量是用戶創建的,其他的則是專用 shell 變量。
例如在腳本里面定義 A=123 ,定義這樣一個變量,前面變量名,后面是變量的值。
2、例子
引用變量可以使用$A,把變量放在腳本里面會出現什么樣的效果呢? 如下
#!/bin/bash #by authors zgs 2020 name=zgs echo "My name is $name" 執行腳本:sh var.sh,結果將會顯示:My name is zgs
簡單的理解變量,相當於定義一個別名-名稱,引用的時候加上$符號就可以了。


shell 常見的系統變量解析:
$0 :當前程序的名稱 $n :當前程序的第 n 個參數,n=1,2,…9 $* :當前程序的所有參數(不包括程序本身) $# :當前程序的參數個數(不包括程序本身) $? :上個命令是否執行成功,0 表示執行成功。不是0表示執行失敗 $UID :當前用戶的 ID $PWD :當前所在的目錄
例如:






三、if語句
1、判斷數字大小
if (表達式) ;then 語句 1 else 語句 2 fi
如:


2、判斷目錄是否存在
#! /bin/bash #auto if test #by authors zgs 2020 DIR=/tmp/20200604 if [ ! -d $DIR ];then mkdir -p $DIR echo -e "\033[32mThis $DIR Create success.\033[0m" else echo -e "\033[32mThis $DIR is exist, please exit.\033[0m" fi
如:


3、判斷文件是否存在
#! /bin/bash #auto test files #by authors zgs 2020 FILES=/tmp/text.txt if [ ! -f $FILES ];then echo "OK" >> $FILES else cat $FILES fi
如:


邏輯運算符解析:
-f 判斷文件是否存在 eg: if [ -f filename ] -d 判斷目錄是否存在 eg: if [ -d dir ] -eq 等於 應用於:整型比較 -ne 不等於 應用於:整型比較 -lt 小於 應用於:整型比較 -gt 大於 應用於:整型比較 -le 小於或等於 應用於:整型比較 -ge 大於或等於 應用於:整型比較 -a 雙方都成立(and) 邏輯表達式 –a 邏輯表達式 -o 單方成立(or) 邏輯表達式 –o 邏輯表達式 -z 空字符串
4、多個條件判斷
#! /bin/bash #auto test scores #by authors zgs 2020 scores=$1 if [ -z $scores ];then echo "usage:{$0 60|80}" exit fi if [[ $scores -gt 85 ]]; then echo "very good!"; elif [[ $scores -gt 75 ]]; then echo "good!"; elif [[ $scores -gt 60 ]]; then echo "pass!"; else echo "no pass!" fi
如:


5、if條件語句年寫Mysql備份腳本


6、一鍵源碼安裝 LAMP 腳本
功能: 打印菜單: 1)安裝 apache WEB 服務器 2)安裝 Mysql DB 服務器 3)安裝 PHP 服務器 4)整合 LAMP 架構並啟動服務 1、Apache 服務器安裝部署。 下載 httpd-2.2.27.tar.gz 版本,下載 URL,解壓,進入安裝目錄, configure;make ;make install 2、Mysql 服務器的安裝。 下載 mysql-5.5.20.tar.bz2 版本,下載 URL,解壓,進入安裝目錄, configure;make ;make install 3、PHP 服務器安裝。 下載 php-5.3.8.tar.bz2 版本,下載 URL,解壓,進入安裝目錄, configure;make ;make install 4、LAMP 架構的整合和服務啟動。


執行命令:sh auto_lamp1.sh 1,則下載安裝Apache
4、for循環
for 變量 in 字符串 do 語句 1 done
案例一:打印 seq 數字循環
#!/bin/sh for i in `seq 15` do echo 'NUM is $i' done
案例二:求和 1-100 的值
#!/bin/bash #auto sum 1 100 j=0 for ((i=1;i<=100;i++)) do j=`expr $i + $j` done echo $j


查看腳本執行的過程:

案例三:找到相關 log,然后批量打包
#!/bin/sh for i in `find /var/log -name '*.log'` do tar –czf 2014log.tgz $i done
find /var/log -name '*.log':查看指定目錄下以log結尾的文件

如:



解壓:

案例四:遠程主機批量傳輸文件
#!/bin/bash #auto scp files for client #by authorszgs 2020 for i in `seq 100 200` do scp -r /tmp/test.txt root@192.168.1.$i:/data/webapps/www done
5、while循環
while 條件語句 do 語句 1 done
1、案例一:while 條件判斷數字
#!/bin/sh i=1; while [[ $i -lt 10 ]];do echo $i; ((i++)); done;



案例二:擴展講解 linux read 命令
read -p "Please input number:" input

案例三:while 逐行讀取某個文件
#!/bin/sh while read line do echo $line; done < /etc/hosts


六、until循環
until 條件 do action done 直到滿足條件,才退出。否則執行 action。
案例一:條件判斷數字
#!/bin/sh a=10; until [[ $a -lt 0 ]];do echo $a; ((a--)); done;
如:


七、case選擇語句
case $arg inpattern1) 語句 1 ;; pattern2) 語句 2 ;; *) 語句 3 ;; esac


八、select 選擇語句
select 一般用於選擇菜單的創建,可以配合 PS3 來做菜單的打印輸出信息。
#!/bin/sh PS3="What you like most of the open source system?" select i in CentOS RedHat Ubuntu do echo "Your Select System: "$i Done

如:


