Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
18
Task/String-comparison/UNIX-Shell/string-comparison-1.sh
Normal file
18
Task/String-comparison/UNIX-Shell/string-comparison-1.sh
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#!/bin/sh
|
||||
|
||||
A=Bell
|
||||
B=Ball
|
||||
|
||||
# Traditional test command implementations test for equality and inequality
|
||||
# but do not have a lexical comparison facility
|
||||
if [ $A = $B ] ; then
|
||||
echo 'The strings are equal'
|
||||
fi
|
||||
if [ $A != $B ] ; then
|
||||
echo 'The strings are not equal'
|
||||
fi
|
||||
|
||||
# All variables in the shell are strings, so numeric content cause no lexical problems
|
||||
# 0 , -0 , 0.0 and 00 are all lexically different if tested using the above methods.
|
||||
|
||||
# However this may not be the case if other tools, such as awk are the slave instead of test.
|
||||
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