Sponsored Content
Top Forums Shell Programming and Scripting perl - how to make output of a command a numbered list Post 302412012 by durden_tyler on Saturday 10th of April 2010 10:55:50 PM
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..
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. 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
NPM-RUN-SCRIPT(1)														 NPM-RUN-SCRIPT(1)

NAME
npm-run-script - Run arbitrary package scripts SYNOPSIS
npm run-script <command> [--silent] [-- <args>...] alias: npm run DESCRIPTION
This runs an arbitrary command from a package's "scripts" object. If no "command" is provided, it will list the available scripts. run[-script] is used by the test, start, restart, and stop commands, but can be called directly, as well. When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts. As of ` https://blog.npmjs.org/post/98131109725/npm-2-0-0, you can use custom arguments when executing scripts. The special option -- is used by getopt https://goo.gl/KxMmtG to delimit the end of the options. npm will pass all the arguments after the -- directly to your script: npm run test -- --grep="pattern" The arguments will only be passed to the script specified after npm run and not to any pre or post script. The env script is a special built-in command that can be used to list environment variables that will be available to the script at run- time. If an "env" command is defined in your package, it will take precedence over the built-in. In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix. For example, if there is a devDependency on tap in your package, you should write: "scripts": {"test": "tap test/*.js"} instead of "scripts": {"test": "node_modules/.bin/tap test/*.js"} to run your tests. The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of ` https://github.com/npm/npm/releases/tag/v5.1.0 you can customize the shell with the script-shell configuration. Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run. npm run sets the NODE environment variable to the node executable with which npm is executed. Also, if the --scripts-prepend-node-path is passed, the directory within which node resides is added to the PATH. If --scripts-prepend-node-path=auto is passed (which has been the default in npm v3), this is only performed when that node executable is not found in the PATH. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install, just in case you've forgotten. You can use the --silent flag to prevent showing npm ERR! output on error. You can use the --if-present flag to avoid exiting with a non-zero exit code when the script is undefined. This lets you run potentially undefined scripts without breaking the execution chain. SEE ALSO
o npm help 7 scripts o npm help test o npm help start o npm help restart o npm help stop o npm help 7 config January 2019 NPM-RUN-SCRIPT(1)
All times are GMT -4. The time now is 04:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy