#!/bin/bash
# a life simulator
# call with ./$scriptname <name> <number>
NUM_REQUIRED_ARGS=2
num_args=$#
# Do we have at least 2 arguments?
if [[ $# -lt $NUM_REQUIRED_ARGS ]]; then
echo "Not enough arguments. Call this script with
,/{$0} <name> <number>"
exit 1 # exit with error
fi
# Set variables, using arguments
name=$1
number=$2
echo "Your first two arguments were: $1 $2"
# for loops: iteration, string interpolation
# $@ → all arguments
echo "You ran this program with ${num_args} arguments. Here they are:"
for arg in "$@"; do
echo "$arg"
done
# two ways to define a function
spaced() {
# parameters are not named; they are positional
echo
echo "######################"
echo "$1" # 1st arg passed into the func
echo "######################"
echo
}
function javatest() {
if [[ $number -eq 99 ]]; then
spaced "You win! You're amaaaaazziinnnggg!" # function call; no ()
elif (( $number < 10 )); then
spaced "You're a courageous one"
# set a variable interactively
echo "Hi ${name}, to avert a horrible death, please enter the password"
read password
if [[ "$password" != "Java" ]]; then
spaced "Well, at least you're not a Java Programming sympathizer. \
You can go now."
else
spaced "DIIEEEE! Actually, nevermind. Writing Java is enough \
of a hellish punishment. You are free to leave"
fi
fi
}
# Call a function
javatest $number
exit 0