纠结的bash/perl 数值/字符比较
时间:2010-11-15 来源:ubuntuer
perl 跟 bash刚好相反。。。。
Perl comparison
Refer to http://ist.marshall.edu/ist334/perl_basics.html
Bash comparison
Refer to http://www.vectorsite.net/tsshell.html
Perl comparison
Refer to http://ist.marshall.edu/ist334/perl_basics.html
Numerical Comparison | ||
---|---|---|
< | less than | $bad = $gpa < 1.5; |
<= | less than or equal | $good = 3.5 <= $gpa; |
> | greater than | $very_good = $gpa > 3.9; |
>= | greater than or equal | $not_bad = $gpa >= 2.3; |
== | equals to | $perfect = $gpa == 4; |
!= | not equal to | $not_perfect = $gpa != 4; |
<=> | compare. The result of this operator is equal to either 1, 0, or -1. It's equal to 1 if the first operand is greater than the second, zero if they are equal, and -1 if the second is greater than the first. | $comparison = $a <==> 123; |
String Comparison | ||
lt | less than | $before = $name lt "b"; |
le | less than or equal | $a_name = $name le "a"; |
gt | greater than | $after = $name gt "Tester"; |
ge | greater than or equal | $after_P = $name ge "O"; |
eq | equivalent to | $is_Bob = $name eq "Bob"; |
ne | not equal to | $not_Bob = $name ne "Bob"; |
cmp | The string comparison operator evaluates to either 1, 0, or -1. | $comparison = $name cmp "Bob"; |
Bash comparison
Refer to http://www.vectorsite.net/tsshell.html
[ "$shvar" = "fox" ] String comparison, true if match.
[ "$shvar" != "fox" ] String comparison, true if no match.
[ "$shvar" = "" ] True if null variable.
[ "$shvar" != "" ] True if not null variable.
[ "$nval" -eq 0 ] Integer test; true if equal to 0.
[ "$nval" -ge 0 ] Integer test; true if greater than or equal to 0.
[ "$nval" -gt 0 ] Integer test; true if greater than 0.
[ "$nval" -le 0 ] Integer test; true if less than or equal to 0.
[ "$nval" -lt 0 ] Integer test; true if less than to 0.
[ "$nval" -ne 0 ] Integer test; true if not equal to 0.
[ -d tmp ] True if "tmp" is a directory.
[ -f tmp ] True if "tmp" is an ordinary file.
[ -r tmp ] True if "tmp" can be read.
[ -s tmp ] True if "tmp" is nonzero length.
[ -w tmp ] True if "tmp" can be written.
[ -x tmp ] True if "tmp" is executable.
相关阅读 更多 +