본문으로 건너뛰기
0%
1분 미만 (72 단어) ko

Bash for and while?

Categories TechSavvy Bash
Tags #TechSavvy #ProgrammingLanguage #Bash

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

Share this article

Found this helpful? Share it with your network

Join the Discussion

Share your thoughts and connect with other readers

댓글

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

댓글을 불러오는 중...