1

Greetings fellow UNIX&Linux users. I have an inquiry in regards to some code I have written in the bash script. My program was supposed to do the following:

Write a script that will read two strings from the user. The script will perform three operations on the two strings:

(1) Use the test command to see if one of the strings is of zero length and if the other is of non-zero length, telling the user of both results. (2) Determine the length of each string and tell the user which is longer or if they are of equal length. (3) Compare the strings to see if they are the same. Let the user know the result.

  6 #(1) Use the test command to see if one of the strings is of zero length and if the other is
  7 #of non-zero length, telling the user of both results.
  8 #(2) Determine the length of each string and tell the user which is longer or if they are of
  9 #equal length.
 10 #(3) Compare the strings to see if they are the same. Let the user know the result.
 11 
 12 echo -n "Hello user, please enter String 1:"
 13 read string1
 14 echo -n "Hello User, please enter String 2:"
 15 read string2
 16 
 17 myLen1=${#string1} #saves length of string1 into the variable myLen1
 18 myLen2=${#string2} #saves length of string2 into the variable myLen2
 19 
 20 if [ -z $string1 ] || [ -z $string2 ]; then
 21         echo "one of the strings is of zero length"
 22 
 23 else
 24         echo "Length of The first inputted string is: $myLen1"
 25         echo "Length of The second inputted string is: $myLen2"
 26 
 27 fi
 28 
 29 if [ $myLen1 -gt $myLen2 ]; then #Determine if string1 is of greater length than string2
 30         echo "The First input string has a greater text length than the Second input string."
 31         exit 1
 32 elif [ $myLen2 -gt $myLen1 ]; then #Determine if String2 is of greater length than String1
 33         echo "The second string has a greater text length than the First string."
 34         exit 1
 35 elif [ $myLen1 -eq $myLen2 ]; then #Determine if the strings have equal length
 36         echo "The two strings have the exact same length."
 37         exit 1
 38 fi

I'm receiving the following error from my script: (otherwise, it works as intended)

./advnacedlab4.sh: line 20: [: too many arguments
./advnacedlab4.sh: line 20: [: too many arguments

Please give me your input. Thanks!

Linuxn00b
  • 131
  • 3
  • 12

1 Answers1

5

As jasonwryan pointed out, you need to protect yourself from having spaces in the strings you're testing. You can do that either by putting quotes around the variables, so when they are expanded they are still treated as a single unit, or by using the [[ operator instead of [ which is smarter about handling such expansions, but is less portable.

Otherwise, if either string1 or string2 has a space you'll have an expression like:

string1="string one"
if [ -z string one ] ...

so it will be passing 2 strings "string" and "one" to -z which only expects a single argument.

jasonwryan
  • 73,126
Eric Renouf
  • 18,431