Shell腳本學習之路---02

31.read:從鍵盤讀取輸入

$read variable
$echo $variable
$read -p "Enter value: " value #-p:代表prompt,顯示提示語。

如果未在read命令後面指定變量,那麼會將輸入值存儲到默認的變量REPLY中

#!/bin/bash
echo "where do you stay ?"
read
echo "you stay in $REPLY"

read後面也可以跟多個變量,那麼鍵盤輸入的也會按照空格進行分開,並依次填入到各個變量中去。

#!/bin/bash
echo "What is your name?"
read fname mname lname
echo "Your first name is : $fname"
echo "Your middle name is : $mname"
echo "Your last name is : $lname"

read也可以將內容存到一個數組中:

read -a cities
echo "Name of city is ${cities[2]}"

如果希望用戶按enter鍵,可以用read命令後面跟一個無用的變量。

echo "please press enter to proceed further"
read temp
echo "now backup operation wil be started!"

32.here操作符。

語法格式:
將2個here之間的內容作爲命令cat的輸入。here可以替換爲任意的分界符,cat也可以替換爲其他命令。

$cat << here
sss
fff
here

例子:
執行一個壓縮備份操作,並將操作結果發送到指定郵箱。

 1 tar -cvf /Users/liudong/tmp.gz  /Users/liudong/tmp  2>/dev/null
  2 [ $? -eq 0 ] && status="Success" || status="Failed"
  3 #send email to administrator
  4 mail -s 'Backip status' [email protected] <<end_of_msg
  5 The backup job finished.
  6 End date: $(date)
  7 Status:$status
  8 end_of_msg

文本文件字符串替換功能:
將flowers.txt中的Rose替換爲Lily。ed爲一個系統內的一個文本編輯器。

  1 cat flowers.txt
  2 ed flowers.txt << quit
  3 ,s/Rose/Lily/g
  4 w
  5 q
  6 quit
  7 cat flowers.txt

如何在here操作中使用字面量的方式顯示一些系統變量,如USER PATH NAME

  1 filename="test1"
  2 cat << 'Quoted_End_Marker'
  3 When we add quotes before and after here document marker,we can     include variables Such as $USER, $PATH, $name and similar
  4 Quoted_End_Marker

here結合<<<使用:

wc -w <<< 'Good Morning and have a nice day !' #別忘了這個單引號

33.wc:統計並顯示文本文件的行數 單詞數 字符數

34.文件操作符相關。

將文件與文件描述符進行綁定:
write描述符:

  1 exec 3> sample_out.txt #將sample_out.txt作爲輸出文件,並將文件描述符3分配給該文件。
  2 echo "This is a test message for sample_out.file file" >&3 #寫內容到文件描述符3中
  3 date >&3 # 將命令的輸出寫入到文件中
  4 exec 3<&- #關閉與文件描述符相關聯的文件。

read描述符:

exec 3< sample_out.txt
cat <&3
exec 3<&-

同時作爲write read描述符:

  1 file_name="sample_out.txt"
  2 exec 3<> $file_name
  3 echo """
  4  Do not dwell in the past,
  5  do not dream of the future,
  6  concentrate the mind on thre presetn moment. - Buddha
  7 """ >&3
  8 exec 3>&-

讀取本機信息的例子:

  1 cp /etc/hosts hosts2
  2 grep -v '^#' hosts2 > hosts3
  3 exec 3< hosts3
  4 exec 4> hostsfinal
  5 read <& 3 address1 name_1 extra_info
  6 read <& 3 address2 name_2 extra_info
  7 echo $name_1 $address1 >& 4
  8 echo $name_2 $address2 >& 4
  9 exec 3<&-
 10 exec 4<&-

read write命令總結:

Command What it does
exec command 該命令將取代當前shell環境並執行,不會再返回到當前shell環境了。
exec > data.txt 打開data.txt並作爲標準輸出
exec < data.txt 打開data.txt並作爲標準輸入
exec 3< data.txt 打開data.txt作爲輸入,描述符設置爲3
sort <&3 對data.txt執行sort操作
exec 4>data.txt 打開data.txt作爲輸出,描述符設置爲4
ll>&4 命令ll的輸出被重定向到data.txt
exec 6<&5 建立一個描述符5的拷貝6
exec 4&- 關閉文件描述符4(不論是read還是write)

35、調試模式.

通過設置執行參數-n,可以讓shell不執行而是隻進行語法檢查。
設置執行參數-v,可以讓shell在執行每條命令前先打印命令內容。

參數值 含義
bash -n script_name 只解釋並分析語法錯誤,而不執行
bash -v script_name 顯示腳本中的命令
bash -x script_name 顯示腳本命令的執行堆棧,類似於函數調用的堆棧
bash -xv script_name 同時打開-x -v
bash +xv script_name 關閉掉-x -v參數

36.set命令。

設置腳本參數的生效範圍。

#-x參數只是在執行這一段代碼時生效,參數可以是-f -v -n,含義與上面一致。
set -x
section of script
set +x

37、算術運算相關。

任何時候,我們在腳本里面聲明一個變量,默認情況下,shell都是把該變量當做一個字符串。

declare -i value #聲明一個整型變量
運算符語法格式:
$ value=4 + 4 # error
$ value=4+4#yes
$ value="4 + 4" #yes
$ value=4.5# error,不能將浮點數賦值給整數。

38.let:算術命令。

使用let命令,就不需要單獨聲明某個變量爲整型了。

  1 value=6
  2 let value=value+1
  3 echo $value
  4
  5 let "value=value+4"
  6 echo $value
  7
  8 let "value+=1"
  9 echo $value

