Linux 於 Bash Shell 要讀取檔案, 並一行一行印出, 或者對每一行作些處理, Shell script 要如何寫?
Shell 讀取檔案並一行一行印出
- #!/bin/bash
- filename='examples.desktop'
- exec < $filename
- while read line
- do
- echo $line # 一行一行印出內容
- echo 'a' $line # 印出 "a $line" 此行的內容, 可另外作特別處理.
- done
- #!/bin/bash
- filename='examples.desktop'
- while IFS='' read -r line || [[ -n "$line" ]]; do
- echo "a $line" # 一行一行印出內容,前面加上 a
- done < $filename
- #!/bin/bash
- filename='examples.desktop'
- IFS=$'\n'
- for line in `cat filename`;
- do
- echo $line;
- done
相關網頁
- Using exec
- An exec <filename command redirects stdin to a file.
- exec < data-file # stdin replaced by file "data-file"