basic script for yes and no answers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting basic script for yes and no answers
# 8  
Old 05-08-2008
THank you for the replies! Please bear with me as I am obviously very new to unix and scripting.

Dave Miller:
Can you break this down for me?
read answer
ans=`cat $answer"N" | cut -c1-1 | tr "y" "Y"`
if [ $ans = "Y" ]

1. since it says read answer should the $ be answer instead of ans?
2. I dont understand this syntax: ans=`cat $answer"N" | cut -c1-1 | tr "y" "Y"`
why cat $answer"N"
cut portion I know means cut the first colum of the answer so the first letter what is the -1
tr I know mean so change so is that saying if the user puts a lower case y change it an uppercase?


denn - is the reason you prefer case is because there are multiple answers and even lower case, uppercase answers etc?

Thanks!!!
# 9  
Old 05-08-2008
you was originally going to accept
y|Y|yes) for user input, so users being users, you've really no
idea what they'll type, just for kicks or to give you a hard time.
all the possible options they could use:
yes
YES
YEs
yES
yeS
YeS
yEs

When i give users a choice, I want either a definate positive or a negative,
anthing else is considered invalid and they'll be reasked until they give a proper response.

This is my suggestion:

Code:
#!/usr/bin/ksh

until [[ $answer = "Y" ]] || [[ $answer = "N" ]]
do
        echo "Do you want to start the process 1-5 Y/N?"
        read answer
        answer=`echo $answer | tr [:lower:] [:upper:]`
        case $answer in
           Y|YES)
                answer=`echo $answer | cut -c1`
                # /usr/home/myhome/script.ksh
                print "Process 1-5 Started. Done."
                ;;
           N|NO)
                answer=`echo $answer | cut -c1`
                print "Process 1-5 Not Started. Exiting Now"
                ;;
                
           *)
                print Unexpected Input: $answer  Retrying
                ;;
        esac

done

Note: I commented out your
/usr/home/myhome/script.ksh
to test.

Last edited by denn; 05-08-2008 at 03:51 PM..
# 10  
Old 05-08-2008
It's my habit, to not have the result of any calculation to use the source variable as the destination variable. That's why I used ans instead of answer.

To break it down:
ans=`cat $answer"N" | cut -c1-1 | tr "y" "Y"`

This is a three part process, the result of which becomes ans.

Part 1 actually has an error. It should be echo, not cat. It adds an 'N' to the end of whatever reply was supplied, thereby creating a default should the user hit enter without typing anything. The next step takes the first character and ignores the rest. The final step turns a lowercase 'y' into a capital 'Y'.

Then the if statement looks for a capitol 'Y'. Anything else, is the same as 'N'.
# 11  
Old 05-08-2008
I'm sorry I'm still having a hard time understanding:

" It adds an 'N' to the end of whatever reply was supplied, thereby creating a default should the user hit enter without typing anything. "

So if someone hit enter without typing anything then it inputs a N so that it will exit?


Why is this good practice? Like what is a sample scenerio?

"to not have the result of any calculation to use the source variable as the destination variable."
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a little help with my first shell script. Basic image resize script...

Hey everyone, just now joined because I didn't want to go onto Ubuntu forums and start asking about how to write shell scripts. Seems like this is a pretty active forum for exactly what I need. I'm trying to modify a shell script I found online, the end goal is to have it find all files in the... (9 Replies)
Discussion started by: mozzles
9 Replies

2. Shell Programming and Scripting

Bash script to give multiple choices and a varying number of answers

Hello everybody, I use `case' quite a lot but , excellent as it is , it only gives one final result ; can anyone suggest a way whereas , say long lists of choices are given and I , or a user could select either one two or any number of results to be echoed . many thanks in... (3 Replies)
Discussion started by: V686
3 Replies

3. Shell Programming and Scripting

Select answers from multiple questions using shell script

I have a text file in this format Some lines.... Question no: 1 The question? A. Answer 1 B. Answer 2 C. Answer 3 D. Answer 4 Answer:B Some lines.... Question no: 2 The question? (choose 2) (10 Replies)
Discussion started by: zorrox
10 Replies

4. Shell Programming and Scripting

Perl script give answers by file

Hi, I am new in perl. I am running a perl installation script, its asking for paths and so many inputs. Can we provide that info by any file. so i can avoid the interactive installation. (2 Replies)
Discussion started by: Priy
2 Replies

5. Shell Programming and Scripting

Shell Script to provide "answers" to SSL Cert Request

Hello, I need assistance with creating a shell script to generate SSL Certificate Requests on remote hosts. Below is my stab at this, but I cannot figure out how to pass the requested arguments into the openssl command correctly. I have a major problem with redirecting the "answers" into the... (2 Replies)
Discussion started by: azvelocat
2 Replies

6. Shell Programming and Scripting

I need help with a basic script

a) Total number of words in the file. b) Total number of different words in the file. How can I use the translate and/or unique commands to accomplish this (4 Replies)
Discussion started by: EECSDAVE
4 Replies

7. Shell Programming and Scripting

Basic script?

All, I have a list of over 400 users that need certain directories created. These will be created in /users/$username on a system and I need a directory called chess under these directories that I create. Instead of me manually adding each one (mkdir /users/user1, mkdir /users/user1/chess)... (1 Reply)
Discussion started by: kjbaumann
1 Replies
Login or Register to Ask a Question