Hi folks,
I am new to bash scripting so please excuse my question.
Is there any chance to combine a case structure with the read command?
Like
case (read -p "" variable) in
x)
Thx!
Hi folks,
I am new to bash scripting so please excuse my question.
Is there any chance to combine a case structure with the read command?
Like
case (read -p "" variable) in
x)
Thx!
Removed: wrong code.
Thx for ur reply!
Unfortunately it is not working like I want it to...
#case $(read -p "" datacheck) in
#1) echo "yes" ;;
#2) echo "no" ;;
#*) echo "wrong";;
#esac
the answer is alway "wrong"
if I write it like this
read -p "" datacheck
#case $datacheck in
#1) echo "yes" ;;
#2) echo "no" ;;
#*) echo "wrong";;
#esac
the outcome depends on the standard input like it should be
Yes, sorry. And why don't you use the second version (the working one)?
What, exactly, does read -p "" datacheck
print to the screen?
What, exactly, would $( ) give for a command that prints nothing to the screen?
This is why your case statement doesn't work.
Well, I misunderstood the -p "". So it is just "read variable".
read variable
would just use the input as the new variable
But I still don't get why
case $(read variable)
does not work.
Would you please be so kind and explain it to me?
See Corona688's statement in post#5.
Command substitution makes stdout of program/command available for e.g. variable assignments or shell interpretation. read var
doesn't print anything to stdout, so nothing can be substituted. Make it case $(read var; echo $var) in ...
works! thx!