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, 07:24 AM   #1
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 155

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, 07:40 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,636

Rep: Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502Reputation: 3502
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, 07:40 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,390

Rep: Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554Reputation: 1554
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, 08:30 AM   #4
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 155

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, 09:11 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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 09:14 AM.
 
Old 03-07-2018, 09:38 AM   #6
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 155

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, 09:58 AM   #7
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 2,226

Rep: Reputation: 701Reputation: 701Reputation: 701Reputation: 701Reputation: 701Reputation: 701Reputation: 701
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 10: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 02:48 PM
[SOLVED] setting new variable to regex of another without changing the other variable in perl Eppo Programming 1 12-22-2011 03:35 PM
File content by field into variable vijayrc Linux - Newbie 3 06-23-2009 03:10 PM
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 04:51 PM
sdl-perl: disappearing windows jrtayloriv Programming 1 02-02-2005 01:55 AM

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

All times are GMT -5. The time now is 08:06 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