Basic if-else practice

Practice the logic of if-elif-else conditional branching

Due: Friday, January 30
Points: 5

Basic practice of the if/then/else syntax for numerical comparators. Given a skeleton script to flesh out, you’ll practice how to use conditional expressions to change a program’s output depending on what arguments are passed into the script.

Deliverables

  • A project folder named `compciv/homework/if-else-practice`

  • A script named `basic-ifelse.sh`

    The basic-ifelse.sh, when run from the command-line with a set of given arguments, should return output exactly as specified in the Output section.

    You can make a copy of the sample skeleton script and modify it.

  • Hints

    You will want to be familiar with conditional branching with if/elif/else statements, how to write a reusable script, and how to test number of arguments and how to compare arguments

    Sample skeleton program

    You are welcome to use the script below and fill in the parts needed to complete the assignment:

    # skeleton basic-ifelse.sh
    # if there are fewer than 1 arguments, i.e. 0
    if [[ "$#" -lt 1 ]]; then
      echo "You need at least one argument"
    
    elif [[ "$#" -eq 1 ]]; then
    # just one argument 
      echo "There is just one argument: $1"
    
    elif [[ "$#" -eq 2 ]]; then
    
      echo "(fix this line yourself)"
    
      if [[ $1 -eq $2 ]]; then
        echo "$1 is equal to $2"
    
        ## write the other conditions for less-than and
        ## greater-than yourself
      fi
    
    elif [[ "$#" -eq 3 ]]; then
    
      echo "There are three arguments: $1, $2, and $3"
    
      # There are a few ways to do this next part, I'm going with
      # the strategy of having one string variable for dealing
      # with comparing $1 to $2, and the other string for $1 vs $3,
      # and then concatenating them at the end.
    
      # You can fill out what I've done, or come up with 
      # your own branching logic
    
      if [[ $1 -eq $2 ]]; then
        str_a="is equal to $2"
      elif [[ $1 -lt $2 ]]; then
        str_a="is less than $2"
        ## and so forth
      fi
        
      if [[ $1 -eq $3 ]]; then
        str_b="is equal to $3"
        ## and so forth
      fi
    
      # finally, echo the full comparison statement
      echo "$1 $str_a and $str_b"
    
    ### Almost done
    ### You need to write in one last conditional branch to deal with 
    ### more than 3 arguments, i.e.
    ### "You need fewer than 9 arguments"
    fi
    
    
    

    Expected output

    Here is what your basic-ifelse.sh script should return in each of the scenarios below:

    # If no arguments are passed in
    user@host:~$ bash basic-ifelse.sh 
    You need more than 0 arguments
    
    # If more than 3 arguments are passed in
    user@host:~$ bash basic-ifelse.sh 100 200 300 400
    You need fewer than 4 arguments
    
    # If more than 3 arguments are passed in
    user@host:~$ bash basic-ifelse.sh 100 200 300 400 500
    You need fewer than 5 arguments
    
    # If only one argument is passed in
    user@host:~$ bash basic-ifelse.sh 9
    The only argument is 9
    
    # If two arguments are passed in and they are equal
    user@host:~$ bash basic-ifelse.sh 9 9
    There are two arguments: 9 and 9
    9 is equal to 9
    
    # If two arguments are passed in and the first is less than the second
    user@host:~$ bash basic-ifelse.sh 9 10
    There are two arguments: 9 and 10
    9 is less than 10
    
    
    # If two arguments are passed in and the first is greater than the second
    user@host:~$ bash basic-ifelse.sh 14 6
    There are two arguments: 14 and 6
    14 is greater than 6
    
    # If three arguments are passed in and they are all equal
    user@host:~$ bash basic-ifelse.sh 9 9 9
    There are three arguments: 9, 9, and 9
    9 is equal to 9 and is equal to 9
    
    # If three arguments are passed in and the first is greater than the second
    # and greater than the third
    user@host:~$ bash basic-ifelse.sh 12 9 9
    There are three arguments: 12, 9, and 9
    12 is greater than 9 and is greater than 9
    
    
    # If three arguments are passed in and the first is equal to the second
    # and less than the third
    user@host:~$ bash basic-ifelse.sh 12 12 40
    There are three arguments: 12, 12, and 40
    12 is equal to 12 and is less than 40
    
    # If three arguments are passed in and the first is equal to the second
    # and less than the third
    user@host:~$ bash basic-ifelse.sh 12 12 40
    There are three arguments: 12, 12, and 40
    12 is equal to 12 and is less than 40
    
    # If three arguments are passed in and the first is less than the second
    # and less than the third
    user@host:~$ bash basic-ifelse.sh 4 12 40
    There are three arguments: 4, 12, and 40
    4 is less than 12 and is less than 40
    
    # etc etc
    user@host:~$ bash basic-ifelse.sh 95 2 3
    There are three arguments: 95, 2, and 3
    95 is greater than 2 and is greater than 3