Writing Script?

Anyone have an example of a simple shell script that solicits a (Y)es or (N)o response from the user. If the response is 'Y' display a message on the screen that thanks the user for the positive response. If the response is 'N' display a message that thanks the user for the negative response. If the answer is anything else, redisplay the question.

see my response to one of your other posts, i already prototyped this.

while ($user_input == "")
if ($user_input == ("y" || "Y"))
do something
else if ($user_input == ("n" || "N"))
do something
else
do something else 

again the syntax is different for different languages.

that while loop makes sure that some user input is entered, and that last else can be used to make sure that they enter either y Y n or N.
you could put it in a function, bash supports this:

userinput()
{
while ... 
if ...
else if ...
else
do 
userinput()
}

make sure if you use my example that you check everything out for you self as i have just typed this up and it is only a general idea of what you can do.

edit: a number of your posts in Q&A for dummies really belong in shell scripting....it really helps if you pay attention to the different forums we have here. it makes the site more navigable and information easier to access for others.

nordsk hensk, would you know what command i can use to prompt the user for input? I want to prompt a user for their password and cant figure out how.

You can accomplish this by using the echo & read commands though keep in mind its not at all secure!

echo "Please Enter Your Checking Account Number: \c"
read ACCOUNT_NUMBER

echo "The account number you entered was: $ACCOUNT_NUMBER"

Great! its working almost perfectly. the only problem is that its printing the "\c". How do i get that to not print?

remove the "\c" character! What shell are you using? that sequence should cause the cursor to remain on line. i.e. not print a new line.

Ok. I thought it did something so I wasn't removing it. But it doesnt stop the terminal from going to the next line.I'm using macosx 10.2.2 jaguar with a tcsh shell. do you know how i could accomplish what "\c" is supposed to do in my shell, because it doesnt seem to work?

Actually, I am not really sure, but you could always try echo -n which is another way to accomplish the same task.

That worked perfectly, thanks.:slight_smile:

well i have yet another question.:rolleyes: I tried to write the information entered in my prompt to a file with:

$prompt_info > /dir/file

it returns "./prompt.sh:WhateverIEnteredInThePrompt: command not found

what am i doing wrong?

you need to echo the contents of the variable and then redirect the standard out stream to your file

example:
FOO="Cool Beans!"
echo "$FOO" > log.dat
cat log.dat

# Cool Beans!

Note that there is a big difference between the redirect operator ">" and ">>". The former will overwrite a file and the latter will append to the file.

I'm assuming $prompt_info is inside your script - try something like:

echo "$prompt_info" > /dir/file

------
yea, what google said :stuck_out_tongue:

Worked great, thanks.

either your file is not found "case sensitive" or you typed in the wrong path, however, and even that i know this is a homework but i'll help.

try this

$input > $HOME/Home_Work.txt

mbabeli, the code you posted is exactly what blip posted, which he said doesn't work; besides, the question was already answered .. twice :rolleyes:

its not homework, I just got a new imac so im trying to become more familiar with unix and learn some stuff.