「シェルスクリプトの基礎 - ファイル内容を1行ずつ読み込む」の版間の差分
ナビゲーションに移動
検索に移動
細 (文字列「</source>」を「</syntaxhighlight>」に置換) |
細 (文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…) |
||
75行目: | 75行目: | ||
three | three | ||
<br><br> | <br><br> | ||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:シェルスクリプト]] | [[カテゴリ:シェルスクリプト]] |
2024年10月14日 (月) 10:38時点における最新版
概要
シェルスクリプトにおいて、while read line文を使用して、ファイル内容を1行ずつ読み込ませるための方法を記載する。
標準入力へリダイレクトさせて読み込む
ファイルを標準入力へリダイレクトさせて、ファイル内容を1行ずつ読み込ませる。
具体的には、list.txtファイルの内容を読み込み、1行ずつechoコマンドで表示させる。
# list.txtファイルの内容 one two three
#!/bin/bash
while read line
do
echo $line
done < ./list.txt
# 出力 one two three
ファイルを変数に格納して読み込む
ファイル内容をcatコマンドで表示させて、それを変数に格納し、ヒアドキュメントを使用して読み込む。
具体的には、list.txtファイル内容を変数DATAに格納して、ヒアドキュメントを使用して変数の内容を読み込む。
# list.txtファイルの内容 one two three
#!/bin/bash
DATA=`cat ./list.txt`
while read line
do
echo $line
done << FILE
$DATA
FILE
# 出力 one two three
catコマンドでファイル内容を表示してパイプで渡す
ファイル内容をcatコマンドで表示させて、その結果をパイプを使用して読み込む。
具体的には、lixt.txtファイルの内容をcatコマンドで表示させて、その結果をパイプでwhile read line文に渡す。
# list.txtファイルの内容 one two three
#!/bin/bash
cat ./list.txt | while read line
do
echo $line
done
# 出力 one two three