24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

bit better:<br />

michel ~/test> cat penguin.sh<br />

#!/bin/bash<br />

# This script lets you present different menus to Tux. He will only be happy<br />

# when given a fish. To make it more fun, we added a couple more animals.<br />

if [ "$menu" == "fish" ]; then<br />

if [ "$animal" == "penguin" ]; then<br />

echo -e "Hmmmmmm fish... Tux happy!\n"<br />

elif [ "$animal" == "dolphin" ]; then<br />

echo -e "\a\a\aPweetpeettreetppeterdepweet!\a\a\a\n"<br />

else<br />

echo -e "*prrrrrrrt*\n"<br />

fi<br />

else<br />

if [ "$animal" == "penguin" ]; then<br />

echo -e "Tux don't like that. Tux wants fish!\n"<br />

exit 1<br />

elif [ "$animal" == "dolphin" ]; then<br />

echo -e "\a\a\a\a\a\aPweepwishpeeterdepweet!\a\a\a"<br />

exit 2<br />

else<br />

echo -e "Will you read this sign?! Don't feed the "$animal"s!\n"<br />

exit 3<br />

fi<br />

fi<br />

michel ~/test> cat feed.sh<br />

#!/bin/bash<br />

# This script acts upon the exit status given by penguin.sh<br />

if [ "$#" != "2" ]; then<br />

echo -e "Usage of the feed script:\t$0 food-on-menu animal-name\n"<br />

exit 1<br />

else<br />

export menu="$1"<br />

export animal="$2"<br />

echo -e "Feeding $menu to $animal...\n"<br />

feed="/nethome/anny/testdir/penguin.sh"<br />

$feed $menu $animal<br />

result="$?"<br />

echo -e "Done feeding.\n"<br />

case "$result" in<br />

1)<br />

echo -e "Guard: \"You'd better give'm a fish, less they get violent...\"\n"<br />

;;<br />

2)<br />

echo -e "Guard: \"No wonder they flee our planet...\"\n"<br />

;;<br />

3)<br />

echo -e "Guard: \"Buy the food that the Zoo provides at the entry, you ***\"\n"<br />

echo -e "Guard: \"You want to poison them, do you?\"\n"<br />

Chapter 8. Writing interactive scripts 95

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!