sh_shell的基礎語法2

sh_shell的基礎語法2

shell腳本轉義字符的替換

shell解釋器默認是不會對轉義字符的進行自動替換的
需要加入-e 纔會進行替換的

#!/bin/sh
a=10
echo "this is $a\n" #不轉義
echo -e "this is $a\n" #進行自動轉義

result:

this is 10\n
this is 10

轉義字符表

轉義字符 轉義含義
\ 反斜槓
\a 警報
\b 退格
\f 換頁
\n 換行
\r 回車
\t 水平製表符
\v 垂直製表符

shell的運算符

原生的Bash是不支持的簡單的數學運算符的,但是它是支持算數,關係,布爾,字符串,文件測試運算符

數學運算符是通過expr的命令完成的

算數運算符

#!/bin/sh
a=1
b=2
test=`expr $a + $b`
echo "a+b:$test"
test=`expr $a - $b` 
echo "a-b:$test"
test=`expr $a \* $b` #乘法符號需要轉義
echo "a*b:$test"
test=`expr $a / $b`
echo "a/b:$test" 
test=`expr $a % $b`
echo "a%b:$test"

result:

a+b:3
a-b:-1
a*b:2
a/b:0
a%b:1

關係運算符

關係運算符只支持數字

#!/bin/sh
a=10
b=20
if [ $a -eq $b ]      #檢查兩個數是否相等,相等back true
then
   echo "a -ep b:true"
else
   echo "a -eq b:false"
fi
if [ $a -ne $b ]      #檢查兩個數是否相等,不相等back true 
then
   echo "a -ne b:true"
else
   echo "a -ne b:false"
fi
if [ $a -gt $b ]      #檢測左邊的數是否大於右邊  大於 back true
then
   echo "a -gt b:true"
else      
   echo "a -gt b:false"
fi  
if [ $a -lt $b ]     #檢測左邊的數是否小於右邊  小於 back true
then      
   echo "a -lt b:true"
else
   echo "a -lt b:false"
fi
if [ $a -ge $b ]     #檢測左邊的數是否大於等於右邊  大於等於 back true
then
   echo "a -ge b:true"
else
   echo "a -ge b:false"
fi
if [ $a -le $b ]    #檢測左邊的數是否小於等於右邊  小於等於 back true
then
   echo "a -le b:true"
else
   echo "a -le b:false"
fi

result:

a -eq b:false
a -ne b:true
a -gt b:false
a -lt b:true
a -ge b:false
a -le b:true

布爾運算符

布爾運算是數字符號化的邏輯推演法,其運算符就是實現的中間媒介

運算符 說明
! 非運算
-o 或運算
-a 與運算

exp1

#!/bin/sh
a=100
b=200
if [ $a -lt 80 -a $b -gt 400 ]   #與運算
then
   echo "true"
else
   echo "false"
fi
if [ $a -gt 80 -o $b -gt 400 ]    #或運算
then
   echo "true"
else
   echo "false"
fi

result:

false
true

字符串運算符

運算符 說明
= 檢測兩個字符串是否相等,相等返回 true
!= 檢測兩個字符串是否相等,不相等返回 true
-z 檢查字符串長度是否爲0
str 檢查字符串是否爲空

code

#!/bin/sh
$str1="abc"
$str2="abc"
if [ $str1 = $str2 ]  #判斷字符串是否相等 
then      
   echo "str1等於str2"
else      
   echo "str1等於str2"
fi      
if [ -z $str1 ]      #判斷字符串長度是否爲0
then         
   echo "the lengrh of str1 is not 0"
else
   echo "the lengrh of str1 is 0"
fi  
if [ $str1 ]        #判斷字符串是否爲bull
then
   echo "the str1 is not null"
else 
   echo "the str1 is null"
fi 

result

str1等於str2
the lengrh of str1 is not 0
the str1 is null

文件操作符

sh的文件操作符是對Linux的文件進行相關的操作

文件測試運算符列表
運算符 說明
[ -b $file ] 檢測文件是否是塊設備文件,如果是,則返回 true
[ -c $file ] 檢測文件是否是字符設備文件,如果是,則返回 true
[ -d $file ] 檢測文件是否是目錄,如果是,則返回 true
[ -f $file ] 檢測文件是否是普通文件(既不是目錄,也不是設備文件),如果是,則返回 true
[ -g $file ] 檢測文件是否設置了 SGID 位,如果是,則返回 true
[ -k $file ] 檢測文件是否設置了粘着位(Sticky Bit),如果是,則返回 true
[ -p $file ] 檢測文件是否是具名管道,如果是,則返回 true
[ -u $file ] 檢測文件是否設置了 SUID 位,如果是,則返回 true
[ -r $file ] 檢測文件是否可讀,如果是,則返回 true
[ -w $file ] 檢測文件是否可寫,如果是,則返回 true
[ -x $file ] 檢測文件是否可執行,如果是,則返回 true
[ -s $file ] 檢測文件是否爲空(文件大小是否大於0),不爲空返回 true
[ -e $file ] 檢測文件(包括目錄)是否存在,如果是,則返回 true

exp

#!/bin/sh
file="testfile"
if [ -s $file ]   # 判斷文件是否存在
then
   echo "File exists"
else     
   echo "File doesn't exist"
fi

result:

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