update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
39
Task/String-comparison/UNIX-Shell/string-comparison-2.sh
Normal file
39
Task/String-comparison/UNIX-Shell/string-comparison-2.sh
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#!/bin/bash
|
||||
|
||||
isint() {
|
||||
printf "%d" $1 >/dev/null 2>&1
|
||||
}
|
||||
|
||||
compare() {
|
||||
local a=$1
|
||||
local b=$2
|
||||
|
||||
[[ $a = $b ]] && echo "'$a' and '$b' are lexically equal"
|
||||
[[ $a != $b ]] && echo "'$a' and '$b' are not lexically equal"
|
||||
|
||||
[[ $a > $b ]] && echo "'$a' is lexically after '$b'"
|
||||
[[ $a < $b ]] && echo "'$a' is lexically before '$b'"
|
||||
|
||||
shopt -s nocasematch # Turn on case insensitivity
|
||||
|
||||
[[ $a = $b ]] && echo "'$a' and '$b' are equal with case insensitivity"
|
||||
|
||||
shopt -u nocasematch # Turn off case insensitivity
|
||||
|
||||
# If args are numeric, perform some numeric comparisions
|
||||
if isint $a && isint $b
|
||||
then
|
||||
[[ $a -eq $b ]] && echo "$a is numerically equal to $b"
|
||||
[[ $a -gt $b ]] && echo "$a is numerically greater than $b"
|
||||
[[ $a -lt $b ]] && echo "$a is numerically less than $b"
|
||||
fi
|
||||
|
||||
echo
|
||||
}
|
||||
|
||||
|
||||
compare foo foo
|
||||
compare foo bar
|
||||
compare FOO foo
|
||||
compare 24 123
|
||||
compare 50 20
|
||||
Loading…
Add table
Add a link
Reference in a new issue