OK, last night I decided to go through the http://tldp.org/LDP/Bash-Beginners-Guide/html/ thing, which I will make note I have not done that sort of thing before really bar a few long forgotten exceptions.
So today I decided, I want to try something that isn’t just basically copied from the book.
Anyway, it doesn’t work (lol), specifically, I have got this (I’m not going to put everything here because I don’t want you telling me ALL the answers before I had a chance to try and figure them out myself! It’s probably all broken from here on as well but let me find that out as I go along and try and fix it first before asking :p):
if [ "$would" = {'y,Y'}{'" ",es,eah,ES'} ]; then
and later on the same basic concept for no instead of yes.
Obviously this does not work. I dunno if its progress or regression that after fiddling with it, its not working occurs with no error messages. It just skips right to the end whatever I type (where it exits with a message).
Basically what I want is if I type any variation on yes YES y Y Yes etc it does the thing after then, any variation on no and it exits with one message, any other thing apart from variations on yes and no then it exits with a different message. At the moment its only exiting with the different message without doing anything.
And like I said I never did anything like this before so be gentle
edit: hmmm, I am guessing what is wrong here is that its not thinking does $would= yes OR y OR YES etc etc, but rather does it = the list of the variations, which obviously its not going to… and that’s why it is going to the end instead of running then?
I got it to work.
Although I completely changed the way I was trying to do.
So what I did was instead of trying to see if what the user entered ($would) was equal to {y,Y} etc, I just made two new variables:
Where the files yesfile and nofile had the various yes and no’s in them and then saw if s2 and s3 were greater than or equal to 1.
OH, spoke too soon I guess, now whatever I type it acts like I said yes, even if I put no… (bit in the middle works though :D)
Edit: YAY now it all works!
This is what it looks like:
#!/bin/bash
# This is a script to show off to people on my instant messenger list that I have
# made a script something that before yesterday I had never done before.
cyear=$(date +%Y)
cmonth=$(date +%m)
cday=$(date +%d)
echo "Hello $USER"
echo "I made this script for you so you can see what I can do!"
echo "..."
echo "...would you like to see? (yes or no)"
read would
# Now I would like to see if they said yes or no, or something else. If they said
# yes, Y, YES, Yes or y then I want to calculate their age. If they say No, no, n,
# N or NO then I want to exit sadly. If they said something else say "just yes or no!"
s1=$would
s2=`grep $s1 yesfile -c`
s3=`grep $s1 nofile -c`
if [ "$s2" -gt "0" ]; then
echo "I take pleasure in showing you!"
echo "Please tell me what year you were born"
read byear
echo "and what month (1-12 please ^_^)"
read bmonth
echo "and on what day? (1-31 =^_^=)"
read bday
echo "thank you ^_^"
echo "you are `expr $cyear - $byear` years `expr $cmonth - $bmonth` months and `expr $cday - $bday` days old"
else
if [ "$s3" -gt "0" ]; then
echo "oh alright then"
exit
else
echo "Hrmph! Disobedient clod!"
exit
fi
fi
#!/bin/bash
# This is a script to show off to people on my instant messenger list that I have
# made a script something that before yesterday I had never done before.
cyear=$(date +%Y)
cmonth=$(date +%m)
cday=$(date +%d)
echo "Hello $USER"
echo "I made this script for you so you can see what I can do!"
echo "..."
echo "...would you like to see? (yes or no)"
read would
# Now I would like to see if they said yes or no, or something else. If they said
# yes, Y, YES, Yes or y then I want to calculate their age. If they say No, no, n,
# N or NO then I want to exit sadly. If they type anything else exit insultingly.
case $would in [yY]es|[yY]eah|[yY]|[yY]ES) s1=1;; *) s1=0;; esac
if [ "$s1" -gt "0" ]; then
echo "I take pleasure in showing you!"
echo "Please tell me what year you were born"
read byear
echo "and what month (1-12 please ^_^)"
read bmonth
echo "and on what day? (1-31 =^_^=)"
read bday
echo "thank you ^_^"
echo "you are `expr $cyear - $byear` years `expr $cmonth - $bmonth` months and `expr $cday - $bday` days old"
else
case $would in [nN]o|[nN]O) s2=1;; *) s2=0;; esac
if [ "$s2" -gt "0" ]; then
echo "oh alright then"
exit
else
echo "Hrmph! Disobedient clod!"
exit
fi
fi