bash에서 스트링 비교할 때, 그냥 compare 기호만 쓰는 줄 알았다. 결과가 제대로 안나와서 확인도중 놀라운 사실을 발견하였다.

 

 

문자 비교시 compare 기호앞에 back slash(\)을 써야 한다는 것!!

또는 if expression 문에 '[[' 또는 ']]'을 써야 한다.

 

 

 var1=ims-google-admin

 

if [ "$var1" \> "ims" ]
then
    echo "1"
else
    echo "2"
fi

 

 

 

 

출처

http://debid.vlsm.org/share/LDP/abs/html/comparison-ops.html

 

 

string comparison

=

is equal to

if [ "$a" = "$b" ]

==

is equal to

if [ "$a" == "$b" ]

This is a synonym for =.

The == comparison operator behaves differently within a double-brackets test than within single brackets.

[[ $a == z* ]]    # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]]  # True if $a is equal to z* (literal matching).

[ $a == z* ]      # File globbing and word splitting take place.
[ "$a" == "z*" ]  # True if $a is equal to z* (literal matching).

# Thanks, Stephane Chazelas

!=

is not equal to

if [ "$a" != "$b" ]

This operator uses pattern matching within a [[ ... ]] construct.

<

is less than, in ASCII alphabetical order

if [[ "$a" < "$b" ]]

if [ "$a" \< "$b" ]

Note that the "<" needs to be escaped within a [ ] construct.

>

is greater than, in ASCII alphabetical order

if [[ "$a" > "$b" ]]

if [ "$a" \> "$b" ]

Note that the ">" needs to be escaped within a [ ] construct.

See Example 26-11 for an application of this comparison operator.

-z

string is "null", that is, has zero length

-n

string is not "null".

'unix and linux' 카테고리의 다른 글

shell에서 하루 전 날짜 구하기  (0) 2007.10.09
bash에서의 String(스트링) 비교  (0) 2007.10.09
Bash에서 숫자 비교  (0) 2007.10.09
Bash 간단한 내용 정리  (0) 2007.10.09
zagent 좀비 프로세스.  (0) 2007.10.08
Posted by '김용환'
,