Linux學習記錄--shell script

shell script

shell script是利用shell的功能所寫的一個程序,這個程序使用純文本文件,將一些shell的語法和命令寫在裏面,搭配正則表達式,管道命令與數據流重定向等功能,達到咱們想要的目的java

shell script執行

直接命令執行

shell script文件必須具有rx的權限,假設my.sh在 /root下node

絕對路徑

[root@bogon ~]# /root/my.sh正則表達式

相對路徑

[root@bogon ~]# . /my.shshell

bash(sh)命令執行

shell script文件必須具有r的權限bash

root@bogon ~]# sh my.shless


source或.命令執行

source或. 命令執行與上面執行不一樣,上面執行shell 都會分配一個子進程來進行,source或. 命令就在本進程執行,也就是說一些非環境變量也能讀取到ide

root@bogon ~]# source my.sh函數


shell script編寫

前面提到shell script中能夠使用shell命令,那麼shell script格式是怎樣的?測試


shellscript文件 my.shthis

#!/bin/bash

#This is my first shell script

echo "hello world"


第1行:必需要以#!/bin/bash開頭表明這個文件內的語法使用bash的語法

第2行:註釋以#開頭

第3行:shell 命令



shell script重要功能

test測試命令

用來檢測系統上面某些文件或者相關的屬性


測試的標誌

表明意義

1. 關於某個文件名的[文件類型]判斷,如 test -e  filename 表示存在否

-e

該[文件名]是否存在?(經常使用)

-f

該[文件名]是否存在且爲文件(file)?(經常使用)

-d

該[文件名]是否存在且爲目錄(directory)?(經常使用)

-b

該[文件名]是否存在且爲一個 block device 裝置?

-c

該[文件名]是否存在且爲一個 character device 裝置?

-S

該[文件名]是否存在且爲一個 Socket 文件?

-p

該[文件名]是否存在且爲一個 FIFO (pipe) 文件?

-L

該[文件名]是否存在且爲一個連結檔?

2. 關於文件的權限偵測,如 test -r filename 表示可讀否 (但 root 權限常有例外)

-r

偵測該文件名是否存在且具備[可讀]的權限?

-w

偵測該文件名是否存在且具備[可寫]的權限?

-x

偵測該文件名是否存在且具備[可運行]的權限?

-u

偵測該文件名是否存在且具備[SUID]的屬性?

-g

偵測該文件名是否存在且具備[SGID]的屬性?

-k

偵測該文件名是否存在且具備[Sticky bit]的屬性?

-s

偵測該文件名是否存在且爲[非空白文件]?

3. 兩個文件之間的比較,如: test file1 -nt file2

-nt

(newer than)判斷 file1 是否比 file2 新

-ot

(older than)判斷 file1 是否比 file2 舊

-ef

判斷 file1 與 file2 是否爲同一文件,可用在判斷 hard link 的斷定上。 主要意義在斷定,兩個文件是否均指向同一個  inode 哩!

4. 關於兩個整數之間的斷定,例如 test n1 -eq n2

-eq

兩數值相等 (equal)

-ne

兩數值不等 (not equal)

-gt

n1 大於 n2  (greater than)

-lt

n1 小於 n2 (less  than)

-ge

n1 大於等於 n2 (greater  than or equal)

-le

n1 小於等於 n2  (less than or equal)

5. 斷定字串的數據

test -z string

斷定字串是否爲 0 ?若 string 爲空字串,則爲 true

test -n string

斷定字串是否非爲 0 ?若 string 爲空字串,則爲 false。
 注: -n 亦可省略

test str1 = str2

斷定 str1 是否等於 str2 ,若相等,則回傳 true

test str1 != str2

斷定 str1 是否不等於 str2 ,若相等,則回傳 false

6. 多重條件斷定,例如: test -r filename -a -x filename

-a

(and)兩種狀況同時成立!例如  test -r file -a -x file,則 file 同時具備 r 與 x 權限時,纔回傳  true。

-o

(or)兩種狀況任何一個成立!例如 test -r file -o -x file,則 file 具備 r 或 x 權限時,就可回傳  true。

!

反相狀態,如 test ! -x file ,當 file 不具備 x 時,回傳 true


舉例

shellscript文件內容:


#!bin/bash
read -p "please input file/dir name: " name
echo "your input is $name"
test ! -e $name && echo "file is not exist! "&&exit 1
test -d $name &&echo "file is directory"
test -f $name &&echo "file is regulate file"
test -r $name &&echo "you can read this file"
test -w $name &&echo "you can write this file"
test -x $name &&echo "you can exec this file"
exit 0


執行結果

[root@localhost scripts]# sh test.sh
please input file/dir name: n
your input is n
file is not exist!
[root@localhost scripts]# sh test.sh
please input file/dir name: sh01.sh
your input is sh01.sh
file is regulate file
you can read this file
you can write this file
you can exec this file
[root@localhost scripts]# ll sh01.sh
-rwxr--r-- 1 root root 48 03-07 10:15 sh01.sh


測試命令

[]測試命令和test用法一直,只是使用時要注意空格符使用

 在中括號內的每一個組件又要有空格符分割

 在中括號內的變量常量都要使用「」括起來


舉例

shellscript文件內容:

#!/bin/bash
read -p "please input y/n: " yn
[ -z "$yn" ] &&echo "your input is empty"&&exit 1
[ "$yn" == "y" -o "$yn" == "Y" ]&&echo "it is ok"&&exit 0
[ "$yn" == "n" -o "$yn" == "N" ]&&echo "it is not ok"&&exit 0
echo "you input is $yn .please use input (y/n)"
exit 2


