1. 程式人生 > 其它 >Shell指令碼迴圈讀取文字、獲取時間格式時間、函式傳參.md

Shell指令碼迴圈讀取文字、獲取時間格式時間、函式傳參.md

技術標籤:Linuxshelllinux指令碼語言

迴圈讀取文字

filepath = "/root/temp/啟動程序.txt"
echo $filepath
while read line
do
  echo $line
done < $filepath
filepath="/root/temp/啟動程序.txt"
echo $filepath
while read line
do
  echo $line
done < $filepath

注意到:filepath="/root/temp/啟動程序.txt"空格的緣故:linux shell中自動變數名和=間不可有空格

,否則執行指令碼時,就會出現“未找到該命令”的錯誤。

其中while do … … done < filepath是將檔案重定向給while迴圈進行操作

獲取指定時間格式時間

date_alarm=`date +"%Y%m%d%H%M"`

輸出標準時間格式,注意date和“+”號之間要有空格

傳參和函式

function sayHello
{
        first_word=$1
        second_word=$2
        third_word=$3
        echo "第一個引數: $first_word 第二個引數: $second_word
第三個引數: $third_word"
} sayHello first second third

在這裡插入圖片描述

可以注意到直接採用$數字的方法直接在呼叫函式後面對應指定的引數即可。