Bash and & or with if
And
$ cat and_or.sh
#!/bin/bash
test_and_or(){
TEST="11"
TEST2="22"
if [ "$TEST" == "11" ] && [ "$TEST2" == "22" ] ;then
echo "String Exist by And"
fi
if [ "$TEST" == "33" ] || [ "$TEST2" == "22" ] ;then
echo "String Exist by Or"
fi
}
test_and_or
$ ./and_or.sh
String Exist by And
String Exist by Or
1.3 Directory Exist
$ cat directory_ex.sh
#!/bin/bash
test_dir_exist(){
set -e
if [ -e "/home/jayleekr/workspace/00_codes/05_info_archive" ];then
echo "DIR Exist"
exit 1
fi
}
test_fail(){
mkdir /a
echo "test_fail"
}
test_dir_exist
$ ./directory_ex.sh
DIR Exist
2. set
2.1 set -e
set -e 가 script 내에 실행되어있으면 해당 스크립트가 동작하는 환경은 script명령어가 error 를 발생하면서 죽었을때 다음 명령을 수행하지 않는다
3. exit
일반적으로 unix 에서는 0 은 성공, 1255 는 error code 로 인식됨
인식되는 범위는 0255 16bit
$? 로 결과값을 확인 가능
$ cat test.sh
echo "hello"
exit 100
$ sh test.sh
hello
$ echo $?
100
4. eval
eval 뒤의 string arg 들을 command 로 실행하게 해줌 string 수준에서 최종 Command 조작 후 실행시 유용하다
5. array(list)
Bash 에서는 괄호로 Array를 표현가능하다 그 안에서 구분자는 아무래도 띄어쓰기(space bar) 이다
$ cat array_ex.sh
#!/bin/bash
lists=("a" b "c")
echo ${lists[1]}
echo ${lists[0]}
echo ${lists[3]}
echo ${lists[2]}
$ sh array_ex.sh
b
a
c
6. sed
sed Utility를 사용안해본사람은 있어도 한번만 사용한 사람은 없다 전해지는 전설의 레전드
7. while
필자는 while보다는 for문을 더 좋아하는 스타일이라 잘 쓰진않지만, 텍스트 파일로 부터 line 을 읽어들여 작업하는 용도로 좀 쓴다.
$ while read line; do git rm -r $line; done < remove.lst
8. 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
Appendix. References
Share this article
Found this helpful? Share it with your network
관련 글
Working with Bash Arrays
Bash: How to Check if Directory Exists
Bash eval - Dynamic Command Execution
Join the Discussion
Share your thoughts and connect with other readers
댓글
GitHub 계정으로 로그인하여 댓글을 남겨보세요. 건설적인 의견과 질문을 환영합니다!
댓글을 불러오는 중...
댓글을 불러올 수 없습니다.