[Shell Script](EN) Use file or command output in while loop

Process file or result of command in while loop line by line in shell script.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)


Method Examples

Process command result line by line in while loop

  • Use Pipeline
ls ./Test | while read line
do
	echo $line
done
ls /tmp | while read line
do
	echo $line
done


Process file line by line in while loop

  • Use Redirection
while read line
do
    echo $line
done < test.txt
  • Use Pipeline
cat test.txt | while read line
do
    echo $line
done


Reference

[Linux] 쉘 스크립트에서 멀티프로세스(혹은 스레드) 기능 사용하기

> 백그라운드로 명령어를 실행해서 병렬적으로 실행되는 멀티 프로세스 환경을 만들어보자.## 환경- Linux 기반 시스템- Bash shell(/bin/bash)## 멀티프로세스? 병렬처리? 멀티스레드? 백그라운드 프로세스?- 여기서 진행할 방식...… Continue reading