[問題] shell script 如何比字串大小

看板Linux作者 (光佳羽)時間12年前 (2012/03/07 00:27), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/1
大家好: 最近在撰寫一個簡單的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 請問是不是我會錯意或是使用錯誤了呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.232.136

03/07 00:30, , 1F
When used with [[, The < and > operators sort lexicogr
03/07 00:30, 1F

03/07 00:31, , 2F
要用[[ ... ]]
03/07 00:31, 2F

03/07 00:32, , 3F
謝謝樓上!解決了!(居然只是因為這樣 orz....)
03/07 00:32, 3F

03/07 09:22, , 4F
[ ... ] 是sh compatible, [[ ... ]]則只有bash有
03/07 09:22, 4F

03/07 09:22, , 5F
上次為了這兩個debug了一個小時....
03/07 09:22, 5F
文章代碼(AID): #1FLZhSaR (Linux)