learn shell第二課
一、字符串操作
1.1 字符串長(zhǎng)度
$STRING='this is a string'
$echo ${#STRING}#"#"號(hào)往往與長(zhǎng)度或者個(gè)數(shù)有關(guān),比如$#就是返回傳入腳本的參數(shù)個(gè)數(shù)
#16
1.2 索引
檢測(cè)一個(gè)字符串里面的字母首次出現(xiàn)在另外一個(gè)字符串的位置
$STRING='this is a string'
$SUB_STRING='hat'
$expr index "$STRING" "$SUB_STRING"
#1
#1是hat當(dāng)中的t首次在STRING當(dāng)中出現(xiàn)的序號(hào)
1.3 切片
索引用${STRING:N:M}的方式切片,N是開(kāi)始的位置,M是結(jié)束的位置
$echo ${STRING:3:7}
#s is a
1.4 子字符串替換
$STRING="to be or not to be"
$echo "${STRING[@]/be/eat}"
#to eat or not to be
字符串替換方法可以用${STRING[@]/replaced/new_item}這樣的結(jié)構(gòu)去替換,其中STRING[@]代表取出STRING里面的所有元素,也就是所有字母。
${STRING[@]/replaced/new_item}默認(rèn)替換第一個(gè),要全部替換,用${STRING[@]//replaced/new_item}
$echo "${STRING[@]//be/eat}"
#to eat or not to eat
$echo "${STRING[@]/#to be/eat}"#替換前面出現(xiàn)的to be
#eat or not to be
echo "${STRING[@]/%be/eat}"#替換后面出現(xiàn)的be
#to be or not to eat
二、判斷語(yǔ)句
判斷語(yǔ)句的基本語(yǔ)法結(jié)構(gòu)是:
if [ judgement ];then
code if the judgement is true
fi
在這里要注意幾點(diǎn),一個(gè)是if在結(jié)束的時(shí)候是有fi的代表finish;另外如果then和if同行的話,要加分號(hào),然后還有judgement里面的判斷,注意跟前后括號(hào)都有空格,另外里面的判斷是跟之前的操作符是一樣的,操作符前后也要有空格。
示例:
NAME="John"
if [ "$NAME" = "John" ];then
echo "His name is John"
fi
#His name is John
另外也可以加入elif還有else其他條件
NAME="Peter"
if [ "$NAME" = "John" ];then
echo "His name is John"
elif [ "$NAME" = "Mary" ];then
echo "His name is Mary"
else
echo "No"
fi
#No
數(shù)字比較的一些邏輯語(yǔ)句
comparison Evaluated to true when $a -lt $b $a < $b $a -gt $b $a > $b $a -le $b $a <= $b $a -ge $b $a >= $b $a -eq $b $a is equal to $b $a -ne $b $a is not equal to $b
字符串比較的邏輯語(yǔ)句
comparison Evaluated to true when "$a" = "$b" $a is the same as $b "$a" == "$b" $a is the same as $b "$a" != "$b" $a is different from $b -z "$a" $a is empty
2.2 case結(jié)構(gòu)
case var in
judgement1) do something;;
judgement2) do something;;
judgement3) do something;;
judgement4) do something;;
my_case=1
case "$my_case" in
1) echo "hello";;
2) echo "my name"
esac
#hello
在這里要注意與case相對(duì)應(yīng)的是要寫esac符號(hào)退出,另外每個(gè)操作之間要加;;兩個(gè)分號(hào)分開(kāi)。跟變量var有關(guān)的判斷語(yǔ)句judgement或者condition要寫完整,上面如果是判斷是否等于則可以不用寫完整。
三、循環(huán)
3.1 遍歷
基本結(jié)構(gòu)
for N in array;do
do something;
done
# loop on array member
NAMES=(Joe Jenny Sara Tony)
for N in ${NAMES[@]} ; do
let i+=1
echo "The number ${i} is $N"
done
#The number 1 is Joe
#The number 2 is Jenny
#The number 3 is Sara
#The number 4 is Tony
注意${NAMES[@]}是取這個(gè)整個(gè)陣列的元素集合,而${NAMES}是取這個(gè)陣列作為整體的一個(gè)變量
3.2 while循環(huán)
while循環(huán)的結(jié)構(gòu)
while condition ; do
do something
done
count=1
while [ "$count" -lt 4 ] ; do
echo "The current number is $count"
let count+=1
done
#The current number is 1
#The current number is 2
#The current number is 3
我們還可以增加break還有continue語(yǔ)句來(lái)控制,跟python一樣
count=1
while [ $count -lt 10 ] ; do
if [ $count -gt 7 ] ; then
break
elif [ $(($count % 2)) -eq 0 ] ; then
echo "The current number is $count"
else
continue
fi
count=$(($count+1))
done
四、shell函數(shù)
shell函數(shù)的結(jié)構(gòu)
function function_name {
commands
}
五、特殊變量
在shell腳本當(dāng)中有一些特殊變量
-
$0- The filename of the current script.| -
$n- The Nth argument passed to script was invoked or function was called.| -
$#- The number of argument passed to script or function.| -
$@- All arguments passed to script or function.| -
$*- All arguments passed to script or function.| -
$?- The exit status of the last command executed.| -
$$- The process ID of the current shell. For shell scripts, this is the process ID under which they are executing.| -
$!- The process number of the last background command.|
六、trap命令
trap命令來(lái)對(duì)某個(gè)信號(hào)進(jìn)行輸出
trap <argv>/<function> <signal>
信號(hào)就是用戶一些中斷程序運(yùn)行的操作,或者是磁盤滿了之類的錯(cuò)誤使得程序無(wú)法繼續(xù)往下運(yùn)行,這樣的一些信號(hào)就給了程序信號(hào)??梢栽诮K端輸入kill -l查看所有的信號(hào)類型。也可以參考tutorialspoint的這篇文章。


我們?cè)?code>traptest.sh這個(gè)文件當(dāng)中鍵入以下代碼:
#!/bin/bash
# notice you cannot make Ctrl-C work in this shell,
# try with your local one, also remeber to chmod +x
# your local .sh file so you can execute it!
trap func SIGINT SIGTERM
echo "it's going to run until you hit Ctrl+Z"
echo "hit Ctrl+C to be blown away!"
function func {
echo "Booh"
}
while true
do
sleep 60
done

我們?cè)诎聪耤trl+c取消運(yùn)行的時(shí)候會(huì)有提示。說(shuō)明這個(gè)signal被捕捉到了。
七、文件檢測(cè)
我們還可以用腳本檢測(cè)文件在當(dāng)前路徑的一些狀態(tài)。
使用判斷語(yǔ)句
<- command> <filename1>
<filename1> <- command> <filename2>
比如-e檢測(cè)文件是否存在
#!/bin/bash
filename=$1#這里接收第一個(gè)參數(shù)作為文件名
if [ -e "$filename" ];then
echo "$filename exists"
fi
我們?cè)囈幌逻\(yùn)行

類似的,-d檢測(cè)某個(gè)路徑是否存在,-r檢測(cè)文件是否可讀。