You are not logged in.

#1 2011-10-17 19:15:22

blueswat
Member
Registered: 2011-10-17
Posts: 3

How to create a menu like of pacman?

Bash

For example

   1) exo  2) garcon  3) gtk-xfce-engine  4) terminal  5) thunar  6) tumbler
   7) xfce4-appfinder  8) xfce4-mixer  9) xfce4-panel  10) xfce4-session
   11) xfce4-settings  12) xfce-utils  13) xfconf  14) xfdesktop  15) xfwm4
   16) xfwm4-themes

Enter a selection (default = all):1 3 5 14

I tried the "command case" but only accepts one option

printf "%s\n" "List of options";
printf "%s\n" "1)option1 2)option2 3)option3";
printf "%s" "Choose the option:";
read -n 100 RESP
case "$RESP" in
1) PROTOCOL="test1";;
2) PROTOCOL="test2";;
3) PROTOCOL="test3";;
*) printf "Invalid option. Choose a valid number.\n"
   exit 0;;
esac

List of options
1)option1 2)option2 3)option3
Choose the option:1 2
Invalid option. Choose a valid number.

Last edited by blueswat (2011-10-18 04:14:02)

Offline

#2 2011-10-17 20:07:25

srikanthradix
Member
Registered: 2010-10-19
Posts: 35

Re: How to create a menu like of pacman?

Won't the for loop work?


This profile is scheduled for deletion.

Offline

#3 2011-10-17 20:25:06

blueswat
Member
Registered: 2011-10-17
Posts: 3

Re: How to create a menu like of pacman?

With loop is possible to make a multi-selection?

Enter a selection (default = all):1 3 5 14

Or will have to select one at a time?

Enter a selection (default = all):1
Enter a selection (default = all):3
Enter a selection (default = all):5
Enter a selection (default = all):14

Offline

#4 2011-10-17 21:27:35

srikanthradix
Member
Registered: 2010-10-19
Posts: 35

Re: How to create a menu like of pacman?

With the for loop you can give multiple inputs at once
Enter a selection (default = all):1 3 5 14
It will process each selection individually inside the script.
or you can also use the while
while [ -n "$*" ]
do
// do something with the argument $1
echo $1;
  shift
done


This profile is scheduled for deletion.

Offline

#5 2011-10-17 21:36:26

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: How to create a menu like of pacman?

This seems to do it ok

printf "%s\n" "List of options";
printf "%s\n" "1)option1 2)option2 3)option3";
printf "%s" "Choose the option:";
read -n 100 RESP
for i in $RESP; do
    case "$i" in
    1) PROTOCOL="test1";;
    2) PROTOCOL="test2";;
    3) PROTOCOL="test3";;
    *) printf "Invalid option. Choose a valid number.\n"
       exit 0;;
    esac
done
exit 0

You're just jealous because the voices only talk to me.

Offline

#6 2011-10-18 01:19:40

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: How to create a menu like of pacman?

Try this:

#!/bin/bash

declare -a options=(
	exo garcon gtk-xfce-engine terminal thunar tumbler
	xfce4-appfinder xfce4-mixer xfce4-panel xfce4-session
	xfce4-settings xfce-utils xfconf xfdesktop xfwm4 xfwm4-themes
)

i=1
for o in "${options[@]}"; do
	printf "%2d) %s\n" $(( i++ )) "$o"
done
echo -n $'\nChoose option(s): '

read -a choice

echo $'\nYou have chosen:'

for c in "${choice[@]}"; do
	[[ "$c" -lt "1" ]] && printf "\nAbort: Unknown option %s\n" "$c" && exit 1
	[[ "$c" -gt "${#options[@]}" ]] && printf "\nAbort: Unknown option %s\n" "$c" && exit 1
	echo "	${options[$((c-1))]}"
done

When inputing the options, don't separate with commas. Do like this:

Choose option(s): 1 5 9

Last edited by rockin turtle (2011-10-18 01:29:50)

Offline

#7 2011-10-18 02:56:57

blueswat
Member
Registered: 2011-10-17
Posts: 3

Re: How to create a menu like of pacman?

Thank you guys

I will try ...

then I post the result.

Offline

Board footer

Powered by FluxBB