Jesse Hernandez
Jesse Hernandez

Reputation: 317

Reading a unix command output with Perl

I need to run a command with variables inside of the command. I need to capture the output and save it in either one line varible or an array, it doesn't matter. It's going to be pasted into a textbox in Tk anyways.

I've tried:

my @array = '$variable_with_command'; 

I can't really use:

my $variable = 'unixcommand $variableinside'; 

because of the variable inside, i've seen that as a suggestion other stackoverflow posts.

It works for:

my $readvariable = 'ls -a';

because there are no variables inside, unless there's a way to include variables and have $readvariable catch the output? Thanks in advance.

Here is my code (this printed zero):

sub runBatch {
    my $directory = "/directpath/directoryuser/sasmodules/";
    my $command = 'sasq ' . $directory . $selectBatch; 
    my @batch = system($command);
    print "This is the output\n";
    print "-----------------------------------";
    print @batch; 
    print "-----------------------------------";
}

Upvotes: 0

Views: 246

Answers (1)

Jesse Hernandez
Jesse Hernandez

Reputation: 317

This is the answer in case it helps anyone based on qx comment.

sub runBatch { 
    my @batch = qx(sasq $director);
    print "This is the output\n";
    print "-----------------------------------\n";
    print @batch; 
    print "-----------------------------------\n";
}

This actually works really well--using back quotes: `

sub runBatch {
    my $directory = "/path/directory/sasmodules/" . $selectBatch;
    my $command = `sasq $directory`; 
    #my @batch = command
    print "This is the output\n";
    print "-----------------------------------\n";
    print $command; 
    print "-----------------------------------\n";
    print $selectBatch;
}

Upvotes: 3

Related Questions