아무리 잘 만든 것도 포장이 초라하면, 알아봐주는 경우가 드물다.

포장 또한 표현하는 기술 아닐까?


'Life > A sentense of the day' 카테고리의 다른 글

공식vs비공식  (0) 2017.07.12
작가의 숨은 의도  (0) 2017.07.09
예측  (0) 2017.07.04
  (0) 2017.07.03
선택과 집중  (0) 2017.07.03

Bash Script 에서 Script 에 관한 문법


Bash Script 에서 따로 자료형을 입력하지 않아도, initial value 가 string 형태의 경우 string 자료형으로 인식한다.


1. String Length

${#string} : string 의 길이 

expr length $string


2. Substring

${string:position} : string 의 앞에서 부터 position 의 위치 부터 string의 끝까지의 substring (position은 0부터 시작한다.)

${string:position:length} : string 의 앞에서 부터 position 위치 부터 length 길이의 substring


참조

http://tldp.org/LDP/abs/html/string-manipulation.html


'Programming > Bash Script' 카테고리의 다른 글

Bash Script Variable Split by Space  (0) 2018.02.04
Bash Script Echo  (0) 2018.02.04
Bash Script If statement  (0) 2017.07.04
Bash script argument  (0) 2017.06.20
Bash script sudo 권한 체크  (1) 2017.06.19

Bash Script If statement

Bash 스크립트 if 조건문 문법

기본적으로, if / elif / else / fi 로 구성한다. 

if[] bracket 사이에는 공백이 있어야 하며, [] bracket 과 [[ ]] double bracket( ) paranthesis 는 다를 수 있으므로 주의하자.

if [ expression ]

then

statement

elif [ expression ]

then

statement

else 

statement

fi


expression 에 사용하는 Comparison 은 아래와 같다.

integer comparison

if [ "$a" -eq "$b" ]  : 같음(equal to)

if [ "$a" -ne "$b" ]  : 다름(not equal to)

if [ "$a" -lt "$b" ]  : 작음(less than)

if (( "$a" < "$b" ))

if [ "$a" -gt "$b" ] : 큼(greater than)

if (( "$a" > "$b" ))

if [ "$a" -le "$b" ] : 작거나 같음(less than or equal to)

if (( "$a" <= "$b" ))

if [ "$a" -ge "$b" ] : 크거나 같음(greater than or equal to)

if (( "$a" >= "$b" ))


string comparison

if [ "$a" == "$b" ]  : 같음(equal to)

if [ "$a" = "$b" ] ※ = 양쪽 모두 빈 칸 이여야 함

if [ "$a" != "$b" ]  : 다름(not equal to)

if [ -z "$a" ] : string 이 null 이 아님(즉, 0이 아닌 길이가 있는 string)


if statement 에 not 을 사용하고자 하는 경우 ! 를 사용하면 된다.

if ! [ "$arg" -eq "matching" ]

then

echo "Not matched with the first one"

fi


If statement 를 사용해 file 존재 유무 확인

if [ -d <DIR> ] : Directory 가 존재

if [ -f <FILE> ] : 파일이 존재하며, directory 가 아님

if [ -x <FILE> ] : 파일이 실행 가능함

if [ -f test.c ]

then

echo "File test.c exists!"

else

echo "File test.c does not exist"

fi


If statement  Logical AND, OR 문법

if [[ condition1 && condition2 ]] : Logical AND

if [ condition1 ] && [ condition2 ]

if [ condition1 -a condition2 ]

if [[ condition1 || condition2 ]] : Logical OR

if [ condition1 ] || [ condition2 ]

if [ condition1 -o condition2 ]

if [[ condition1 && condition2 ]] || [[ condition3 && condition4 ]]

then

echo "Condition (1 AND 2) OR (3 AND 4)"

fi



참조

http://tldp.org/LDP/abs/html/comparison-ops.html

'Programming > Bash Script' 카테고리의 다른 글

Bash Script Variable Split by Space  (0) 2018.02.04
Bash Script Echo  (0) 2018.02.04
Bash Script String  (0) 2017.07.04
Bash script argument  (0) 2017.06.20
Bash script sudo 권한 체크  (1) 2017.06.19

+ Recent posts