现在的位置: 首页 > 综合 > 正文

-ne 和 !=的比较

2014年06月05日 ⁄ 综合 ⁄ 共 592字 ⁄ 字号 评论关闭
#!/bin/bash

a=4
b=5

#  Here "a" and "b" can be treated either as integers or strings.
#  There is some blurring between the arithmetic and string comparisons,
#+ since Bash variables are not strongly typed.

#  Bash permits integer operations and comparisons on variables
#+ whose value consists of all-integer characters.
#  Caution advised, however.

echo

if [ "$a" -ne "$b" ]
then
  echo "$a is not equal to $b"
  echo "(arithmetic comparison)"
fi

echo

if [ "$a" != "$b" ]
then
  echo "$a is not equal to $b."
  echo "(string comparison)"
  #     "4"  != "5"
  # ASCII 52 != ASCII 53
fi

# In this particular instance, both "-ne" and "!=" work.

echo

exit 0

 

4 is not equal to 5
(arithmetic comparison)

4 is not equal to 5.
(string comparison)

 

 

抱歉!评论已关闭.