shell腳本
shell腳本介紹
shell腳本就是一些命令的集合,能幫助我們更方便管理服務器
shell腳本的創建和執行
自定義腳本都放在sbin目錄下
# cd /usr/local/sbin
撰寫第一個腳本
# vi first.sh
腳本內容
#! /bin/bash 表示該檔案使用的是bash語
## This is my first shell script. #表示注釋## Writen by kei 2020-03-31 date
echo "Hello world!" 執行腳本內容 # sh first.sh # ./first.sh 使用這個命令是要先給腳本x權限
查看腳本執行程序
# sh -x first.sh

命令date
date的常用用法
date +%Y 表示以四位數字格式列印年份
date +%y 表示以兩位數字格式列印年份
date +%m 表示月份
date +%d 表示日期
date +%H 表示小時
date +%M 表示分鐘
date +%S 表示秒
date +%w 表示星期(結果顯示為0表示周日)

shell腳本中的變數
定義變數的格式為“變數名=變數的值”,在腳本中參考變數時需要加上符號$
撰寫一個與變數相關的腳本
# vi variable.sh
腳本內容
#! /bin/bash ## In this script we will use variables.## Writen by kei 2020-03-31 d=`date +%H:%M:%S` 反引號的作用是將引號中的字串當成shell命令執行
echo "The script begin at $d."
echo "Now we'll sleep 2 seconds."
sleep 2
d1=`date +%H:%M:%S`
echo "The script end at $d1." d和d1在腳本中作為變數 執行腳本 # sh variable.sh
數學運算
# vi sum.sh
腳本內容
#! /bin/bash ## For get the sum of two numbers.## Writen by kei 2020-03-31 a=1
b=2
sum=$[$a+$b] 數學計算要用[ ]括起來,而且前面要加符號$
echo "$a+$b=$sum"
執行腳本
# sh sum.sh

和用戶互動
# vi read.sh
腳本內容
#! /bin/bash ## Using 'read' in shell script.## Kei 2020-03-31 read -p "Please input a number: " x read命令把用戶輸入的字串作為變數值
read -p "Please input another number: " y
sum=$[$x+$y]
echo "The sum of the two numbers is: $sum" 執行腳本 # sh read.sh
查看執行程序
# sh -x read.sh

shell腳本預設變數
shell腳本在執行時,后面可以跟一個或者多個引數
# vi option.sh
腳本內容
#! /bin/bash sum=$[$1+$2] $1和$2為腳本的預設變數 echo "sum=$sum" 執行腳本 # sh -x option.sh 1 2
修改腳本,$0代表腳本本身的名字
# vi option.sh
腳本內容
#! /bin/bash echo "$1 $2 $0" 執行腳本 # sh option.sh 1 2
shell腳本中的邏輯判斷
不帶else
格式
if 判斷陳述句; then
command
fi
撰寫腳本
# vi if1.sh
腳本內容
#! /bin/bash read -p "Please input your score: " aif ((a<60)); then
echo "You didn't pass the exam."
fi 執行腳本 # sh if1.sh
帶有else
格式
if 判斷陳述句; then
command
else
command
fi
撰寫腳本
# vi if2.sh
腳本內容
#! /bin/bash read -p "Please input your score: " aif ((a<60)); then
echo "You didn't pass the exam."
else
echo "Good! You passed the exam."
fi 執行腳本
# sh if2.sh

帶有elif
格式
if 判斷陳述句1; then
command
elif 判斷陳述句2;then
command
else
command
fi
撰寫腳本
# vi if3.sh
腳本內容
#! /bin/bash read -p "Please input your score: " aif ((a<60)); then 判斷數值大小除了可以用(())的形式外,還可以用[ ]
echo "You didn't pass the exam."
elif ((a>=60)) && ((a<85)); then &&表示并且,||表示或者
echo "Good! You passed the exam."
else
echo "Very good! Your score is very high!"
fi
執行腳本
# sh if3.sh

常用符號
-lt 小于
-gt 大于
-le 小于或等于
-ge 大于或等于
-eq 等于
-ne 不等于
和檔案有關的判斷
if還常用于判斷檔案的屬性
-e 判斷檔案或目錄是否存在
-d 判斷是不是目錄以及是否存在
-f 判斷是不是普通檔案以及是否存在
-r 判斷是否有讀權限
-w 判斷是否有寫權限
-x 判斷是否可執行
格式
if [ -e filename ]; then
command
fi
case邏輯判斷
格式
case 變數 in
value1) 不限制value的個數
command
;;
value2)
command
;;
*) *代表其他值
command
;;
esac
撰寫腳本
# vi case.sh
腳本內容
#! /bin/bash read -p "Input a number: " na=$[$n%2]
case $a in $a的值為1或0
1)
echo "The number is odd."
;;
0)
echo "The number is even."
;;
*)
echo "It's not a number!"
;;
esac
執行腳本
# sh case.sh

shell腳本中的回圈
常用的回圈有for回圈和while回圈
for回圈
格式
for 變數名 in 回圈的條件;do
command
done
回圈的條件可以是一組字串或者數字,也可以是一條命令的執行結果
# vi for.sh
腳本內容
#! /bin/bash for i in `seq 1 5`; doecho $i
done
執行腳本
# sh for.sh
回圈的條件還可以參考系統命令的執行結果,但需要用反引號括起來
# for file in `ls`; do echo $file; done

while回圈
用冒號:代替回圈條件時可以做到死回圈
格式
while 條件; do
command
done
撰寫腳本
# vi while.sh
腳本內容
#! /bin/bash a=5while [ $a -ge 1 ]; do
echo $a
a=$[$a-1]
done
執行腳本
# sh while.sh

shell腳本中的函式
shell腳本中的函式就是先把一段代碼整理到一個小單元中,并給這個小單元命名,當用到這段代碼時直接呼叫這個小單元的名字即可
格式
function 函式名()
{
command1
command2
}
撰寫腳本
# vi func.sh
腳本內容
#! /bin/bash a=5while [ $a -ge 1 ]; do
echo $a
a=$[$a-1]
done
[root@localhost sbin]# cat func.sh
#! /bin/bash function sum()
{
sum=$[$1+$2]
echo $sum
} sum $1 $2 執行腳本 # sh func.sh 1 2
shell腳本中的中斷和繼續
break
break用在回圈中,表示退出該層回圈 撰寫腳本 # vi break.sh 腳本內容 #! /bin/bash for i in `seq 1 5`do
echo $i
if [ $i == 3 ] 當i等于3時會跳出回圈
then
break
fi
echo $i
done
echo aaaaaa 執行腳本 # sh break.sh
contiune
在回圈中,contiue表示退出本次回圈 撰寫腳本 # vi contiune.sh 腳本內容 #! /bin/bash for i in `seq 1 5`do
echo $i
if [ $i == 3 ] 當i等于3時,結束本次回圈
then
continue
fi
echo $i
done
echo $i 執行腳本 # sh continue.sh
exit
exit表示直接退出腳本
編寫腳本
# vi exit.sh
腳本內容
#! /bin/bash for i in `seq 1 5`do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
echo aaaaaaa
執行腳本
# sh exit.sh

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/115511.html
標籤:Linux