執行結果

[root@localhost scripts]# sh [].sh
please input y/n:
your input is empty
[root@localhost scripts]# sh [].sh
please input y/n: Y
it is ok
[root@localhost scripts]# sh [].sh
please input y/n: A
you input is A .please use input (y/n)

執行參數

能夠在執行shell script時添加參數

sh var.sh first second third

      $1   $2   $3

其中 first 做爲第一個參數存儲在$1中,依次類推。


幾個重要的參數:

$#:獲取參數的數量

$@:獲取參數的總體內容


舉例

shellscript文件內容:

#!/bin/bash
echo "total parameter number is : $#"
echo "whole parameter is : $@"
echo "1st  parameter is : $1"
echo "2st  parameter is : $2"

執行結果

[root@localhost scripts]# sh var.sh first second third
total parameter number is : 3
whole parameter is : first second third
1st  parameter is : first
2st  parameter is : second


if…..then

語法:

If [條件判斷式 ] ;then

 待執行的shell 命令

fi


舉例

shellscript文件內容:

#!/bin/bash
read -p "please input y/n: " yn
if [ -z "$yn" ];then
echo "your input is empty"
exit 1
fi
if [ "$yn" == "y" -o "$yn" == "Y" ];then
echo "it is ok"
exit 0
fi
if [ "$yn" == "n" -o "$yn" == "N" ];then
echo "it is not ok"
exit 0
fi
exit 2


執行結果

[root@localhost scripts]# sh ifthen.sh
please input y/n:
your input is empty
[root@localhost scripts]# sh ifthen.sh
please input y/n: Y
it is ok
[root@localhost scripts]# sh ifthen.sh
please input y/n: N
it is not ok

if …else

語法:

If [條件判斷式 ] ;then

 待執行的shell 命令

else

 待執行的shell 命令

fi

if …elif..else

語法:

If [條件判斷式 ] ;then

待執行的shell 命令

elif[ 條件判斷式 ] ;then

待執行的shell 命令

else

待執行的shell 命令

fi


case …esac

語法:

case$變量 in

「第一個變量內容」)

待執行的shell 命令

;;

「第n個變量內容」)

待執行的shell 命令

;;

*) è最後一個變量內容. *表明全部

待執行的shell 命令

;;

esac


舉例

shellscript文件內容:

#!/bin/bash
case $1 in
"hello")
echo " hello how are you!"
;;
"")
echo "you must input parameter!"
;;
*)
echo "parameter is hello,yours is $1"
;;
esac


執行結果

[root@localhost scripts]# sh case.sh
you must input parameter!
[root@localhost scripts]# sh case.sh hello
hello how are you!
[root@localhost scripts]# sh case.sh he
parameter is hello,yours is he

函數功能

語法:

functionfname()

{

程序段

}


舉例

shellscript文件內容:

#!/bin/bash
function print()
{
if [ -z "$1" ];then
  echo "你沒有輸入參數"
else
  echo -n "你輸入的是 $1"
fi
}
function returnval()
{
  return 3
}
case $1 in
"one")
print 1
;;
"return")
returnval
echo "return value is $?"
;;
"nopara")
print
;;
esac


執行結果

[root@localhost scripts]# sh function.sh one
你輸入的是 1
[root@localhost scripts]# sh function.sh nopara
你沒有輸入參數
[root@localhost scripts]# sh function.sh return
return value is 3

函數後面也能夠添加參數$0記錄函數名,$1記錄參數1,依次類推

函數也能夠具備返回值,返回值內容經過$?查看

while do done

語法:

while[ 條件表達式 ] =>只要條件知足就執行

do

程序段

done


舉例

shellscript文件內容:

declare -i sum=0;
declare -i num=0
while [ $num -le 100 ]
do
sum=sum+num
num=num+1
done
echo "總和是: $sum"


執行結果

[root@localhost scripts]# sh while.sh
總和是: 5050 

until do done

語法:

while[ 條件表達式 ] =>只要條件不知足就執行

do

程序段

done

for do done

語法:

for varin con1 con2….

for varin conarr

for (初始值;限制值;執行步長)


舉例

shellscript文件內容:

#!/bin/bash
userarr=$(cat /etc/passwd | cut -d ':' -f 1|head -n 5)
for user in $userarr
do
echo "user is : $user"
done
for char in A B C
do
echo "char is $char"
done
sum=0
for ((i=1; i<=$1; i++))
do
sum=$(($sum+$i))
done
echo "1+2..+$1=$sum"

執行結果

[root@localhost scripts]# sh for.sh 100
user is : root
user is : bin
user is : daemon
user is : adm
user is : lp
char is A
char is B
char is C
1+2..+100=5050

shell script追蹤與調試

語法:sh [-nvx]  xxx.sh

選項與參數:

-n:不執行,僅檢查語法錯誤

-v:在執行前,輸出script內容

-x:將使用到script內容顯示到屏幕,追蹤主要靠這個


舉例:

[root@localhost scripts]# sh -x function.sh nopara
+ case $1 in
+ print
+ '[' -z '' ']'
+ echo $'\344\275\240\346\262\241\346\234\211\350\276\223\345\205\245\345\217\202\346\225\260'
你沒有輸入參數
[root@localhost scripts]# sh -x function.sh one
+ case $1 in
+ print 1
+ '[' -z 1 ']'
+ echo '你輸入的是 1'
你輸入的是 1
相關文章
相關標籤/搜索