LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2012, 03:48 PM   #1
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Rep: Reputation: Disabled
passing perl script argument to an outside program


I have a program called checkuser, called from the shell with this syntax:
checkuser <user>@<domain> dsl

I am attempting to write a perl script that takes a username as an argument, and gets info from checkuser. The output is <100 lines, so I just want to pull it as a list of lines, straight to memory.

I hope the following will further suggest what I am attempting (though it still seems incorrect..):
my @checkuserpull = `checkuser $1@domain dsl`

As always, any assistance is greatly appreciated
 
Old 11-14-2012, 04:09 PM   #2
Birei
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Rep: Reputation: 6
Hi bspears1,

Arguments are saved in the array @ARGV. So use $ARGV[0] for first parameter, $ARGV[1] for the second one and so on.
Code:
my @checkuserpull = `checkuser ${ARGV[0]}@domain dsl`

Last edited by Birei; 11-14-2012 at 04:10 PM.
 
1 members found this post helpful.
Old 11-14-2012, 04:30 PM   #3
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thanks for the suggestion

@Birei,

So I tried:
Code:
#!/usr/bin/perl
my @checkuserpull = `checkuser ${ARGV[0]}@domain dsl`;
I then did:
./testscript test.account6

It replied:
checkuser: invalid user name

However, if I do the following, from the shell, it works just fine:
checkuser test.account6@domain dsl

So.. something still isn't being passed correctly.
 
Old 11-14-2012, 04:49 PM   #4
Birei
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Rep: Reputation: 6
Perl assumes that @domain is an array. You must escape it:
Code:
my @checkuserpull = `checkuser ${ARGV[0]}\@domain dsl`
 
1 members found this post helpful.
Old 11-14-2012, 04:56 PM   #5
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Original Poster
Rep: Reputation: Disabled
Sweet Thank you, Birei!
 
Old 11-14-2012, 07:09 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by bspears1 View Post
Sweet Thank you, Birei!
You are doing it methodologically wrong - regardless of language. Your fundamental mistake is the assumption you'll make no mistake.

If you change that assumption,. you'll start writing your code in the manner which allows you to easily check inputs and outputs of intermediate steps.

But it appears you haven't used

Code:
use strict;
use warnings;
in the beginning of your script in the first place - which would have pointed you to the mistake immediately.


Now, back to the wrong assumption - you have to write your code using the following idiom:

Code:
my $cmd = "checkuser ${ARGV[0]}@domain dsl"; # this line is actually wrong
warn "about to get stdout from $cmd command line"; # comment this line out when your code works
my @checkuserpull = `$cmd`;
.
 
1 members found this post helpful.
Old 11-14-2012, 07:29 PM   #7
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Original Poster
Rep: Reputation: Disabled
@Sergei

You assume that I made an assumption about my infallibility. In fact, I normally expect to mess it up, and find mistakes. I did not use

Code:
use strict;
use warnings;
because I am very new, and unaware of such options. They will be added, for sure, since I still have quite a bit to do.

Birei's alterations to my example made it possible to successfully get the information I need. If I have chosen a poor method, perhaps you could suggest a better way? It would be appreciated
 
Old 11-15-2012, 12:27 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by bspears1 View Post
@Sergei

You assume that I made an assumption about my infallibility. In fact, I normally expect to mess it up, and find mistakes. I did not use

Code:
use strict;
use warnings;
because I am very new, and unaware of such options. They will be added, for sure, since I still have quite a bit to do.

Birei's alterations to my example made it possible to successfully get the information I need. If I have chosen a poor method, perhaps you could suggest a better way? It would be appreciated
The method of getting the needed is OK, non-checking of actual command line to be executed is not.

My assumption of me screwing up command lines is carved in stone, code code that is:

Code:
cat -n system_wrapper.prl
     1  ###############################
     2  # written by Sergei Steshenko #
     3  ###############################
     4
     5  use strict;
     6
     7  sub
     8    {
     9    my ($info_marker, $command)=@_;
    10
    11    warn "$info_marker executing ==> ",$command,"\n";
    12    # ^
    13    # | - it should be to STDERR, not STDOUT, UNIX(R) traditions should be respected.
    14    system($command);
    15    };

cat -n backticks_wrapper.prl
     1  ###############################
     2  # written by Sergei Steshenko #
     3  ###############################
     4
     5  use strict;
     6
     7  sub
     8    {
     9    my ($info_marker, $warning_marker, $command)=@_;
    10
    11    warn "$info_marker going to grab STDOUT from '$command' command\n";
    12    my $stdout=`$command`;
    13    if($?)
    14      {
    15      warn "$warning_marker '$command' command returned non-zero status: \$?=$?";
    16      }
    17
    18    $stdout; # the returned value
    19    };
.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax for passing argument in shell script shridhar22 Programming 11 08-30-2012 04:42 PM
[SOLVED] passing globbing expressions as argument to bash script mdcjsps Linux - Newbie 6 10-29-2011 05:09 AM
[SOLVED] (Something like...) passing an argument to a MATLAB script Mike_V Programming 1 12-21-2009 04:04 PM
shell script, Passing string as argument? antis Programming 4 09-07-2007 01:03 AM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 09:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration