Creating "menus" for script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating "menus" for script
# 1  
Old 02-14-2015
Creating "menus" for script

I have a script I'm working on. I needed to figure out a way to display the following information for our Linux servers: What application runs on the server (app), who owns the application (owner) and the hostname.

This is done by looking at a text file "test_owner_list" that has info like this:
<servername>,<owner>,<app>

test_owner_list is written as follows:
Code:
server1,user1,app1
server2,user2,app2
server3,user1,app3
server4,user3,app2
...

With the way it's written now, I could do something from a prompt like
Code:
<scriptname> -a java

and it will show me all servers with java

Or I could do something like:
Code:
<scriptname> -s server1 server2 server3

and it will show me the owner/app for the servers requested

I brought this up to my boss who told me "this is good, but I would rather use menus instead of remembering arguments."

My question is this. Is there a way to take my logic and and create a:
Code:
"Choose Option 1 to list all servers owned by a user:
 Choose Option 2 to list owner/app of a user:
 Choose Option 3 to list servers running an application:
 Note: Separate multiple entries with a space
    then 
    What is the app: 
Who is the user:
 What is the server: 
  (bam here's the information)"

My script is below.
Code:
#!/bin/ksh[/COLOR][/SIZE][/FONT]
 
#read the first command line param to be used as the execution option
option=$1;
#discard the first command line param so it doesn't interfere with the loop later
shift;

#if they didn't enter a valid option, exit
if [ "$option" != "-u" -a "$option" != "-s" -a "$option" != "-a" ]; then
   echo "valid options are -u, -s, and -a";
   echo "-u <username> will list all servers owned by that user";
   echo "-s <hostname> will list the owner/app for the given server";
   echo "-a <app> will list all servers used for a given app";
exit;
fi;

#loop through each of the remaining command line params
for param in "$@"; do
if [ "$option" = "-s" ]; then
   #grep the master list for lines beginning with the given server name
   #display the second and third fields on that line (owner and app respectively)
   echo $param `grep ^$param, test_owner_list | cut -d "," -f 2,3`;
elif [ "$option" = "-u" ]; then
   #grep the master list for lines with a username matching the provided one
   #display the first field on that line (server name)
   echo $param `grep ,$param, test_owner_list | cut -d "," -f 1`;
elif [ "$option" = "-a" ]; then
   #grep the master list for lines that end with the given app name
   #display the first field on that line (server name)
   echo $param `grep ,$param$ test_owner_list | cut -d "," -f 1`;
fi;
done;

Moderator's Comments:
Mod Comment
Please refrain from using FONT definitions in CODE blocks.
It makes it more difficult to read and multiple spaces are not properly preserved.

Last edited by rbatte1; 02-16-2015 at 01:36 PM.. Reason: Changed QUOTE to CODE tags for required output
# 2  
Old 02-14-2015
This is bash, but the select builtin exist in ksh as well:
Code:
select FCT in server user application quit
        do case $FCT in
           ser*) select SRV in $(cut -d, -f1 file4 | sort -u)
                        do grep $SRV file4
                        break
                        done    ;;

           app*) select APP in $(cut -d, -f3 file4 | sort -u)
                        do grep $APP file4
                        break
                        done    ;;

           use*) select USR in $(cut -d, -f2 file4 | sort -u)
                        do grep $USR file4
                        break
                        done    ;;
           quit)        exit;;
           *)           echo "input error; usage:"
           esac
        done

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-15-2015
Quote:
Originally Posted by RudiC
This is bash, but the select builtin exist in ksh as well:
Code:
select FCT in server user application quit
        do case $FCT in
           ser*) select SRV in $(cut -d, -f1 file4 | sort -u)
                        do grep $SRV file4
                        break
                        done    ;;

           app*) select APP in $(cut -d, -f3 file4 | sort -u)
                        do grep $APP file4
                        break
                        done    ;;

           use*) select USR in $(cut -d, -f2 file4 | sort -u)
                        do grep $USR file4
                        break
                        done    ;;
           quit)        exit;;
           *)           echo "input error; usage:"
           esac
        done

This is great! I'll try to work it into my script tomorrow. Thanks so much!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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 Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

5. Homework & Coursework Questions

Need help creating shell script with output that has 2014 calendar and 2 text items from a"fortune"

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am required to create a bash shell script with either emacs or vi. It must include the year 2014 calendar on... (9 Replies)
Discussion started by: dandanhelpmeman
9 Replies

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

7. Shell Programming and Scripting

Creating a Shortcut (to just type "l" but it runs "ls -lah")

How do I create shortcuts? For example: I just want to type one key "l" and have it output the command of "ls -lah" I believe it's creating a file called l with 755 permissions but I'm not sure where to put the file. *if it matters, I'm on a shared hosting web server using cPanel with... (2 Replies)
Discussion started by: ijustsawmars
2 Replies

8. Virtualization and Cloud Computing

Creating VirtualBox-Image as "harddisk" by shell-script

Hello, I'm trying to create a VirtualBox "harddisk" and put an dd-image into it. This image shoudn't work as a virtual maschine, I just want to be able to mount it to an folder. How can I do this with an shell script? Sebi ---------- Post updated at 10:36 AM ---------- Previous update... (0 Replies)
Discussion started by: Sebi0815
0 Replies

9. 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
Login or Register to Ask a Question