shell script 如何比字串大小 - Linux

By Daph Bay
at 2012-03-07T00:27
at 2012-03-07T00:27
Table of Contents
大家好:
最近在撰寫一個簡單的shell script時遇上一個有趣的問題
這隻shell script是利用bash執行的
功能很簡單:就是比較兩個字串的字典順序大小
經過man bash後得到訊息
string1 == string2
string1 = string2
True if the strings are equal. = should be used with the test
command for POSIX conformance.
string1 != string2
True if the strings are not equal.
string1 < string2
True if string1 sorts before string2 lexicographically.
string1 > string2
True if string1 sorts after string2 lexicographically.
於是我寫下類似以下的code
====
if [ "$str1" < "$str2"]; then
.....
else
....
fi
====
然而它總是在判斷上失敗,並且將 < 符號認定為 I/O redirect
請問是不是我會錯意或是使用錯誤了呢?
--
最近在撰寫一個簡單的shell script時遇上一個有趣的問題
這隻shell script是利用bash執行的
功能很簡單:就是比較兩個字串的字典順序大小
經過man bash後得到訊息
string1 == string2
string1 = string2
True if the strings are equal. = should be used with the test
command for POSIX conformance.
string1 != string2
True if the strings are not equal.
string1 < string2
True if string1 sorts before string2 lexicographically.
string1 > string2
True if string1 sorts after string2 lexicographically.
於是我寫下類似以下的code
====
if [ "$str1" < "$str2"]; then
.....
else
....
fi
====
然而它總是在判斷上失敗,並且將 < 符號認定為 I/O redirect
請問是不是我會錯意或是使用錯誤了呢?
--
Tags:
Linux
All Comments

By Kyle
at 2012-03-10T14:40
at 2012-03-10T14:40

By Anthony
at 2012-03-15T03:24
at 2012-03-15T03:24

By Adele
at 2012-03-15T16:45
at 2012-03-15T16:45

By Leila
at 2012-03-19T02:20
at 2012-03-19T02:20
Related Posts
如何讓兩台mail server同步?

By Elma
at 2012-03-06T23:39
at 2012-03-06T23:39
選擇 centos 或 debian

By Jack
at 2012-03-06T23:28
at 2012-03-06T23:28
pcmanx-gtk2 1.1

By Erin
at 2012-03-06T23:27
at 2012-03-06T23:27
Ubuntu live CD

By Joseph
at 2012-03-06T22:34
at 2012-03-06T22:34
apache2 access.log 出現不明網址

By Quanna
at 2012-03-06T21:22
at 2012-03-06T21:22