Register
It is currently Fri Apr 18, 2014 11:26 am

Create Menu from CSV File


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Nov 30, 2010 3:27 pm   

Joined: Tue Nov 30, 2010 3:18 pm
Posts: 2
Hello All,

I have a need to build a dynamic menu based on the values of a csv file.

For example, lets say my file looks like this:

a,aa,abc,1.1.1.1
a,aa,abd,2.2.2.2
a,ab,abc,3.3.3.3
b,bb,bcd,4.4.4.4
b,bb,bce,5.5.5.5
b,bc,bcf,6.6.6.6
c,cc,cde,7.7.7.7
c,cc,cdf,8.8.8.8
c,cd,cfg,9.9.9.9

I want the menu to read the first column of each row and make on entry menu for each item. So it would look like this:

1) a
2) b
3) c

Then the user would choose one of the items and it would move over to column two, where it would look like this if (b) was chosen:

1) bb
2) bc

Then again if (bb) was chosen, it would display this:

1) bcd
2) bce

Finally, if if the user chose bcd, the script would launch an ssh session to 4.4.4.4 as that is the last column in the file.

Hopefully this makes sense and I'm not way off base with my thoughts here.

Thanks in advance!!
Kelly


Top
 Profile  
 PostPosted: Tue Nov 30, 2010 9:18 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
Homework ??


Top
 Profile WWW  
 PostPosted: Tue Nov 30, 2010 9:22 pm   

Joined: Tue Nov 30, 2010 3:18 pm
Posts: 2
No. This is a learning opportunity for me. We have some homemade scripts that work in different fashions, but the consensus from my peers is that this type of script would be preferred. This would be used to drill down through a site/market/equipment listing and if the data is contained within a CSV file, it would be easier to keep updated.


Top
 Profile  
 PostPosted: Wed Dec 01, 2010 11:48 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
I replaced commas with tabs in your file. The script can be something like this:
Code:
#! /bin/bash
unset chosen
position=1
columns=$(head -n1 "$1" | grep -o $'\t' | wc -l)

while (( position <= columns )) ; do
    options=($(grep "^$chosen" "$1" | cut -f $position | sort -u))
    select choice in "${options[@]}" ; do
        if [[ $choice ]] ; then
            let position++
            chosen+=$choice$'\t'
            break
        fi
    done
done
host=$(grep "$chosen" "$1" | cut -f $position)
ping $host

Some checking should be added (parameter exists, the csv file exists, etc.)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP