linux系統中整數值比較語句

2022-06-08 14:00:16 字數 1640 閱讀 2489

1、-eq 表示等於  

[root@linuxprobe test]# [ 3 -eq 4

][root@linuxprobe test]# echo $?

1[root@linuxprobe test]# [

3 -eq 3

][root@linuxprobe test]# echo $?

0

注:linux系統中的整數比較不能有小數

[root@linuxprobe test]# [ 3.5 -eq 4

] ## 報錯

-bash: [: 3.5: integer expression expected

2、-ne表示不等於

[root@linuxprobe test]# [ 3 -ne 5

][root@linuxprobe test]# echo $?

0[root@linuxprobe test]# [

3 -ne 3

][root@linuxprobe test]# echo $?

1

3、-gt表示大於

[root@linuxprobe test]# [ 3 -gt 6

][root@linuxprobe test]# echo $?

1[root@linuxprobe test]# [

3 -gt 2

][root@linuxprobe test]# echo $?

0

4、-ge表示大於等於

[root@linuxprobe test]# [ 3 -ge 2

][root@linuxprobe test]# echo $?

0[root@linuxprobe test]# [

3 -ge 3

][root@linuxprobe test]# echo $?

0[root@linuxprobe test]# [

3 -ge 4

][root@linuxprobe test]# echo $?

1

5、-lt表示小於

[root@linuxprobe test]# [ 3 -lt 5

][root@linuxprobe test]# echo $?

0[root@linuxprobe test]# [

3 -lt 1

][root@linuxprobe test]# echo $?

1

6、-le表示小於等於

[root@linuxprobe test]# [ 3 -le 4

][root@linuxprobe test]# echo $?

0[root@linuxprobe test]# [

3 -le 3

][root@linuxprobe test]# echo $?

0[root@linuxprobe test]# [

3 -le 2

][root@linuxprobe test]# echo $?

1

注:整數比較運算子僅是對數字的操作,不能和字串、小數等進行比較

Linux 數值與字元比較

把字串當成整型進行比較,由於abcd等字元對不上0123當程式嘗試去轉成二進位制時無法完成轉換,所以用於數值比較的運算不能用於字串比較 但是把整型當成字串進行比較,0123這些數值完全可以轉成ascii碼而且原本兩個整型相等則變成ascii碼還是相等 不等的還是不等,所以可用於字串的比較也可用於整型...

c中數值的比較

呼叫兩次printf函式 麻煩!include int main void 對比 呼叫一次printf函式 更簡單 簡潔!includeint main void 比較三個整數的大小,並顯示!includeint main void 比較三個值中的最小值,並顯示。includeint main vo...

Linux測試比較語句

測試和比較語句用於if或指令碼命令中 if condition then commands else if conditon then commandsfi或 condition command 如果condition為真則執行command condition command 如果conditio...