1. 程式人生 > >shell指令碼的那點小事兒--shell指令碼語言輸入輸出與檔案操作(三)

shell指令碼的那點小事兒--shell指令碼語言輸入輸出與檔案操作(三)

內容一:檔案包含

在shell指令碼中匯入其他shell指令碼

語法1: ./filename 這裡./filename相當於執行了filename指令碼

檔案A->fileA.sh

#!/bin/bash
echo "我是檔案A"

檔案B->fileB.sh

#!/bin/bash
./fileA.sh #匯入檔案A
echo "我是檔案B"

語法2:

檔案A->fileA.sh 與語法1一致

檔案B->fileB.sh修改如下

#!/bin/bash
source fileA.sh #或者這樣的方式source ./fileA.sh
echo "I am File-B"

內容二:cat命令

cat命令:

  1. 作用一:檢視檔案內容
  2. 作用二:連線檔案
  3. 作用三:建立檔案(一個或者多個)
  4. 作用四:重定向輸出到終端
  5. 作用五:重定向輸出到檔案

語法:cat [選項][檔案] -> cat [option][file]

e.g NO.1

在Mac終端輸入

cat fileA.sh #輸出檔案內容
cat fileB.sh #輸出檔案內容

e.g NO.2 -n

cat -n fileA.sh #輸出檔案和行號
cat -n fileB.sh #輸出檔案和行號

e.g NO.3 -b

cat -b fileA.sh #輸出檔案和行號,不輸出空白行
cat -b fileB.sh #輸出檔案和行號,不輸出空白行

e.g NO.4 -e

cat -e fileA.sh #輸出檔案,在每一行的後面加美元符$,需要將多行內容轉換成一行的時候使用

e.g NO5

#控制檯輸入cat,然後enter進入輸入模式,然後在控制輸入任何字元,控制檯會同時列印
#類似復讀機一樣(下述成為復讀模式,方便理解)
cat #接收標準的輸入,並且輸出標準

按control + c退出,復讀模式

內容三:獲取輸入 read命令

寫法一:

read用於接收鍵盤輸入或者其他方式的輸入

#!/bin/bash
echo "please your name"
read name
echo "you are ${name}"

寫法二: -p 提示語

#!/bin/bash
read -p "please your name" name
echo "you are ${name}"
#-p 後面跟字串表示 提示語句,類似於佔位符placeholder

寫法三: 超時禁止輸入 -t

#!/bin/bash
if read -t 5 -p "please input your name" name
then
    echo "you are ${name}"
else
    echo "time out"
fi
#-t 可以理解延遲輸入,5為5秒,意思接受5秒內的輸入內容,超過5秒執行else內容

寫法四: -s 不顯示出入內容

#!/bin/bash
read -s -p "please input your password" pwd
echo -e "\nyour password is ${pwd}"
#-s 是密文輸入,用於密碼一類的文字輸入
#${pwd}獲取當前路徑

寫法五:從檔案中讀取內容

#!/bin/bash
cat -n fileA.sh | while read line
do
    echo "${line}"
done
# | shell指令碼中的管道語法,前者的結果作為後者的輸入
# cat -n fileA.sh 從fileA.sh中讀出到檔案內容作為輸入被read來接收
# while read line 迴圈獲取read接收到的輸入,每次迴圈得到內容用line來接收

 

內容四:printf輸出

1.printf和echo的區別

  • printf 不能自動換行,echo 自動換行
  • printf 一般用於格式列印,echo 用於標準的輸出

2.printf的語法結構

  • printf format-string 引數列表

3.案例

1)輸出 %-10s 寬度為10的字元,-表示左對齊

#!/bin/bash
printf "%-10s %-8s %-4s\n" 姓名 性別 體重kg
printf "%-10s %-8s %-4.2f\n" Tom 男 100
printf "%-10s %-8s %-4.2f\n" Jam 男 70
printf "%-10s %-8s %-4.2f\n" Alice 男 75
printf "%-10s %-8s %-4.2f\n" Alun 男 65

2)輸出printf format-string

#!/bin/bash
#用printf列印字串
printf "%d %s\n" 1 "Avalanching"
printf '%d %s\n' 1 'Avalanching'
printf "%d %s\n" 1 Avalanching
printf %d 1
printf %s Avalanching

內容五:shell中的函式

基本語法

#!/bin/bash
function function-name () {
    code
}

function-name param1 param2 ....

#關鍵字function 函式名字 () { 程式碼 }
#呼叫直接用 函式名字 空格 引數1 空額 引數2 空格 引數3 
#可以傳多個引數,引數和引數,引數和方法名之間用空格隔開

e.g

#!/bin/bash
function func() {
    echo "|--begin"
    count=!{#}
    if [ count > 0 ]
    then
        for value in ${@}
        do
            echo "|--|--${value}"
        done
    else

        echo "|--|--null"
    fi
    echo "|--over"
    return 100
}

echo "before"
func $1 $2
reuslt=$?
echo "come back value:${reuslt}"
echo "over"

終端呼叫:
./fileA.sh tom ava

輸出結果:
before
|--begin
|--|--tom
|--|--ava
|--over
come back value:100
over

#$?為func的返回值,shell指令碼中,將函式內部的返回值傳給了$?,而無法直接使用result=func $1 $2來獲取
#[email protected]獲取到一個引數列表 

總結:在shell函式return的值是賦值給$?的,而變數接收的值是函式中的echo的輸出值。

內容六:輸入輸出重定向

1.輸入重定向

語法:

終端輸入:wc 檔名

wc fileA.sh

輸出:14 52 283 fileA.sh

  • 引數1->14:文字的行數
  • 引數2->52:文字的單詞數(空格分開,或換行作為一個單詞)
  • 引數3->283:位元組數(B)

1.1.1"<"輸入重定向

1.1.2 "<<"建立檔案

2.輸出重定向

2.1.1 ">"輸出的重定向

將方向指向一個檔案,將檔案中的內容刪除,寫入新的內容

e.g

在fileB.sh輸入

#!/bin/bash
echo "this is fileB"
echo "I have a Dream" > fileA.sh

執行結果:fileA.sh檔案中原文字被替換成 I have a Dream

2.1.2 ">>"追加

將方向指向一個檔案,將檔案中的原內容後面寫入新的內容

在fileB.sh輸入

#!/bin/bash
echo "this is fileB"
echo "I have a Dream,too" >> fileA.sh
執行結果:fileA.sh檔案中原文字後追加 I have a Dream,too

應用:檔案編輯,日誌列印,建立臨時檔案

注意:以上的縮排是為了方便檢視層級關係,如果在執行程式碼中報錯(not found command),請去除縮排,注意if後的空格