Case with list of pickes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case with list of pickes
# 1  
Old 09-18-2014
Case with list of pickes

I need help with following :-
I have the following function:-

Code:
 function queuecreate {

    echo "1 - Bread"
        echo "2 - Milk"
        echo "3 - Suger"
        echo "4 - Coffee"     

    echo "What do you need? > "
    read QT
    
    case $QT in
    1 ) echo "You Chose Bread"
        ;;
        2 ) echo "You Chose Milk"
        ;;
        3 ) echo "You Chose Suger"
        ;;
        4 ) echo "You Chose Coffee"
        ;;

        1 | 4 ) echo "You Chose Bread and Coffee"
        ;;
* ) echo "You did not enter a number"
        echo "between 1 and 4." 

esac

Currently the script will print whatever number I input , for example if I input 1 , the
output will be (You Chose Bread) and so on. and if I input 1 and 4 the output will be (You Chose Bread and Coffee)



How can I change it in the way look like this :-

Code:
What do you want ?

( ) Bread
( ) Milk 
( ) Suger 
( ) Coffee 

Type yes next to the Iteam you want.

So if the I type yes next to Bread and yes next to coffee
Code:
(yes) Bread
( ) Milk 
( ) Suger 
(yes) Coffee

the output should be:

Code:
You chose Bread and Coffee


Last edited by Don Cragun; 09-18-2014 at 04:40 AM.. Reason: Add CODE tags for input and output as well as for code samples.
# 2  
Old 09-18-2014
What OS and shell are you using?

You could use curses to do what you're suggesting, but it would be a lot easier to keep the numbered list you had before and allow the user to enter one or more values in the data you store in the variable QT. The way to efficiently process the returned value would vary depending on the shell you're using.
# 3  
Old 09-18-2014
Don ,
I am using Linux and AIX,

I agree with you, keeping the number will be easy , however , my list has around 30 iteams , I am using the above example just to give me hint .

so my list qround 30 iteams , it will be hard to give case like

1 | 4 | 20 | 30 because I don't know what I might chose between 1 to 30.


I though maybe there is way to do it like 1-30 , or by writing yes next to it

---------- Post updated at 10:42 AM ---------- Previous update was at 03:40 AM ----------

Smilie Any Ideas are welcome
# 4  
Old 09-18-2014
You could experiment with dialog:
Code:
dialog --checklist "Choose item(s):" 11 35 5 \
    1 Bread off \
    2 Milk off \
    3 Sugar off \
    4 Coffee off \
2>/tmp/answer.$$

# 5  
Old 09-18-2014
The following is easy to extend
Code:
  items="\
1 - Bread
2 - Milk
3 - Suger
4 - Coffee"     
  echo "$items"
  echo "What do you need? > "
  read QT
  choice=`echo "$QT" | tr -sc '[:digit:] ' '\n'`
  picklist=""
  for ch in $choice
  do
    out=`
      echo "$items" |
      while read i1 dash i2
      do
        if [ $ch = $i1 ]; then
          echo $i2
        fi
      done
    `
    if [ -z "$out" ]; then
      echo "$ch is out of range"
    else
      picklist="$picklist $out"
    fi
  done
  echo "You chose $picklist"

# 6  
Old 09-18-2014
Here is another way to do this just using standard interfaces:
Code:
queuecreate() {
	echo "1  - Bread"
	echo "10 - Milk"
	echo "11 - Suger"
	echo "14 - Coffee"     
	printf 'What do you need? (Enter space separated list of codes): '
	read QT
	set -- $QT

	for choice in "$@"
	do	case $choice in
		(1)	echo "You Chose Bread";;
		(10)	echo "You Chose Milk";;
		(11)	echo "You Chose Suger";;
		(14)	echo "You Chose Coffee";;
		(*)	printf 'Code "%s" is not valid\n' "$choice";;
		esac
	done
}
while [ 1 ]
do	queuecreate
	printf 'Try again? (y or n): '
	read resp
	case $resp in
	(N*|n*)	exit 0
	esac
done

Note that I used 1, 10, 11, and 14 just to get the resulting values into double digits without needing to enter 10 choices. If you want to print a more English like list with commas and an "and" where appropriate, you can easily complicate the logic in the case statement a little bit to make that happen.

Does this help?
# 7  
Old 09-18-2014
What's a pickes?

Thread title: Case with list of pickes
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

2. Shell Programming and Scripting

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (7 Replies)
Discussion started by: slatoms
7 Replies

3. Red Hat

Dynamic case creation based on output list from a command

I am attempting to create a script that would allow me to list all the instances associated with a DB2 and then prompt the user to choose which one to issue the db2profile command against. I use the db2 command db2ilist to get a list of the instances for a particular server, but the number of... (1 Reply)
Discussion started by: slatoms
1 Replies

4. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

5. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

6. UNIX for Dummies Questions & Answers

List files/dirs alphabetically case sensitive - AaBbCc?

Hi there, im trying to list some dirs/files alphabetically (case sensitive) like in windows: Folder_1 folder_2 Folder 1 Folder 2 folder 3 A.txt a 1.txt a 2.txt B.txt ... i tried ls, sort, ls and sort but cant get it to work? Is this actually possible?? (im using #!/bin/sh) (3 Replies)
Discussion started by: nuttenlova
3 Replies

7. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question