array to choices .. lost


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting array to choices .. lost
# 1  
Old 12-21-2009
array to choices .. lost

Hi Im' trying to output a list of files then make the list files as choices, need someone to give me hand.

so far here is what i got.. a bit messy

Code:
#!/bin/sh

menu_str=`ls -1 file*`

cnt=0
for i in $menu_str
do
menu_item[cnt]=${i}
cnt=$(($cnt+1))
echo $cnt 
echo ${menu_item[*]}
done

#echo $cnt ${menu_item[*]}
i=-1
echo Enter your number of choices
read choice
while [ $i -lt $cnt ]; do
        #echo "$i ${menu_item[$i]}"
        let i++
        if [ $i -eq $choice ]; then
                echo "you have chosen $i ${menu_item[$i]}"
                exit 1
        fi
done


#############################

should output something like:

Code:
echo "number  of choices <1 - $cnt>"
read $choices

Choice 1. File1111
Choice 2. File2223
Chioce 3. File1233

and so on..

If $choice is equal to array of choices then
echo you have chosen Choice 1.
echo do something...


Thanks

Last edited by pludi; 12-21-2009 at 07:29 AM.. Reason: code tags, please...
# 2  
Old 12-21-2009
Maybe this script is an option for you; I did not alter your script just wrote it new:
Code:
#!/bin/bash

ARR=( `ls -1 file*` )
Z=1

for ELE in ${ARR[*]}; do
        printf "%-5d%-50s\n" $Z $ELE
        Z=$(($Z+1))
done

echo "--------------------------------"
read INPUT

I=$(($INPUT-1))
echo "You pressed [${INPUT}] for file [${ARR[${I}]}]"

exit 0

Output:
Code:
$> ll file*
-rw-r--r-- 1 root root 0 2009-12-21 13:23 file1
-rw-r--r-- 1 root root 0 2009-12-21 13:23 file2
-rw-r--r-- 1 root root 0 2009-12-21 13:23 file3
$> ./mach.ksh
1    file1
2    file2
3    file3
--------------------------------
2
You pressed [2] for file [file2]

If there could be a situation where there is no file, there will be an error so you might want to check additionally for files to fill your array 1st.

Last edited by zaxxon; 12-21-2009 at 09:31 AM.. Reason: Added info; shortened it and defined array directly
# 3  
Old 12-21-2009
Code:
#!/bin/ksh

PS3='Enter your number of choices> '

select i in *
do
  echo "you have chosen [${REPLY}] for file [${i}]"
done

# 4  
Old 12-22-2009
Thanks, both should do the trick
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Solaris

Lost out-of-band communication with 2540 Array

Hello Dears, My server hots cannot contacted my array disk 2540 by using out-of-band Ethernet Cable. The Controller Ethernet port is off , I replace the ethernet cable but not OK I reboot the server and the Array disk 2540 but Not OK. Please somebody know how to solve this issue? ... (1 Reply)
Discussion started by: ghislino
1 Replies

2. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

3. Shell Programming and Scripting

Generating a list of choices in a menu

Hello all, I have the below script and I'm a little stuck on the best way to continue. Essentially I'm creating a text file (systems.txt) with a list of servers in it by hostname. Then I would like to echo a menu with each hostname and a number to use to pick it from the list. It's somehow... (7 Replies)
Discussion started by: sysera
7 Replies

4. Filesystems, Disks and Memory

Lost Data Lost Admin

First time so excuse my ignorance please. I may not be accurately describing the issue. I have inherited a small lab mostly SUN V120s. We lost power and are trying to recover. Nope no backups... The primary issue I have is 1 box is an Oracle Server. It has 2 36Gb harddrives. I am able to... (3 Replies)
Discussion started by: murphsr
3 Replies
Login or Register to Ask a Question