Operating System - HP-UX
1837900 Members
3171 Online
110123 Solutions
New Discussion

Re: RE: Assigning a Variable to a search

 
Michael Funke_1
Occasional Contributor

RE: Assigning a Variable to a search

I'm using Perl to do a search within OpenMail for our users and once this search feature is made, I want the output to be assigned a variable. What's the best way to do this in Perl?
"Sleep, who needs sleep?"
3 REPLIES 3
John Eaton
Frequent Advisor

Re: RE: Assigning a Variable to a search

I assume you want to assign the return of your command to a variable, rather that the other way around (your wording suggested this). Try this:
$YOURVARIABLE = `your_command`
The backticks work like they do with shell programming, including variable expansion.
Michael Funke_1
Occasional Contributor

Re: RE: Assigning a Variable to a search

John, basically, I'm looking through a string of information and ignoring the first 3 characters and need to pull the next 5 characters as the system UID #. In order to do a password reset function in OpenMail, I grab this UID # according to the name information that the user inputs on the form.

This is what I have so far:

$UID=qx(/opt/openmail/bin/omshowu -n $first_name $middle_name $last_name $genqua
l |grep System |awk -F":" '{print $2}'|awk -F" " '{print $1}');

But, I need to somehow capture the next 5 characters as the UID on every user on this server. Any idea?

Best Regards,


Michael Funke

"Sleep, who needs sleep?"
Ralph Grothe
Honored Contributor

Re: RE: Assigning a Variable to a search

Micheal,

what is the output of the omshowu command with the args as supplied?
I'm afraid, we're not using Openmail, that's why I neither know the command nor its output.
But I know a bit of Perl, and possibly could help you with the parsing of the output.

Meanwhile just some general suggestions.

If you are expecting large chunks of output from a command, it's preferable not to use the backticks, system, or qx calls but rather pipe it into a while loop because it keeps the stack small for each line is swallowed sequentially.

e.g.

open OMSHOWU, "/opt/openmail/bin/omshowu arg1 arg2 |" || die "couldn't pipe: $!\n";
while () {
# parse lines here
}
close OMSHOWU;

Then if already using Perl I wouldn't want to rely on something inferior like awk.
The regex and parsing features are much superior to anything else.
That's why Perl once was thought up. ;-)

If you use backticks, don't forget to do a chop or chomp afterwards because usually you want to get rid of the record separator \n.

Gathering from your awk field separators (i.e. ":") I assume the format is similar to that of /etc/passwd, which would make things fairly easy.

Have you had a look at CPAN yet?
Maybe someone has already come up with a module to parse OpenMail output/databases.
Then I'd strongly advise to make use of such a module.


Madness, thy name is system administration