39.expr:也是用來執行算術命令的。

expr 40 + 2 #42
expr 40+2# 40+2 如果想expr作用於算術運算,那麼後面的表達式中的數字與運算符之間要留空格。
expr 40 / 10 # 4
expr 40 * 2 #error *要通過轉義才被識別爲乘號
expr 40 \* 2 # 80

例子:

  1 x=5
  2 y=2
  3 z=`expr $x + $y`
  4 echo $z

40、算術運算表達式。

兩種格式都行:
$(( expression ))
[expression]expr[ expression ] 和expr表達式的明顯區別就是不用在變量前面加符號。

  1 x=5
  2 y=2
  3 z=$(( x + y ))
  4 echo $z
  1 x=5
  2 y=2
  3 z=$[ x + y ]
  4 echo $z

稍微複雜點的表達式:如何計算計算a的b次方

a=5
b=3
c=$[ $a ** $b ] # 這個就表示a的b次方
echo $c

其他例子:

B=10
A=$[ B + 10]
echo $A
echo $[ 3 + 4 -5 ]

算術運算的優先級控制

echo $[(3 + 4) * 5]

涉及到交互運算的腳本:

  1 echo "enter first value"
  2 read number_1
  3 echo "enter second value"
  4 read number_2
  5 total=`expr $number_1 + $number_2`
  6 echo $total
  7 sum=$((number_1 + number_2))
  8 echo "sum is $sum"
  9 echo "sum is $[ number_1 + number_2]"

判斷一個輸入數字是奇數還是偶數。

echo "Please enter a value"
read x
#三種運算書寫方式。
y=`expr $x%2`
#y=$[ x%2 ]
#y=$((x%2))
if test $y -eq 0
then
echo "Entered number is even"
else
echo "Entered number is odd"
fi

41.數字的不同進製表示。

variable=base#number-in-that-base
#十進制21的不同表示方法
x=2#10101
x=8#25
x=16#15

42.浮點數運算。

在bashshell中,只能進行整數運算,如果要進行浮點數運算,就需要藉助其他工具,如awk,bc

$ echo "scale=2;15/2" | bc
output:7.50 # scale代表設置的精度,既小數點後的位數

$bc #輸入bc,會在當前shell中進入bc的命令行狀態。
((83.12+32.13)*37.3)

43.$?

用來獲取上一條shell命令的執行結果。也包括操作是否成功或者邏輯結果是否爲true.

$ x=10;y=20;((x>y));echo $?
output:1

44.test

判斷name的值是否爲Ganesh
$ test $name = Ganesh
$ echo $?

也可以替換爲[[]]

$ [[ $name = Ganesh]]
$ echo $?

$ [[ $name = [Gg]???? ]] #通配符匹配
$ echo $?

45.&& ||短路邏輯運算符

$ [[ $name == Ganesh && $friend == "John"]]
$ [ $name == Ganesh ] && [ $friend == "John"]

46.字符串比較的關鍵字

Test operator Test true if
-n string 字符串長度是否不爲0.
-z string 字符串長度是否爲0
string1 != string2 兩個字符串是否不相等
string1 == string2 或者 string1 = string2 兩個字符串是否相等
string1 > string2 或者 string1 < string2 比較字符串的大小

注意:= != 與字符串之間的空格不能省略。

47.數值的比較。

i1:integer_1
i2:integer_2

Test operator Test true if
[ i1 -eq i2 ] i1與i2相等
[ i1 -ne i2 ] i1與i2不相等
[ i1 -gt i2 ] i1大於i2
[ i1 -ge i2 ] i1大於或等於i2
[ i1 -lt i2 ] i1小於i2
[ i1 -le i2 ] i1小於等於i2
[[ i1 > i2 ]] i1小於i2
[[ i1 != i2 ]] i1與i2不相等
[[ i1 == i2 ]] i1與i2相等
[[ i1 && i2 ]] 邏輯與
[[ i1 || i2 ]] 邏輯或
test i1 -eq i2 使用test關鍵字

備註:[] [[]]可以使用test關鍵字替代

48.文件 目錄相關命令。

Test operator Tests true if
-b file_name 是否爲塊文件
-c file_name 是否爲字符文件
-d file_name 目錄是否存在
-e file_name 文件是否存在
-f file_name 文件爲普通文件,非目錄
-G file_name 文件存在,並且屬於組用戶
-L file_name 文件是否爲符號鏈接
-r file_name 文件可讀
-s file_name 文件大小不爲0
-w/x file_name 文件可寫.可執行

49.文件比較命令。

Test operator Tests true if
[ file_1 -nt file_2] 文件1比文件2新
[ file_1 -ot file_2] 文件1比文件2舊
[ file_1 -ef file_2] 文件1和文件2的inode相同

50.字符串邏輯測試。

Test operator tests true if
[ string_1 -a string_2 ] string_1和string_2都是true
[ string_1 -o string_2 ] string_1或者string_2都是true
[ !string_1 ] string_1未match到
[[ pattern_1 && pattern_2 ]] pattern_1和pattern_2都是true
[[ pattern_1 || pattern_2 ]] pattern_1或者pattern_2都是true
[[ !pattern_1 ]] pattern_1未匹配到
字符串通配符匹配
name=Ganesh
[[ $name == [Gg]anesh ]] 
[[ $name == [Gg]anesh && friend == "Lydia" ]]
echo $?

shopt -s extglob
city=Kannur
[[ $city == [Kk]a+(n)ur ]]
echo $?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章