Merge pull request #88 from erintherad/update-doc

update archaic and sexist example
This commit is contained in:
Peter Bieringer 2019-08-09 05:59:44 +02:00 committed by GitHub
commit c75eee187f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 42 deletions

View File

@ -208,73 +208,72 @@ Type the year that you want to check (4 digits), followed by [ENTER]:
<sect2 id="sect_08_02_02"><title>Prompting for user input</title>
<para>The following example shows how you can use prompts to explain what the user should enter.</para>
<screen>
<prompt>michel ~/test&gt;</prompt> <command>cat <filename>friends.sh</filename></command>
<prompt>michel ~/test&gt;</prompt> <command>cat <filename>books.sh</filename></command>
#!/bin/bash
# This is a program that keeps your address book up to date.
# This is a program that creates a favorite book library.
friends="/var/tmp/michel/friends"
books="books.txt"
echo "Hello, "$USER". This script will register you in Michel's friends database."
echo "Hello, "$USER". This script will add your favorite book to the database."
echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your gender and press [ENTER]: "
read -n 1 gender
echo -n "Enter the title and press [ENTER]: "
read title
echo -n "Enter the name of the author and press [ENTER]: "
read author
echo
grep -i "$name" "$friends"
grep -i "$title" "$books"
if [ $? == 0 ]; then
echo "You are already registered, quitting."
if [ $? == 0 ]; then
echo "You have already suggested a book, quitting."
exit 1
elif [ "$gender" == "m" ]; then
echo "You are added to Michel's friends list."
elif [ "$author" == "shakespeare" ]; then
echo "What's in a name? That which we call a rose by any other name would smell as sweet."
exit 1
else
echo -n "How old are you? "
read age
if [ $age -lt 25 ]; then
echo -n "Which colour of hair do you have? "
read colour
echo "$name $age $colour" &gt;&gt; "$friends"
echo "You are added to Michel's friends list. Thank you so much!"
echo -n "Enter the cost and press [ENTER]: "
read cost
if [ $cost -lt 25 ]; then
echo "$title | $author | $cost" &gt;&gt; "$books"
echo "Your book is added to the database. Thank you so much!"
else
echo "You are added to Michel's friends list."
echo "Let me look for $title by $author at the library."
exit 1
fi
fi
<prompt>michel ~/test&gt;</prompt> <command>cp <filename>friends.sh /var/tmp</filename>; cd <filename>/var/tmp</filename></command>
<prompt>michel ~/test&gt;</prompt> <command>cp <filename>books.sh /var/tmp</filename>; cd <filename>/var/tmp</filename></command>
<prompt>michel ~/test&gt;</prompt> <command>touch <filename>friends</filename>; chmod <option>a+w</option> <filename>friends</filename></command>
<prompt>michel ~/test&gt;</prompt> <command>touch <filename>books</filename>; chmod <option>a+w</option> <filename>books</filename></command>
<prompt>michel ~/test&gt;</prompt> <command>friends.sh</command>
Hello, michel. This script will register you in Michel's friends database.
Enter your name and press [ENTER]: michel
Enter your gender and press [ENTER] :m
You are added to Michel's friends list.
<prompt>michel ~/test&gt;</prompt> <command>books.sh</command>
Hello, michel. This script will add your favorite book to the database.
Enter the title and press [ENTER]: 1984
Enter the name of the author and press [ENTER]: Orwell
Enter the cost and press [ENTER]: 30
Let me look for 1984 by Orwell at the library.
<prompt>michel ~/test&gt;</prompt> <command>cat <filename>friends</filename></command>
<prompt>michel ~/test&gt;</prompt> <command>cat <filename>books</filename></command>
</screen>
<para>Note that no output is omitted here. The script only stores information about the people Michel is interested in, but it will always say you are added to the list, unless you are already in it.</para>
<para>Note that no output is omitted here. The script only stores information about the books that Michel is interested in. It will always thank you for your suggestion, unless you already provided it.</para>
<para>Other people can now start executing the script:</para>
<screen>
<prompt>[anny@octarine tmp]$</prompt> <command>friends.sh</command>
Hello, anny. This script will register you in Michel's friends database.
Enter your name and press [ENTER]: anny
Enter your gender and press [ENTER] :f
How old are you? 22
Which colour of hair do you have? black
You are added to Michel's friends list.
<prompt>[anny@octarine tmp]$</prompt> <command>books.sh</command>
Hello, anny. This script will add your favorite book to the database.
Enter the title and press [ENTER]: Sense and Sensibility
Enter the name of the author and press [ENTER]: Austen
Enter the cost and press [ENTER]: 10
Your book is added to the database. Thank you so much!
</screen>
<para>After a while, the <filename>friends</filename> list begins to look like this:</para>
<para>After a while, the <filename>books</filename> list begins to look like this:</para>
<screen>
tille 24 black
anny 22 black
katya 22 blonde
maria 21 black
Sense and Sensibility | Austen | 10
Harry Potter and the Sorcerer's Stone | Rowling | 20
The Lord of the Rings | Tolkien | 22
To Kill a Mockingbird | Lee | 12
--output omitted--
</screen>
<para>Of course, this situation is not ideal, since everybody can edit (but not delete) Michel's files. You can solve this problem using special access modes on the script file, see <ulink url="http://www.tldp.org/LDP/intro-linux/html/sect_04_01.html#sect_04_01_06">SUID and SGID</ulink> in the Introduction to Linux guide.</para>