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
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.
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
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.)
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?
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.
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" ]
since it says read answer should the $ be answer instead of ans?
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?
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:
#!/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.
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'.