-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteststrings.sh
More file actions
58 lines (48 loc) · 1.42 KB
/
teststrings.sh
File metadata and controls
58 lines (48 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
#
# This script do the following:
#
# 1. Tests to see if the first string is of zero length, and if the other is of non-zero length,
# telling the user of both results.
# 2. Determines the length of each string, and reports on which one is longer or if they are of equal length.
# 3. Compares the strings to see if they are the same, and reports on the result.
# check two string arguments were given
[[ $# -lt 2 ]] && \
echo "Usage: Give two strings as arguments" && exit 1
str1=$1
str2=$2
# test command
echo "Is string 1 zero length? Value of 1 means FALSE"
[ -z "$str1" ]
echo $?
# note if $str1 is empty, the test [ -z $str1 ] would fail
# but [[ -z $str1 ]] succeeds
# i.e., with [[ ]] it works even without the quotes
echo "Is string 2 nonzero length? Value of 0 means TRUE"
[ -n $str2 ]
echo $?
# comparing the length of two string
len1=${#str1}
len2=${#str2}
echo length of string1 = $len1, length of string2 = $len2
if [ $len1 -gt $len2 ]
then
echo "String 1 is longer than string 2"
else
if [ $len2 -gt $len1 ]
then
echo "String 2 is longer than string 1"
else
echo "String 1 is the same length as string 2"
fi
fi
# compare the two strings to see if they are the same
if [[ $str1 == $str2 ]]
then
echo "String 1 is the same as string 2"
else
if [[ $str1 != $str2 ]]
then
echo "String 1 is not the same as string 2"
fi
fi