Find the answer to your Linux question:
Results 1 to 4 of 4
RE: Beginning scripting Hello: Here's an easy one to start the New Year with. What is a more efficient way to construct the tests below? Code: #!/bin/bash echo -n "Guess ...
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    10

    bash: using test and or ([ ] and -o)

    RE: Beginning scripting

    Hello:
    Here's an easy one to start the New Year with.

    What is a more efficient way to construct the tests below?

    Code:
    #!/bin/bash
    echo -n "Guess three colors. Enter all in lowercase.
    "
    read word1
    read word2
    read word3
    
    #TEST 1
    color=red
    [ "$color" = "$word1" -o "$color" = "$word2" -o "$color" = "$word3" ]
    A=$?
    
    #TEST 2
    color=white
    [ "$color" = "$word1" -o "$color" = "$word2" -o "$color" = "$word3" ]
    B=$?
    
    #TEST 3
    color=blue
    [ "$color" = "$word1" -o "$color" = "$word2" -o "$color" = "$word3" ]
    C=$?
    
    D=$((A+B+C))
    
    if [ $D = 0 ]; then
    echo "Congratulations! You guessed all three correctly."
    else
    echo "Sorry, try again."
    fi
    Thanks,
    Lux

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    try this - it uses loops and arrays:
    Code:
    #!/bin/bash
    echo -n "Guess three colors. Enter all in lowercase.
    "
    read word1
    read word2
    read word3
    
    # array to hold colors guessed correctly
    declare -a guessed
    
    # loop thru all guessed words 
    for word in $word1 $word2 $word3; do 
    
      # loop thru the list of correct colors
      for color in red white blue; do
        if [ "$word" == "$color" ]; then
          guessed[${#guessed[*]}]=$color
          break
        fi
      done
    done
    
    # if there are 3 entries in the array, they must have guessed them all
    if [ ${#guessed[*]} -eq 3 ]; then
      echo "Congratulations! You guessed all three correctly."
    else
      echo "Sorry, try again."
    fi
    this way makes it easy to add more colors/inputs, too.

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    10
    Thank you for the alternate script, atreyu, and for the accompanying explanations. I do like your solution much better than the one I proposed and agree that it lends itself to easier expansion. However, I would still like to know if there is simpler way to write the test expression. Specifically, I'm wondering if there is a way to change this
    Code:
    #TEST 2
    color=white
    [ "$color" = "$word1" -o "$color" = "$word2" -o "$color" = "$word3" ]
    B=$?
    to something more like this
    Code:
    #TEST 2
    color=white
    [ "$color" = ("$word1" -o "$word2" -o "$word3") ]
    B=$?
    which of course does not work as shown.
    Thanks for the help.
    Lux

  4. #4
    Linux Guru
    Join Date
    May 2011
    Posts
    1,843
    oh, now i see your question. i don't know how to do what you are suggesting in bash, but it did make me think of another way -using bash's case statement, e.g.:
    Code:
    declare -i ok
    ok=0
    case word1 in
      red) ok+=1 ;;
      blue) ok+=1 ;;
      green) ok+=1 ;;
    esac
    echo "You guessed $ok correct colors"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...