Case structure combined with standard input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case structure combined with standard input
# 1  
Old 03-06-2013
Case structure combined with standard input

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
Code:
case (read -p "" variable) in
x)


Thx!
# 2  
Old 03-06-2013
Removed: wrong code.

Last edited by radoulov; 03-06-2013 at 10:44 AM..
# 3  
Old 03-06-2013
Thx for ur reply!

Unfortunately it is not working like I want it to...
Code:
#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

Code:
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

Smilie

Last edited by radoulov; 03-06-2013 at 10:39 AM..
# 4  
Old 03-06-2013
Yes, sorry. And why don't you use the second version (the working one)?
# 5  
Old 03-06-2013
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.
# 6  
Old 03-07-2013
Quote:
Originally Posted by Corona688
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".

Code:
read variable

would just use the input as the new variable

But I still don't get why
Code:
case $(read variable)

does not work.

Would you please be so kind and explain it to me?
# 7  
Old 03-07-2013
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 ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

2. Shell Programming and Scripting

standard input and cron

I have a program that requires the user to enter input values while it is being run for example in bash ... ... .. echo "Enter your input" read input echo $input ... ... ...I need to schedule this program with crontab, hence a problem, cronjobs run in the background, any ideas on how to... (10 Replies)
Discussion started by: walforum
10 Replies

3. Shell Programming and Scripting

case structure in .sh file

Hi, while true do printf "$FBOLD\nPlease enter the minutes (0-59): $FREG" read MIN case "$MIN" in ||s) break 2;; *) echo "" echo "Invalid minutes, please try again.";; esac done In the above... (4 Replies)
Discussion started by: milink
4 Replies

4. Solaris

standard input

Please give me any example for standard input in Solaris. (6 Replies)
Discussion started by: karman0931
6 Replies

5. UNIX for Dummies Questions & Answers

Fall through case structure

Hello Experts, I was wondering how can we get a fall trough case structure. Eg: If we have two cases A and B and a default; if I do not break after A, it should execute all the statements of A as well as B if I break after B else fall through even lower. I tried this: #!/bin/sh v1=1... (6 Replies)
Discussion started by: hkansal
6 Replies

6. UNIX for Dummies Questions & Answers

ksh case structure

Hello Experts, I ve been trying to build another shell where I am using the following code. transact="tv5cpc1" case "$transact" in "...cp..") xActType="" ;; "...de..") xActType="sp_dep" ;; "...ep..") xActType="sp_epa" ;; "....v.") ... (4 Replies)
Discussion started by: hkansal
4 Replies

7. Shell Programming and Scripting

change standard input ?

Dear... I have a scrpit that contains multiple read command.... when I run the script I have to enter 3 variables so that I can get the output.. but, I dont want to put those 3 inputs manually every time... I want to make a shell that reads the 3 inputs from a file. the script name is... (4 Replies)
Discussion started by: yahyaaa
4 Replies

8. Shell Programming and Scripting

Case structure

Hi, Can anyone help me with the following case structure? echo "Please enter the date for which you want the Report:\c" read dat d1=`echo $dat|cut -c7-8` m1=`echo $dat|cut -c5-6` y1=`echo $dat|cut -c1-4` yr=`expr "$d1" - 1` case "$yr" in 0) MONTH=`expr "$m1" - 1`... (4 Replies)
Discussion started by: kamitsin
4 Replies

9. Shell Programming and Scripting

standard input

how can i redirect standard input? i dont remember :/, though could you redirec not from a command? i mean, to redirect always stdin and stout (1 Reply)
Discussion started by: Jariya
1 Replies

10. UNIX for Dummies Questions & Answers

Problem w. case structure

Hello, I am having a problem setting a range of numbers for the "case" structure. I can use with no problems, but when I use it doesn't work??? Does the case struture allow numeric ranges? eg: echo -e "enter number between 0 and 60: \c" read $answer case $answer in ) echo... (2 Replies)
Discussion started by: Joe54321
2 Replies
Login or Register to Ask a Question