perl - how to make output of a command a numbered list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl - how to make output of a command a numbered list
# 1  
Old 04-09-2010
perl - how to make output of a command a numbered list

Hi there, just wondered if somebody could help me with a problem I have

I have a program that when run from the command line will output a list of objects


Code:
# list_servers
server1
server5
server7
server8
#



I just wanted to know, in perl, how can i make each line of output from the above coimmand, a selectable option in an interactive perl script, so for example

Code:
# example.pl

Please select from the following available servers

1) server1
2) server5
3) server7
4) server8

enter selection:

is this possible in perl ?
# 2  
Old 04-09-2010
Quote:
Originally Posted by rethink
...
I just wanted to know, in perl, how can i make each line of output from the above coimmand, a selectable option in an interactive perl script, so for example

Code:
# example.pl
 
Please select from the following available servers
 
1) server1
2) server5
3) server7
4) server8
 
enter selection:

is this possible in perl ?
Yes, it is possible in Perl -

Code:
$
$
$ cat -n example.pl
     1  #perl -w
     2
     3  # define the main menu as a multiline string
     4  $main_menu = <<END
     5  Please select from the following available servers
     6
     7  1) server1
     8  2) server5
     9  3) server7
    10  4) server8
    11
    12  END
    13  ;
    14
    15  # print the main menu
    16  print $main_menu;
    17
    18  # prompt for user's choice
    19  printf("%s", "enter selection: ");
    20
    21  # capture the choice
    22  $choice = <STDIN>;
    23
    24  # and finally print it
    25  print "You entered: ",$choice;
    26
$
$ perl example.pl
Please select from the following available servers

1) server1
2) server5
3) server7
4) server8

enter selection: 4
You entered: 4
$
$

tyler_durden
# 3  
Old 04-09-2010
Thank you for responding, however, it wasnt so much the capture of the input (ive already got that bit written, but thanks anyway Smilie) but how to turn the output of a command 'dynamically' into a list

for example, the the output of the list_servers script I mentioned before is variable, and could produce a different list of servers each time.
# 4  
Old 04-10-2010
Quote:
Originally Posted by rethink
... how to turn the output of a command 'dynamically' into a list

for example, the the output of the list_servers script I mentioned before is variable, and could produce a different list of servers each time.
Sorry for not paying attention to your question.
Here's an idea to implement a dynamic list -

Code:
$ 
$ 
$ # run the program that prints list of servers
$ ./list_servers
server1
server5
server7
server8
$ 
$ #
$ # display the content of the Perl program
$ cat -n example.pl
     1    #perl -w
     2    
     3    # fill up the server list
     4    @servers = `./list_servers`;
     5    $num = 0;
     6    
     7    # print the main menu
     8    print "Please select from the following available servers\n\n";
     9    foreach $i (@servers) {
    10      print ++$num,")  $i";
    11    }
    12    
    13    # prompt for user's choice
    14    printf("\n%s", "Enter selection: ");
    15    
    16    # capture the choice
    17    $choice = <STDIN>;
    18    
    19    # and finally print it
    20    print "You entered    : ",$choice;
    21    print "So you chose   : ",$servers[$choice-1];
    22    
$ 
$ # now run the Perl program
$ perl example.pl
Please select from the following available servers

1)  server1
2)  server5
3)  server7
4)  server8

Enter selection: 3
You entered    : 3
So you chose   : server7
$ 
$

HTH,
tyler_durden

I haven't added any code for input validation though.

Last edited by durden_tyler; 04-11-2010 at 12:21 AM..
# 5  
Old 04-11-2010
using POSIX module kindly see below code:-

Code:
perl -M'Shell::POSIX::Select' -wle '
@servers=`cat ./list_servers`;
chomp(@servers) ;
select (@servers){print "You choose server $_"}
'

BR

SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Scripting - Select multiple files from numbered list

I am trying to have the user select two files from a numbered list which will eventually be turned into a variable then combined. This is probably something simple and stupid that I am doing. clear echo "Please Select the Show interface status file" select FILE1 in *; echo "Please Select the... (3 Replies)
Discussion started by: dis0wned
3 Replies

2. Shell Programming and Scripting

list files command output

Hi All, Below is the 2 different ouputs of the command "ls -lrt", my question is what exactly "total 0" & "total 8" means here ? $ ls -rtl total 0 -rw-r--r-- 1 oracle dba 0 Feb 10 20:16 c -rw-r--r-- 1 oracle dba 0 Feb 10 20:16 b -rw-r--r-- 1... (1 Reply)
Discussion started by: kannan84
1 Replies

3. Shell Programming and Scripting

rename numbered files to numbered files with leading zeroes

Hi, I have some hundreds/thousands of files named logX.dat, where X can be any integer, and they are sequential, X ranges between 1 and any number: log1.dat log2.dat log3.dat log6.dat log10.dat ... log6000.dat I would like to rename them to scatter_params_0001.dat... (6 Replies)
Discussion started by: pau
6 Replies

4. Shell Programming and Scripting

Output of a command in perl

Hi All If i run the following command nvidia-settings -V -q screens I get this output X Screen on sutton:0 sutton:0.0 (Quadro FX 4600) Is connected to the following display devices: DELL 2007FP (DFP-0: 0x00010000) Eizo CG243W (DFP-1:... (1 Reply)
Discussion started by: ab52
1 Replies

5. Shell Programming and Scripting

Output of command in comma separated list

Hi; I have an output of a particular command say $command fstl:r-x ajay:r-x how can i get this in comma separated list, eg: fstl:r-x,ajay:r-x Thnks; (4 Replies)
Discussion started by: ajaypadvi
4 Replies

6. Shell Programming and Scripting

Perl Parse word from command output

Hello, I used the following script to conect to cisco router: #!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$'); my $device =... (5 Replies)
Discussion started by: ahmed_zaher
5 Replies

7. Shell Programming and Scripting

how to read tail -F command output in perl

Hi All, How I will read the output of the tail -F command in perl. I have text file with below contains file1.txt 1 2 3 4 $running=1; sub openLog($) { (my $log) = @_; (1 Reply)
Discussion started by: pravin27
1 Replies

8. Shell Programming and Scripting

perl: looping through the output of a 'system' command

Hi there could anybody point me in the right direction when it comes to looping through the output of a system command in perl (i.e. df -k) doing a test against each line to see if it matches? for example if i have a df -k output like this and I wanted to grab the lines that matched "sda" or... (3 Replies)
Discussion started by: rethink
3 Replies

9. UNIX for Dummies Questions & Answers

add numbered bulletpoints to grep output

I would like to add "word -esque" numbered bulletpoints to the output of a grep search, which has multiple hits for the search term i.e. grep x filename 1) abcd x 2) efgh x 3) ijkl x I dont have a clue to how to start writing such a script, i cant use SED or AWK so i was maybe thinking... (2 Replies)
Discussion started by: rprules
2 Replies
Login or Register to Ask a Question