Displaying default values when accepting input from user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Displaying default values when accepting input from user
# 1  
Old 10-21-2010
Network Displaying default values when accepting input from user

Is there a way to display the default answer when accepting input from the user in the unix script..

e.g.
ans="n"
read $ans?"Enter y to continue n to exit:"

altough ans contains n the message doesn't display the current contents on ans .. you get

Enter y to continue n to exit:
# 2  
Old 10-21-2010
You could try printing the default and then backup over it. tput will give the the move cursor left escape sequence.

Code:
BACK="$(tput cub1)"
ans=y
echo -e "Enter y to continue n to exit: $ans$BACK\c" ; read ans

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 10-21-2010
perfect .. thanks very much .....
# 4  
Old 10-21-2010
@Chubler don't know about the behaviour on your or his OS, but your solution would empty the $ans (if just press enter when the cursor is on the y default value)

Code:
# BACK="$(tput cub1)"
# ans=y
# echo "Enter y to continue n to exit: $ans$BACK\c" ; read ans
Enter y to continue n to exit: y
# echo "$ans"

#

so i would go for :
Code:
read ${ans:=n}?"Enter y to continue n to exit:"
echo "$ans"

Code:
# read ${ans:=n}?"Enter y to continue n to exit:"
Enter y to continue n to exit:
# echo "$ans"
n
#

I did put a _ to show where the cursor is

The problem with my code is that it would make the answer appear twice if an input is given...



---------- Post updated at 02:50 PM ---------- Previous update was at 02:18 PM ----------




The final right code is a mix of ours :

Code:
BACK="$(tput cub1)"
read ${ans:=n}?"Enter y to continue n to exit: ${ans:=n}$BACK"

# 5  
Old 10-21-2010
nope, chubler's solution works perfectly for me ..

thanks
# 6  
Old 10-21-2010
On which Operating system and shell do you use ?

Last edited by ctsgnb; 10-21-2010 at 10:42 AM..
# 7  
Old 10-21-2010
AIX 6.1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capturing multiple values from user input

Hello, I need to capture multiple values from user input and do not know how to do it. I'm writing a script to delete old files, but want to give the option to keep some by asking the user. This is what my output looks like... Old files to be deleted... 1 file1 2 file2 Then this bit of... (3 Replies)
Discussion started by: jrymer
3 Replies

2. Programming

Filling the class from values taken from user input

I have a program that accepts user input. For example I have mdacc that the user sets the value. I then have a class which stores the value set by the user. I use set_param to set the values in the class. I pass through it the list of user defines arguments from argv. What would be the opinion on... (0 Replies)
Discussion started by: kristinu
0 Replies

3. Shell Programming and Scripting

Accepting Input regardless of Case

Hi I am trying to get my script to accept input regardless if the person enters a or A. here is the portion of the code where I get the input. echo -n 'Please enter your choice:' # prompt user for input. read reply # read input echo case $reply in #... (2 Replies)
Discussion started by: DualPandas
2 Replies

4. Shell Programming and Scripting

Accepting user input and arguments in PERL

Hi All, Can we pass arguments while calling the perl script and as well as ask user input during execution of the script? My program is as below: I am passing arg1 and arg2 as argements to test.pl ]./test.pl arg1 arg2 Inside the test.pl I have : print "Do you want a name ? (y/n) : ";... (2 Replies)
Discussion started by: jisha
2 Replies

5. UNIX for Dummies Questions & Answers

accepting input date

I how do i accept a input date in script which is lesser than a specified day? ex: to accept a date less than or equal to 100 days(from today).?:( Thanks for the help in advance.:) (1 Reply)
Discussion started by: abhi_123
1 Replies

6. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies

7. Shell Programming and Scripting

Accepting user input in c shell

i need to accept the user input in my c shell script before executing next command. i have the following code which ask for user input, but does not store this value. set req echo " Enter your input(Y/N)?" read req if (req = Y) echo " print $req" else echo " print $req" ... (3 Replies)
Discussion started by: skumar11
3 Replies

8. Shell Programming and Scripting

Accepting user input in Bourne shell and using sed

He guys. Basically I want to make a script that can add, delete and view stuff in a external file called config.txt. I can open it up in Joe but im not sure how to read in the user input or using commands automatically in joe to edit, save then quit. Problem area below: 1) echo "Add... (1 Reply)
Discussion started by: Pits
1 Replies

9. Linux

Why isn't the computer accepting input?

I'm using Red Hat Linux 3, and the computer has stopped taking input from mouse, keyboard, etc. What are possible causes? How can I fix? -Worried Linux User (11 Replies)
Discussion started by: lunchtime
11 Replies

10. Shell Programming and Scripting

Accepting User Input

I'm just starting out with UNIX and have figured some stuff out. I just need some help with accepting user input on the command line. For instance, I created a number counter that counts down from any positive hard coded number. But, I want the commnad line line to read "Countdown 20" where 20... (1 Reply)
Discussion started by: scott78
1 Replies
Login or Register to Ask a Question