LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Display current value (https://www.linuxquestions.org/questions/programming-9/display-current-value-593763/)

Gremlin2 10-22-2007 04:28 PM

Display current value
 
Hi all,

How can I display a current variable value while performing read?
In other words, in case of standard echo/read sequence – something will be displayed first and then my script will stop, waiting for user input. But nothing will be displayed as a “default value” for that “read” (i.e. something already written as an input for read while the user is able to edit it and eventually, after the “enter” was hit – the finial text will be read by the “read” command).

IMHO, any professional level script should suggest a user with a default value for something, allowing the user to simply hit “enter” and accept the proposed value while sill allowing users change it to some other (desired) value. How can I do it in BASH Scripting?

Any answer will be appreciated,
Alex.

rsashok 10-22-2007 05:00 PM

I don't quite understand your question. But 'read' by definition waits for input. If you want to read something that might be there or not before actual access attempt, you might consider creating a fifo. More on creating and using fifo's are on the Web, and 'man mkfifo'.

Gremlin2 10-22-2007 05:22 PM

Look, indeed the read command waits for user input. But the question is how can I suggest something as such input, primary by placing it on behalf of user input (i.e. defaut value)? Usually, the read command allows you to enter your input and then hit "enter" but if there is something already typed in and the user can simply hit "enter" (accept it) and continue or he can delete it and type in something else? Such a suggested answer for read, that's what I'm interested in.

rsashok 10-22-2007 07:05 PM

I see... . But then you could use 'read', but before you do:
Code:

@echo "If your want default value of 10 hit Enter, otherwise type your value......"
read var1
if [ $var1 -eq $NEWLINE ] then;
((var1=10))
fi

You should figure out how to see if var1 is a newline, I can't tell from the top of my head.

jlinkels 10-22-2007 08:21 PM

Not sure whether "\n" is included as part of the returned read string. I thougt you could test on
Code:

if [ $var1 -eq "\n" ]
or
Code:

if [ $var1 -eq "" ]
One of the two will work, and echoing any read in variable in Bash is a must during development anyway.

It might be obvious, but run your program with
Code:

sh -x myprogram.sh
You'll see what is going on.

jlinkels


All times are GMT -5. The time now is 05:55 AM.