Using an array with a case statement in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using an array with a case statement in KSH
# 1  
Old 10-17-2011
Using an array with a case statement in KSH

Hi,

I'm really new ro shell scripting (actually any kind of programming) and am pretty sure I'm making a pretty basic error here but I can't for the life of me figure it out.

What I'm trying to do is get an array working with a case statement in a KSH script. The code is as follows:

Code:
set -A zone `zoneadm list | grep -v global`
 
PS3="Enter choice:"
 
select zone_menu in ${zone[*]} "All" "Exit"
 
do
   case $zone_menu in
 
                    ${zone[]})
                        print ${zone[$REPLY-1]};;
 
                    All)
                        print ${zone[*]};;
 
                    Exit)
                        break ;;
   esac
done

So far what the above does right is that it takes the list of zones on a Solaris 10 box (minus the global zone) and inputs these into an array. I've no problem generating the list od options for the case statement using the array but it seems to be imposible to get the correct output from the case selection. I can basically get the correct output from the first option, the ALL option and the Exit option but nothing else works.

Does what I say make any sense and if so, can someone please help me? It's driving me bannanas! Smilie

Thanks in advance,

Steve

---------- Post updated at 05:12 PM ---------- Previous update was at 04:31 PM ----------

I just did the following:

Code:
...
do
   case $zone_menu in
 
                    ${zone[$REPLY-1]})
                        print ${zone[$REPLY-1]};;
 
                    All)
                        print ${zone[*]};;
...

I was looking right at it. Smilie

Steve

P.S.: Bloody Azerty keyboards!!!
# 2  
Old 10-18-2011
You're doin' great for a scripting newb.

The do ... done are not necessary here.
# 3  
Old 10-19-2011
Thanks for the reply otheus and thanks for the compliment. I've been working hard to get over what I consider the last barrier to my carrier as a sys-engineer.

I tried to leave the do...done out but the interpreter doesn't see the case statement coming and it exits.

The script has grown to over 200 lines (including comments) and so far so good. I've one issue with an if... elif... else statement but I'll have a hunt around to see what I can find on the forums before posting anything here.

Cheers,

Steve
# 4  
Old 10-19-2011
Ahhh... the select command requires the do..done. That's a new one to me. *hattip*
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh case statement issue

Hi. I wrote the following case statement to replace a series of 'ELIF' statements as it looks better and is easier to maintain. However, for some reason the commands don't fully work in this format. Take option 1. It should call a script that runs in the background but it doesn't work. Can anyone... (3 Replies)
Discussion started by: user052009
3 Replies

2. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

3. 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

4. Shell Programming and Scripting

How to remove a defined character on an array variable in a do while statement in ksh

I have a korn shell code here on a while do statement which replace the string stored on an array removing double quotes characters on it but it doesn't work. example record: appointmentDate = "tree" which value should result to tree #!/bin/ksh # Remove " on string records let recordCount=3... (5 Replies)
Discussion started by: ryukishin_17
5 Replies

5. Shell Programming and Scripting

case statement

Hi, I am writing case statement to execute some finction, my requirement is once one of the case statement is executed again it has to prompt for the option. for script in `echo "$Script_Selected"` do case $script in 1) getNoOFActUsers ;; 2) moveServerrOORotation ;; ... (2 Replies)
Discussion started by: Satyak
2 Replies

6. Shell Programming and Scripting

ksh "case" statement question

Hi I have the following case statement: case $larg in *_* ) a=${larg%_*}; b=${larg#*_}; ;; *^* ) a=${larg%^*}; b=${larg#*^}; ;; esac I cannot figure out what *_* and *^* stand for... Also what a=${larg%_*}; b=${larg#*_}; and a=${larg%^*}; b=${larg#*^}; ... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. UNIX for Dummies Questions & Answers

case statement in UNIX scripting (ksh)

Hi, I have a script like below : #!/bin/ksh echo "Do you want to export all docs ?" read alld echo "Do you want to export template or report only " read temr case && ] #arguments ;; case && ] #arguments ;; case && ] #arguments ;; (4 Replies)
Discussion started by: luna_soleil
4 Replies

8. Shell Programming and Scripting

ksh case statement

I am trying to write a ksh script using the case statement to select certain directories to remove. The directories that I am looking for are in the following format 2008-10-10. I want to exclude all other files/directories that contain anything other the 4 digit year,a dash, 2 digit month, a... (2 Replies)
Discussion started by: dgilc
2 Replies

9. UNIX for Dummies Questions & Answers

CASE statement

Hi, I am writing a bash shell script. My script has a few user defined parameters. When the script runs the first thing it does is make sure that these parameters are valid. One of the parameters is called YEAR. A valid input for YEAR can be 1997-2000. One way I have come up with to ensure... (3 Replies)
Discussion started by: msb65
3 Replies

10. Shell Programming and Scripting

Case Statement

Can anyone please tell me why this wont work! Thanks so much! #!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target (19 Replies)
Discussion started by: Zeta_Acosta
19 Replies
Login or Register to Ask a Question