"dynamic" case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "dynamic" case
# 1  
Old 05-19-2009
"dynamic" case

I searched but didn't find the answer to my question in previous posts, so i am going to throw it out to all the pros..

I have written a script for solaris zone creation, and part of this script has you select the lun that you wish to build the zone on. It is currently really messy in this part, and needs to be constantly modified as the number of luns grows.

What i am looking for is a way to have this be dynamic (ie if $DISK25 exists then have that be a valid selection in the menu). I know that a case statement is probably not the best way to do this, and maybe a select would work? The script works in its current incarnation, but i am looking for a way to avoid having to continuously add lines to the case statement as we add more and more luns, as well as get the number of lines in the code down..

Code:
 
#!/bin/ksh
#set -x
<jump to relevant code>
function zone_disk
{
header
echo | /usr/bin/pfexec format
print
print -n "Enter the number of the disk you wish to use for this zone (or q to exit): "; read DISKINFO
clear
header
case $DISKINFO in
0) DISKINFO=$DISK1; print; zpool_vdev; print "You have selected $DISKINFO";;
1) DISKINFO=$DISK2; print; zpool_vdev; print "You have selected $DISKINFO";;
2) DISKINFO=$DISK3; print; zpool_vdev; print "You have selected $DISKINFO";;
3) DISKINFO=$DISK4; print; zpool_vdev; print "You have selected $DISKINFO";;
4) DISKINFO=$DISK5; print; zpool_vdev; print "You have selected $DISKINFO";;
5) DISKINFO=$DISK6; print; zpool_vdev; print "You have selected $DISKINFO";;
6) DISKINFO=$DISK7; print; zpool_vdev; print "You have selected $DISKINFO";;
7) DISKINFO=$DISK8; print; zpool_vdev; print "You have selected $DISKINFO";;
8) DISKINFO=$DISK9; print; zpool_vdev; print "You have selected $DISKINFO";;
9) DISKINFO=$DISK10; print; zpool_vdev; print "You have selected $DISKINFO";;
10) DISKINFO=$DISK11; print; zpool_vdev; print "You have selected $DISKINFO";;
11) DISKINFO=$DISK12; print; zpool_vdev; print "You have selected $DISKINFO";;
12) DISKINFO=$DISK13; print; zpool_vdev; print "You have selected $DISKINFO";;
13) DISKINFO=$DISK14; print; zpool_vdev; print "You have selected $DISKINFO";;
14) DISKINFO=$DISK15; print; zpool_vdev; print "You have selected $DISKINFO";;
15) DISKINFO=$DISK16; print; zpool_vdev; print "You have selected $DISKINFO";;
16) DISKINFO=$DISK17; print; zpool_vdev; print "You have selected $DISKINFO";;
17) DISKINFO=$DISK18; print; zpool_vdev; print "You have selected $DISKINFO";;
18) DISKINFO=$DISK19; print; zpool_vdev; print "You have selected $DISKINFO";;
19) DISKINFO=$DISK20; print; zpool_vdev; print "You have selected $DISKINFO";;
20) DISKINFO=$DISK21; print; zpool_vdev; print "You have selected $DISKINFO";;
21) DISKINFO=$DISK22; print; zpool_vdev; print "You have selected $DISKINFO";;
22) DISKINFO=$DISK23; print; zpool_vdev; print "You have selected $DISKINFO";;
 <code snipped, as each additional line is more of the same in this case>
 
esac

and this is the part of the code where i get the $DISK# varaible for the case statement above

Code:
 
# this function gets the cxtxdx numbers of the disks and assigns them to variables
function disk_numbers
{
# echo cxtxdx numbers to a variable
CTDNUM=`echo | /usr/bin/pfexec format | grep c.t | awk '{print $2}'`
echo $CTDNUM > /tmp/disk
 
# count the disks for the counter and passing number to an integer
typeset -i DISKS=$(nawk -F' ' '{print NF;exit}' /tmp/disk)  
 
integer cnt=1
#typeset -i
while (( cnt <= DISKS ))
do
eval DISK$cnt=`echo $CTDNUM | awk '{print $'$cnt'}'`
cnt=$cnt+1
done
/usr/bin/pfexec rm /tmp/disk
}

hopefully my question makes sense, but if there are any questions I will promptly answer them

I am by no means a pro at coding, so any help would be greatly appreciated!
# 2  
Old 05-19-2009
Not sure if you intended to do this

Code:
print -n "Enter the number of the disk you wish to use for this zone (or q to exit): "; read DISKINFO
clear
header
DISKINFO=\$DISK$DISKINFO;
print; zpool_vdev; print "You have selected $DISKINFO"

Cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Advanced & Expert Users

Should I say "field 8" or "column 8" in this case?

I saw some recent posts where I thought the terms "field" and "column" were being misused. I work with data a lot, and have my opinions. I'm wondering if those opinions are correct. ***** Rows seem clear - I don't think there is any controversy about what a row is, either for database or text... (10 Replies)
Discussion started by: hanson44
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

7. Shell Programming and Scripting

passing a list of dynamic names to a "PS" command in shell script?

Hi, I am new to shell script. This is my first post .I have written a small script which returns list of names starts with "ram" in /etc/passwd .Here is that:- #!/bin/ksh NAME_LIST="name_list.txt" cat /dev/null > $NAME_LIST evalcmd="cat /etc/passwd | grep "^ram?*" | cut -d: -f1" eval... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

8. UNIX for Dummies Questions & Answers

copy "cp" command how distinquish upper/lower case

hello, i thought this should work to specify that i want to "cp" all files no matter what the case, ie upper or lower, as i have 2 files called Makefile and mak cp -i mak* but apparently the -i isn't a valid option like it is with grep thanks for any suggestions! bobk (1 Reply)
Discussion started by: bobk544
1 Replies
Login or Register to Ask a Question