Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Getting rid of $/ on linux

by kamal (Sexton)
on Jul 12, 2010 at 23:56 UTC ( #849132=perlquestion: print w/ replies, xml ) Need Help??
kamal has asked for the wisdom of the Perl Monks concerning the following question:

in the sample
my $cmd = 'ps efax 2>/dev/null|grep firstbo|grep -v grep|wc -l'; my $stat = $ssh->exec($cmd); print "Before chomp: " , length($cmd); print "|".$stat."|"; chop $stat; print "|".$stat."|"; print "After chomp: " , length($cmd);
i need to get rid of the new line which is the result of "wc -l" but neither chomp() nor chop() is working. Can anyone please help me Thanks, -Kamal. Javafan, i apologize for not providing a working script, this is my first time posting.
#!/usr/bin/perl use Net::SSH::Expect; use YAML; use Data::Dumper; use warnings; use strict; use diagnostics; ##################### # process the input ##################### my $in = YAML::LoadFile('test_input.yml'); my $version = $in->{test_instance}{version}; my $build = $in->{test_instance}{build}; my $host_data = $in->{host}[0]; my $modelnum = $host_data->{model_number}; my $company = $host_data->{company}; my $address = $host_data->{address}; #print Dumper($address), "\n"; my $ssh = Net::SSH::Expect->new( ssh_option => "-v -v -v", #ssh_option => "-v", #host => $address, host => "baseline-dc1", #host => "upgrade-dc1", #host => "upgrade-3d1", #host => "baseline-3d1", #host => "batterup-dc2", user => 'admin', #password => 'OPTIONAL', raw_pty => 1, #timeout => 2, log_stdout => 1, ssh_option => '-o StrictHostKeyChecking=no ' . '-o UserKnownHostsFile=/dev/null ' . '-o LogLevel=ERROR' ); $ssh->run_ssh(); #$ssh->exec("stty raw -echo"); $ssh->send("sudo -i"); $ssh->waitfor('Password:', 3); $ssh->send("password"); my $catschedular = $ssh->exec("cat /sys/block/sda/queue/scheduler" +); my $echoconf = $ssh->exec("echo '/usr/lib/bigloo/3.1a' >> /etc/ld. +so.conf"); my $stat = $ssh->exec("ps efax 2>/dev/null|grep firstbo|grep -v grep|w +c -l | perl -pe 'chomp'"); open OUT, "> /tmp/STAT.out"; print OUT $stat; close OUT; print "stat: |" . $stat . "|\n"; chomp $stat; my $newstat; $newstat = substr($stat, 0, 1); $stat = $newstat; print "new stat: |" . $newstat . "|\n"; print "old stat: |" . $stat . "|\n";
result:
0root@baseline-dc1:~# stat: |0root@baseline-dc1:~# | new stat: |0| old stat: |0| before...|0|after...|0|bat@batbot:~/tests/Install_ISO$ more /tmp/STAT. +out 0root@baseline-dc1:~#
as you can see that the last line is 0root@baseline-dc1:~# whereas it needs to be just a 0 hope this helps. i really appreciate ALL your comments and help.

Comment on Getting rid of $/ on linux
Select or Download Code
Re: Getting rid of $/ on linux
by ikegami (Pope) on Jul 13, 2010 at 00:43 UTC
    If there's a bug, it's more likely to be yours than in Perl. You're printing the length of the wrong variable.
Re: Getting rid of $/ on linux
by rowdog (Curate) on Jul 13, 2010 at 01:05 UTC

    This is just a guess but all this mucking around with line endings makes me think you might want to look over what Net::SSH::Expect has to say about psuedo-terminals.

Re: Getting rid of $/ on linux
by jwkrahn (Monsignor) on Jul 13, 2010 at 01:07 UTC

    This may work better:

    open CMD, '-|', 'ps', 'efax' or die "Cannot open pipe from ps: $!"; my $stat; while ( <CMD> ) { $stat++ if /firstbo/; } close CMD or warn $! ? "Error closing ps pipe: $!" : "Exit status $? from ps";
Re: Getting rid of $/ on linux
by JavaFan (Canon) on Jul 13, 2010 at 08:37 UTC
    You claim it's not working. But you don't say why. Your code isn't cut-paste-runnable, and you couldn't be bothered to include your output.

    What is the output, and what do you expect the output to be?

Re: Getting rid of $/ on linux
by graff (Chancellor) on Jul 13, 2010 at 08:45 UTC
    Here's a variant on jwkrahn's approach that's closer to what seems to be the real goal of the OP code:
    my $ps_output = $ssh->exec( 'ps efax 2>/dev/null' ); my @stat = ( $ps_output =~ /firstbo.*/g ); my $stat = scalar @stat;
    Counting matches locally is just easier. Note that the regex match here uses "firstbo.*" to make sure it matches "firstbo" only once per line (just like the remote "grep" command would have done).
Re: Getting rid of $/ on linux
by Fletch (Chancellor) on Jul 13, 2010 at 13:35 UTC

    Unrelated to your perl problems that others have already pointed out, but here's a handy little trick:

    Even if you do use grep on the remote end there's no need to run it twice. I don't remember where I found this originally, but if you include a character class with a single member you don't need to use the second -v invocation to weed out your grep process from ps results. E.g. for your example you'd do grep '[f]'irstbo; the brackets keep it from matching against the grep process in the results (since [f]irstbo can't match aganst grep [f]irstbo but does match the process you're looking for).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://849132]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-07-12 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls