一:認識 shell script 的作用:
(1):運行方式:
1:直接命令下達:shell.sh文件必須要具備可讀與可運行(rx)的權限。
•絕對路徑:使用/home/dmtsai/first.sh來下達命令。
•相對路徑:假設工作目錄在/home/dmtsai/,則使用./first.sh來運行。
•變量“PATH”功能:將first.sh放在PATH指定的目錄內,如~/bin/
2:以bash程序來運行:通過“bash first.sh”或“sh first.sh”來運行。
3:使用-n及-x來檢查與追蹤shell文件的語法是否正確。
4:編寫第一個shell script:
運行:
[root@server1 dir]# sh first.sh Hi Mr dengan
[root@server1 dir]# ./first.sh
Hi Mr dengan
[root@server1 dir]# bash first.sh
Hi Mr dengan
[root@server1 dir]# echo $? //通過exit 1:來表達當程序正確執行完后給系統傳回一個1,保存在$?中.
1
export PATH :是將其設為全局變量,
#!/bin/bash //在宣告這個script使用的shell名稱。
PATH:可讓這個程序在運行時直接執行一些外部命令,而不必寫絕對路徑。
(2):輸入:
session.sh
#!/bin/bash #Introduce: #User inputs his name and age.program show it in screen. #History: #2020/3/15 PATH=/bin:/sbin/:/usr/bin/:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "please input your name:" name #提示使用者輸入 read -p "Input your age:" age echo -e "\a\n hello $name" echo -e "Your age is $age" exit 1 ~
操作:
[root@server1 dir]# sh session.sh
please input your name:deng
Input your age:12
hello deng
Your age is 12
二:使用判斷式:
(1):&&和||的基本用法: cmd1 && cmd2 1,若cmd1執行完畢之后且正確執行($?=0),則開始執行cmd2。 2,若cmd2執行完畢之后且執行錯誤($? not equal 0),則cmd2不執行。 cmd1 || cmd2 1,若cmd1執行完畢之后且正確執行,則不執行cmd2. 2,若cmd2執行完畢之后錯誤執行,則開始執行cmd2 [dengzhaoxu@server1 ~]$ test -e /dmtsai && echo "exit" || echo "Not exit" Not exit
(2):test命令
test:命令的選項: -e 判斷該文件名(目錄)是否存在 , -f :判斷該文件名(目錄)是否存在且為文件名 , -d :判斷該文件名(目錄)是否存在且為目錄名.
-r:判斷該文件是否存在且為讀文件, -u:判斷該文件是否存在且具有SUID特殊權限. -s:判斷該文件是否存在且為非空文件.
-nt:判斷file1是否比file2新, eq:判度兩個整數是否相等.
le:小於等於,ge:大於等於,lt小於
例子:
-rw-r--r--. 1 0 0 342 3月 15 22:56 ~
-rwxrwxrwx. 1 0 0 239 3月 15 22:38 first.sh
-rw-r--r--. 1 0 0 21 3月 15 22:28 path
-rw-r--r--. 1 0 0 0 3月 15 22:29 pwd
-rwxrwxrwx. 1 0 0 342 3月 15 23:01 session.sh
-rwxrwxrwx. 1 0 0 0 3月 11 16:28 test
[dengzhaoxu@server1 ~]$ ls -a dir
~ . .. first.sh path pwd session.sh test
[dengzhaoxu@server1 ~]$ cat session
cat: session: 沒有那個文件或目錄
[dengzhaoxu@server1 ~]$ cat session.sh
cat: session.sh: 沒有那個文件或目錄
[dengzhaoxu@server1 ~]$ vim session.sh
[dengzhaoxu@server1 ~]$ test path -nt pwd
[dengzhaoxu@server1 ~]$ test path -nt pwd && echo "exit" || echo "no"
no
[dengzhaoxu@server1 ~]$ test 2 -eq 2 && echo "yes" || echo "no" //判斷兩個整數是否相等.
yes
[dengzhaoxu@server1 ~]$ test 2 -ne 3 && echo "yes" || echo "no" //判斷兩數值不想等。
yes
[dengzhaoxu@server1 ~]$ str=deng
[dengzhaoxu@server1 ~]$ str1=deng1
[dengzhaoxu@server1 ~]$ test $str1 = $str && echo "yes" || echo "no" //判斷字符串是否相等.
no
[dengzhaoxu@server1 ~]$ test -r path -a -x path && echo "yes" || echo "no" //-a判斷兩種狀況是否同時成立,若同時成立則返回true
no
[dengzhaoxu@server1 ~]$ test ! -x path && echo "yes" || echo "no" //當path文件不具備x權限時返回true.
yes
(3):test3.sh:執行該shel script
[root@server1 dir]# cat test3.sh
#!/bin/bash
#Program:
#User input a filename,program will check the flowing:
#History:
#2018/08/25
PATH=/bin:/sbin/:usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "please input a fileame,I will check the filename's type and \permission.\n\n"
read -p "Input a filename:" filename
test -z $filename && echo "The filename '$filename' DO NOT exit" && exit 0
#開始判斷文件屬性和類型
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="perm writable"
#開始輸出信息
echo "The filename :$filename is a $filetype"
echo "And the permission are :$perm:"
[root@server1 dir]# sh test3.sh
please input a fileame,I will check the filename's type and \permission.
Input a filename:test2
The filename :test2 is a regulare file
And the permission are :perm writable:
(4):使用[]判斷符合:
[root@server1 dir]# name="deng"
[root@server1 dir]# [ $name == "deng1" ]&& echo "yes" || echo "no" //
n
三:使用條件判斷式:
test3.sh
#!/bin/bash #Program: #This program shows the user's choice #History: #2018/08/25 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input(Y/N):" yn if[ "$yn "== "Y" ] || [ "$yn" == "y" ];then echo "OK,continue" exit 0 fi if[ "$yn" == "N" ]||[ "$yn" == "n" ];then echo "Oh,interrupt!" fi echo "I don't know what your choice is" && exit 0
test5.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input(Y/N):" yn
if[ "$yn"=="Y" ]||[ "$yn"=="y" ];then
echo "OK,continue"
elif[ "$yn"=="N" ]||[ "$yn"=="n" ];then
echo "Oh,interrupt!"
else
echo "i don't konow"
fi
四:使用date命令和了解腳本在不同的環境下運行的差異:
(1):使用bash(sh)來運行腳本時,該腳本是在一個新的bash環境下運行,那么該腳本中的變量也同樣在不同的空間中.實質上他是在子程序bash中。
(2):source 運行腳本則在父程序bash中運行,那么變量也會在父空間中.
代碼:
#!/bin/bash #Program: #實現:每天都會i創建一個不同的文件,即根據日期的不同來創建文件. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "I will use 'touch' command to create three files" read -p "please input your filename:" filename #為了避免用戶隨意按enter鍵,利用變量功能分析文件名是否設置. #filename = ${fileuser:-"filename"} #利用不同的日期來創建不同的文件名 date1=$(date --date='2 days ago' +%Y%m%d) #獲取前兩天的日期 date2=$(date --date='1 days ago' +%Y%m%d) #前一天的日期 date3=$(date +%Y%m%d) #今天的日期 #設置文件名 file1=${filename}${date1} file2=${filename}${date2} file3=${filename}${date3} #創建文件 touch "$file1" touch "$file2" touch "$file3" ~ ~
1:在父程序bash中運行
root@server1 dir]# source test6.sh
I will use 'touch' command to create three files
please input your filename:fuu
[root@server1 dir]# echo $file1
fuu20200328
[root@server1 dir]# echo $file2
fuu20200329
2:在子程序bash中運行 #說明當來到父程序bash中時子程序中運行的腳本中的變量不能輸出。
[root@server1 dir]# sh test6.sh
I will use 'touch' command to create three files
please input your filename:fuu1
[root@server1 dir]# echo $file1 //沒有輸出fuu120200328
fuu20200328
3:date的使用:
昨天:
date -d'-1 days' +%Y%m%d
date -d'1 days ago' +%Y%m%d
前天:
date -d'-2 day' +%Y%m%d
date -d'2 days ago' +%Y%m%d
明天:
date -d'+1 day' +%Y%m%d
date -d'+1 day next' +%Y%m%d
上個月:
date -d'-1 month' +%Y%m%d-%H%M%S
下個月:
date -d'+1 month' +%Y%m%d-%H%M%S
上個周:
date -d'-1 week' +%Y%m%d-%H%M%S
五:使用declare來定義變量的類型,利用$((計算式))來進行數值運算.
[root@server1 dir]# a=4 [root@server1 dir]# b=5 [root@server1 dir]# declare -i total=$a*$b [root@server1 dir]# echo total total [root@server1 dir]# echo $total 20 [root@server1 dir]# echo $((13%3)) 1 [root@server1 dir]#