What are we learning?

By the end of this lesson you will have:

The Lesson

Resources:    BASH Scripting for Beginners    |    BASH Syntax Cheat Sheet    |    Official BASH Reference

Aliases:

Since BASH is a shell to the kernel and a scripting language, it can be very powerful

Scripts:

Let's make our own executable program using BASH

The ADD script:

#!/usr/bin/bash

if [[ $# == 0 ]] then

echo "Null"

elif [[ $# == 1 ]] then

  echo $1

elif [[ $# == 2 ]] then

echo $[ $1 + $2 ]

else

  echo "Too many arguments"

fi

Your Tasks

1. Dog Treats Create the script treats.sh such that it is executable and it solves the following problem:

Barley the dog loves treats. At the end of the day he is either happy or sad depending on the number and size of treats he receives throughout the day. The treats come in three sizes: small, medium, and large. His happiness score can be measured using the following formula: 1 x S + 2 x M + 3 x L

where S is the number of small treats, M is the number of medium treats and L is the number of large treats.

If Barley’s happiness score is 10 or greater, then he is happy. Otherwise, he is sad. Determine whether Barley is happy or sad at the end of the day.

Input Specification

Your program will take three command-line arguments (no more, no less). Assume correct input. The first argument contains the number of small treats, S, the second is the number of medium treats, M, and the third is the number of large treats, L, that Barley receives in a day.

Output Specification

If Barley’s happiness score is 10 or greater, output happy. Otherwise, output sad.

Samples:

~$ ./treats.sh 0 0 0

sad

~$ ./treats.sh 3 1 0

sad

~$ ./treats.sh 3 2 1

happy

(for a 4+)
2. Sum Using the references linked at the top of this page and the images to the right, create the BASH script sum.sh such that it follows these constraints:

Examples:

~$ ./sum.sh

0

~$ ./sum.sh -8

-8

~$ ./sum.sh 1 2 3 -7

-1

Submitting Code

Go to our Google Classroom > Classwork and find the "3 - BASH Scripting" task. Complete and submit!