Add a variety of options to the main script

The options added are:
 -p, --pokemon POKEMON_NAME
 -f, --file COW_FILE
 -n, --no-name
 -h, --help
This commit is contained in:
Lucas Possatti
2015-06-14 02:39:45 -03:00
parent afc544cb20
commit da15ecfcb7
2 changed files with 76 additions and 8 deletions

View File

@@ -24,7 +24,7 @@ cat > $bin_path/$bin_name <<- EOF
current_path=`pwd`
cd $install_path/
./pokemonsay.sh
./pokemonsay.sh \$@
cd \$current_path
EOF

View File

@@ -1,17 +1,85 @@
#!/bin/sh
# Define where the pokemon are.
usage() {
echo
echo " Description: Pokemonsay makes a pokémon say something to you."
echo
echo " Usage: $(basename $0) [-p POKEMON_NAME] [-f COW_FILE] [-n] [-h] [MESSAGE]"
echo
echo " Options:"
echo " -p, --pokemon POKEMON_NAME"
echo " Choose what pokemon will be used by its name."
echo " -f, --file COW_FILE"
echo " Specify which .cow file should be used."
echo " -n, --no-name"
echo " Do not tell the pokémon name."
echo " -h, --help"
echo " Display this usage message."
echo " MESSAGE"
echo " What the pokemon will say. If you don't provide a message, a message will be read form standard input."
exit 1
}
# While there are arguments, keep reading them.
while [ $# -gt 0 ]
do
key="$1"
case $key in
-p|--pokemon)
POKEMON_NAME="$2"
shift; shift
;;
-p=*|--pokemon=*)
POKEMON_NAME="${1#*=}"
shift
;;
-f|--file)
COW_FILE="$2"
shift; shift
;;
-f=*|--file=*)
COW_FILE="${1#*=}"
shift
;;
-n|--no-name)
DISPLAY_NAME="NO"
shift
;;
-h|--help)
usage
;;
*)
# Append this word to the message.
if [ -n "$MESSAGE" ]; then
MESSAGE="$MESSAGE $1"
else
MESSAGE="$1"
fi
shift
;;
esac
done
# Where the pokemon are.
pokemon_path=`pwd`/cows
# Find all pokemon in the pokemon path and choose a random one.
pokemon=$(find $pokemon_path -name "*.cow" | shuf -n1)
# Define which pokemon should be displayed.
if [ -n "$POKEMON_NAME" ]; then
pokemon_cow=$(find $pokemon_path -name "$POKEMON_NAME.cow")
elif [ -n "$COW_FILE" ]; then
pokemon_cow="$COW_FILE"
else
pokemon_cow=$(find $pokemon_path -name "*.cow" | shuf -n1)
fi
# Get the pokemon name.
filename=$(basename "$pokemon")
filename=$(basename "$pokemon_cow")
pokemon_name="${filename%.*}"
# Call cowsay.
cowsay -f $pokemon
cowsay -f "$pokemon_cow" "$MESSAGE"
# Write the pokemon name.
echo $pokemon_name
# Write the pokemon name, unless requested otherwise.
if [ -z "$DISPLAY_NAME" ]; then
echo $pokemon_name
fi