LinuxQuestions.org
Help answer threads with 0 replies.
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 03-07-2018, 06:24 AM   #1
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 146

Rep: Reputation: 20
Perl: disappearing variable content


Hi,

I'm faced with a strange disappearing value of a variable in Perl. Somewhere in my script I have the following code:

Code:
print Dumper(@uniqNfsMounts);

foreach (@uniqNfsMounts)
{
  $mountPoint = (split /,/,$_)[0];
  $nfsServer  = (split /,/,$_)[1];
  $line = $nfsServer . ":" . $mountPoint . "\t" . $mountPoint . "\tnfs\tbg,intr\t0 0\n";
  print "DEBUG: mountpoint: $mountPoint\n";
  print "DEBUG: NFS Server: $nfsServer\n";
  print "DEBUG: $line";
}
When I run it I get the following output:
Code:
';AR1 = '/sapinstall,slkssr002
';AR2 = '/sapinstall,slkssr001
DEBUG: mountpoint: /sapinstall
DEBUG: NFS Server: slkssr002
:/sapinstallr002/sapinstall     nfs     bg,intr 0 0
DEBUG: mountpoint: /sapinstall
DEBUG: NFS Server: slkssr001
:/sapinstallr001/sapinstall     nfs     bg,intr 0 0
So, from the print statements I can see that the variable $nfsServer gets filled with the values slkssr002 resp. slkssr001. However, in the assignment to $line, the value is suddenly gone.

Could anybody help me solve this mistery? I'm really lost at why this content disappeared.

Kind regards,
Louis
 
Old 03-07-2018, 06:40 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,898

Rep: Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241Reputation: 3241
hm. It looks quite strange for me. Probably I would try to debug it, that may help to find an answer.
 
Old 03-07-2018, 06:40 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,348

Rep: Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502Reputation: 1502
My guess is you have a carriage return ("\r") in your $nfsServer, this moves the cursor back to the beginning of line after printing, so that subsequent printing overwrites earlier text.

Example:
Code:
~/src$ perl -e 'print "12345\rbar\n"'
bar45
~/src$ perl -e 'print "12345\rbar\n"' | cat -v
12345^Mbar
 
2 members found this post helpful.
Old 03-07-2018, 07:30 AM   #4
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 146

Original Poster
Rep: Reputation: 20
Quote:
Originally Posted by ntubski View Post
My guess is you have a carriage return ("\r") in your $nfsServer, this moves the cursor back to the beginning of line after printing, so that subsequent printing overwrites earlier text.

Example:
Code:
~/src$ perl -e 'print "12345\rbar\n"'
bar45
~/src$ perl -e 'print "12345\rbar\n"' | cat -v
12345^Mbar
Hi,

Thanks. That was exactly it. I've now added this:
Code:
chop($nfsServer) if ($nfsServer =~ m/\r$/);
and now it works like a charm.
 
Old 03-07-2018, 08:11 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,156

Rep: Reputation: 698Reputation: 698Reputation: 698Reputation: 698Reputation: 698Reputation: 698
Not sure why you need to test if it contains \r before removing it, this will work too:
Code:
foreach (@uniqNfsMounts) {
    s/\r+$//;
    ($mountPoint, $nfsServer) = split /,/;

    $line = $nfsServer . ":" . $mountPoint . "\t" . $mountPoint . "\tnfs\tbg,intr\t0 0\n";
    print "DEBUG: mountpoint: $mountPoint\n";
    print "DEBUG: NFS Server: $nfsServer\n";
    print "DEBUG: $line";
}

Last edited by keefaz; 03-07-2018 at 08:14 AM.
 
Old 03-07-2018, 08:38 AM   #6
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 146

Original Poster
Rep: Reputation: 20
Quote:
Originally Posted by keefaz View Post
Not sure why you need to test if it contains \r before removing it, this will work too:
Code:
foreach (@uniqNfsMounts) {
    s/\r+$//;
    ($mountPoint, $nfsServer) = split /,/;

    $line = $nfsServer . ":" . $mountPoint . "\t" . $mountPoint . "\tnfs\tbg,intr\t0 0\n";
    print "DEBUG: mountpoint: $mountPoint\n";
    print "DEBUG: NFS Server: $nfsServer\n";
    print "DEBUG: $line";
}
Thanks, that's indeed a more elegant solution.
 
Old 03-07-2018, 08:58 AM   #7
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 1,020

Rep: Reputation: 342Reputation: 342Reputation: 342Reputation: 342
What keefaz said...no test is required, just:
Code:
chomp($nfsServer)
[using chomp() because it only removes newline characters, where chop() removes the last character even if it's not a newline]
EDIT: Oh! newline would be \n, not \r. "Never mind" --G.Radner

Last edited by scasey; 03-07-2018 at 09:06 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
[SOLVED] setting new variable to regex of another without changing the other variable in perl Eppo Programming 1 12-22-2011 02:35 PM
File content by field into variable vijayrc Linux - Newbie 3 06-23-2009 02:10 PM
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 03:51 PM
sdl-perl: disappearing windows jrtayloriv Programming 1 02-02-2005 12:55 AM

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

All times are GMT -5. The time now is 01:54 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration