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
# 1  
Old 05-06-2008
basic script for yes and no answers

What is the basic syntax for a script that says

do you want to do this?
y - execute this
n - end
not y or n - end and print this

for example if I want to run this:
"Do you want to start this process?"
answer
if y,Y, or yes
then
run the following script (do I put the script with complete path?)
print processes started
else
say processes not started
end

thanks!

Last edited by llsmr777; 05-06-2008 at 04:11 PM..
# 2  
Old 05-06-2008
Do you want someone here to write the script for you? Not gonna happen. However, if you write something, someone here can help yo figure out what went wrong if it doesn't work.

That said, here's some help to get you started:

Use the echo command to display something, the read comand to get a user reply, and the test function to check the reply.

YES. You CAN call another script (or command line function, or whatever) from within a script. And whether or not you need the complete path is dependent upon the $PATH of the user that is running it. Suffice to say, it can't hurt.

Note that in your brief logic, you have it saying that it started AFTER it finished! You should put that line ABOVE the line where you go to the other script.
# 3  
Old 05-06-2008
Thank you for the reply. No definately don't want someone to write a script for me. I definately want to learn. I tried to google to see some examples but could not find any.

I have something written but not sure if the IF or CASE statement woudl be better. I also think these are some mistakes in what I wrote.

I'll write what I have for each. Any feedback would be fantastic:

echo "Do you want to start the process 1-5?"
read answer
if [$answer="y"];
then
/usr/home/myhome/script.ksh
else
echo "Not starting Process 1-5. End Script"
fi


or

echo ""Do you want to start the process 1-5?"
read answer
case $answer in
y|Y|yes)
/usr/home/myhome/script.ksh
print "Process 1-5 Started. Done."
;;
*)
print "Process 1-5 Not Started. Done."
;;
esac
# 4  
Old 05-06-2008
In your first version:
if [$answer="y"];
You need spaces around everything:
if [ $answer = "y" ]
Get additional acceptable responses:
if [ $answer = "y" -o $answer = "Y" -o $answer = "yes" -o $answer = "YES" ]
That semicolon is not needed. It's often used to put the next logical line on the same physical line.

Your second version looks OK, except I've never done multiple conditions in a case statement with the pipe like you did. Bottom line, if it works, go for it.
Except, again, you've got the message that it started AFTER it finishes. (I.E. The called script executes to completion, THEN the message that it started shows up.)
# 5  
Old 05-06-2008
Thank for evaluating my script!

for your comment: "Note that in your brief logic, you have it saying that it started AFTER it finished! You should put that line ABOVE the line where you go to the other script. "

Do you mean in my case script?
It needs to read:
case $answer in
y|Y|yes)
print "Process 1-5 Started. Done."
/usr/home/myhome/script.ksh

Also when I try to run the case script I get the following error...got any ideas?:
syntax error at line 9 : `"' unmatched
I actually have this script because I did not call another script:

#!/bin/ksh
echo ""Do you want to start process 1-5?"
read answer
case $answer in
y|Y|yes)
echo "Process 1-5 Started."
;;
*)
echo "Process 1-5 Not Started."
;;
esac


and I get that syntax error no matter what answer I put????


Do you have any best practice advice on when to use case vs. If?
# 6  
Old 05-07-2008
1 - Yeah, that's what I meant. Put the message that the process is starting, BEFORE it starts.

2 - I hadn't copied and tried it myself until now. I got the same error and had to scratch my head a bit before I figured it out. You're going to hate yourself: You have an extra quote at the begining of line TWO!



case vs if ? Use case when there are multiple options. Use if when there aren't.

Note, I would have done it a little different:

read answer
ans=`cat $answer"N" | cut -c1-1 | tr "y" "Y"`
if [ $ans = "Y" ]

This accomplishs a couple things: It adds a default answer. Uses only the first character, doesn't care if it's upper or lower case.

If there were multiple choices, I'd use the same type of logic to get A, B, C... responses, and then use case:

read answer
ans=`cat $answer"X" | cut -c1-1 | tr "a-z" "A-Z"`
case $ans in

Last edited by Dave Miller; 05-07-2008 at 08:48 AM..
# 7  
Old 05-07-2008
I actually perfer the case statement vs. if for situations like this, but to each his own.

Why you're getting the error.

Code:
echo ""Do you want to start process 1-5?"

#remove one quote and it works
echo "Do you want to start process 1-5?"

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