LoginSignup
3
4

More than 5 years have passed since last update.

ファイルやコマンドの実行結果を入力として1行ずつ処理する

Posted at

ファイルやコマンドの実行結果を入力として1行ずつ処理する

どちらも、サンプルはあるのですが、両方がまとめて書かれていることが
無かったのでまとめてメモしました。

どちらもechoで出力しているだけなので、このままだと意味はまったく無いですが
条件に当てはまるファイルのみリネーム場合などで使いました。

コマンドの実行結果を1行ずつ処理する場合

a.sh
#!/bin/sh
ls *sh| while read line
do
 echo $line
done 

実行結果

~ $ ./a.sh 
a.sh
b.sh

ファイルに出力された実行結果を1行ずつ処理する場合

b.sh
#!/bin/sh

while read line
do
    echo "$line"
done < ${1}

実行結果

~ $ ls *sh > b.txt
~ $ cat b.txt
a.sh
b.sh
~ $ ./b.sh b.txt 
a.sh
b.sh
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4