Building a dynamic UNIX menu with input files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Building a dynamic UNIX menu with input files
# 1  
Old 10-05-2016
Building a dynamic UNIX menu with input files

Hi!

I am looking to build dynamic menu (named: lookup.sh) that reads a pipe delimited file for input.

for example, contents of input.txt could be:
Code:
user1|srv1
user3|srv1
user4|srv1
user2|srv2

I want the menu look like:
Code:
1) get password for user1 on srv1
2) get password for user3 on srv1
3) get password for user4 on srv1
4) get password for user2 on srv2
Choose Option:

then if you choose option #1, it would cat pass_user1_srv1.txt to provide the password.

the key thing I am trying to accomplish is that I don't want to modify lookup.sh when i add a new option. i want to go to the input.txt file and add a line to that and it will be read in the next time lookup.sh is run.

will a bash array work? I am not too familiar with them.

Any ideas?
Any help would be greatly appreciated!
Chris

Last edited by Scrutinizer; 10-05-2016 at 07:32 PM.. Reason: code tags
# 2  
Old 10-05-2016
What operating system and shell are you using?

If you're using a recent bash or ksh, have you considered select?
# 3  
Old 10-05-2016
Code:
#!/bin/bash

while [ -z "${ans[0]}" ]
do
  awk -F'|' '{print NR ") get password for " $1 " on " $2}' input.txt
  read -p "Choose Option Number: " sel
  ans=($(awk -F'|' 'NR==sel {print $1, $2}' sel="$sel" input.txt))
done
cat pass_${ans[0]}_${ans[1]}.txt

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 10-05-2016
RHEL 6 -- ksh or bash are available.

---------- Post updated at 09:52 PM ---------- Previous update was at 06:40 PM ----------

---------- Post updated at 09:59 PM ---------- Previous update was at 09:52 PM ----------

@rdrtx1 -- your code worked! Thank you very much!

Last edited by cpolikowsky; 10-05-2016 at 11:58 PM..
# 5  
Old 10-06-2016
Assuming bash or ksh are available, try
Code:
IFS=$'\n'
select ans in $(sed 's/^/get password for /; s/[|]/ on /' input.txt)
  do    T=${ans% on*}
        echo cat pass_${T##* }_${ans##* }.txt
  done
1) get password for user1 on srv1
2) get password for user3 on srv1
3) get password for user4 on srv1
4) get password for user2 on srv2

# 6  
Old 10-06-2016
If you do use a select loop, remember that you will need to use break to get out of the select loop. I try to avoid break everywhere else because it can be ambiguous as to which loop is being stepped out of.


Just my two-penneth-worth,
Robin
# 7  
Old 10-06-2016
Quote:
Originally Posted by rbatte1
If you do use a select loop, remember that you will need to use break to get out of the select loop. I try to avoid break everywhere else because it can be ambiguous as to which loop is being stepped out of.


Just my two-penneth-worth,
Robin
Hi Robin,
The command:
Code:
break N

should break out of the innermost N (1 if N is not specified) enclosing for, while, until, and (in shells that implement select loops) select loops. You get varying behavior if you try to break out of more loops than you are currently in OR if you try to break out of a loop invoking a function using a break command inside that function's defining text, but if you're writing portable code you just shouldn't attempt to do either of those things.

Is there some other issue with break that has caused you problems?

Note that you can also exit a select loop by it hitting EOF on the file from which it is reading. So, if you are reading from a keyboard, you can add a choice to the selection list that is something like Enter ctl-D to exit when done if you want to avoid using break in a select statement. (But, it would usually be a good idea to add a break associated with that label in case the user enters the number associated with that selection instead of following directions and hitting ctl-D. Smilie ) Or, you can set PS3 with something like:
Code:
PS3='Enter selection number (or ctl-D to exit): '

and avoid that possible confusion.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Dynamic menu selection? Help..

Hi guys, i would like to create a program that allow user to show the information of certain thing such as network card. I would like the menu to be dynamic, for example: my computer system have 2 network card inserted at the moment, therefore the menu will have 2 choice for the user. eth0... (12 Replies)
Discussion started by: malfolozy
12 Replies

2. Shell Programming and Scripting

Creating Dynamic Input or daa files

Hi, I got a requirement to automate the process. We have SLA files, there are some 80 SLA files comes from 1.30pm - 5.30pm. I was asked to write a script to check for the SLA files in the load directory, if the files come then we got to send the mail to the group, if the mails doesnt come... (1 Reply)
Discussion started by: afahmed
1 Replies

3. Web Development

Dynamic Drop Down Menu

I need to create a dynamic drop down menu which is populated by entries such as; htdocs/client1/index.php htdocs/client2/index.php htdocs/client3/index.php htdocs/client4/index.php etc. So htdocs/client*/index.php Is this possible? I know how to do this using normal arrays, but not... (2 Replies)
Discussion started by: JayC89
2 Replies

4. Shell Programming and Scripting

Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables? I am assigning a variable name to --> $user_var Then I am trying to change its underlying variable value by $((user_var))=$user_value .. its failing,, Please let me know if there is a way to do this dynamically.. FileA.props... (5 Replies)
Discussion started by: kchinnam
5 Replies

5. Shell Programming and Scripting

building output file from multiple input files

Hi there, I am trying to figure out a way to combine multiple sources with different data on a single file, and I am trying to find the best way to do it. I have multiple files, let's say A, B, C and D. A has a field in common with B, B has a field in common with C, and C has a field in... (2 Replies)
Discussion started by: ppucci
2 Replies

6. Shell Programming and Scripting

Dynamic Menu Help

I'm working on a menu to read folders in as menu selections then CD to the selected folder and display the contained files as menu selections for execution. I'm using the following to read in the file list but I get lost after that. I only read in files that begin with CAPs. The problem is... (3 Replies)
Discussion started by: ScottKe
3 Replies

7. Shell Programming and Scripting

dynamic Select menu

Hi all is menu driven by SELECT can be a dynamic ? My requirement is that i want SELECT to be created on run time not predefine . The select should be created as per the no of words in a file thanks in advance rawat (2 Replies)
Discussion started by: rawatds
2 Replies

8. UNIX for Advanced & Expert Users

building flat files in unix and importing them from windows

what is a flat file in unix? i have to import a unix flat files from windows based programme. my question is not to export from unix but only to import from windows only. how to build that flat files? how to create export to windows how to import from windows (3 Replies)
Discussion started by: tunirayavarapu
3 Replies

9. Shell Programming and Scripting

Dynamic select with multiple word menu items

Hello all, I'm developing a deployment script at work and at one point it would need to display something like this: Which version of ADMIN would you like to deploy? 1) 1.0.0 (store1, 10 Jan 2004) 2) 1.0.1 (store1, 20 Jun 2004) 3) 1.0.2 (store1, 15 Jul 2004) Select a version : I know... (5 Replies)
Discussion started by: domivv
5 Replies
Login or Register to Ask a Question