본문으로 건너뛰기
0%

Bash for and while?

카테고리

for

$ cat for.sh 
#!/bin/bash

LISTS=("a" "b" "c") 

# Don't forget Brace!
echo ${LISTS[0]}
echo ${LISTS[1]}
echo ${LISTS[2]}
# Don't forget Brace!
for item in ${LISTS[@]}
do
    echo $item
done
$ ./for.sh
a
b
c
a
b
c

아래는 find, eval, sed 를 함께 쓴 예제

for file in $(find . -name "*.txt")
do
    echo $file
    eval sed -i "/confidential/d" $file # confidential 이 포함된 라인 지우기
done

while

필자는 while보다는 for문을 더 좋아하는 스타일이라 잘 쓰진않지만, 텍스트 파일로 부터 line 을 읽어들여 작업하는 용도로 좀 쓴다.

$ while read line; do git rm -r $line; done < remove.lst

Appendix. References

댓글 남기기

여러분의 생각을 들려주세요

댓글

GitHub 계정으로 로그인하여 댓글을 남겨보세요. 건설적인 의견과 질문을 환영합니다!

댓글을 불러오는 중...