TonyNY has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm trying to debug a string but for some reason it is printing on 3 lines instead of 2. Perhaps I am not concatenating the

string properly?

my $wgetlog = + '/usr/sfw/bin/wget -r -np --reject "index.html*" --https-only --no- +check-certificate --user ' . $userid . + ' --password ' . $password . ' https://' . $server . '/dev/' . $user +id . '/' . $actionid . "_" . $pkgname . '/'; print "$wgetlog\n";

here are my results:

/usr/sfw/bin/wget -r -np --reject "index.html*" --https-only --no-che +ck-certificate --user user01 --password password01 https://server01 /dev/user/99129 _DEPLOYMENT/

Please advise, thanks

Replies are listed 'Best First'.
Re: var string not printing correctly
by NetWallah (Canon) on Jun 29, 2018 at 01:32 UTC
    Your $server and $actionid strings are probably terminated by "\n".

    Try to "chomp" them before using in this .

    print "Server->$server<-\n";

                    Memory fault   --   brain fried

      $actionid was the one giving me the problem, chomped it now I'm good to go, thanks NetWallah!

      Thanks NetWallah. I tried the string with the server and actionid vars entered in manually but still got the same result.

Re: var string not printing correctly
by haukex (Archbishop) on Jun 29, 2018 at 08:29 UTC

    Assuming that NetWallah is right and you've got newlines on those variables, that makes me think you're getting them from user input (a file or the console). Note that this potentially opens you up to a big security hole! I would recommend using an array to store your command, because this will let you use the safer form of system where you give it a list with more than one element. You may also want to use a module if you want to capture the output of the external command, such as capturex from IPC::System::Simple. I wrote about this topic at length here.

    my @wgetlog = ('/usr/sfw/bin/wget','-r','-np', '--reject','index.html*','--https-only','--no-check-certificate', '--user',$userid,'--password',$password, "https://$server/dev/$userid/${actionid}_$pkgname");

      Thanks haukex, I will give this a try.

        bliako made a very good point that reminded me I forgot to mention: Using the "list" form of system, or a module that avoids the shell, also avoids the issue of having to escape special characters in the strings!

Re: var string not printing correctly
by hippo (Archbishop) on Jun 29, 2018 at 13:14 UTC

    Here you go:

    $ cat TonyNY.pl #!/usr/bin/env perl use strict; use warnings; my $userid = 'u'; my $password = 'p'; my $server = 's'; my $actionid = 'a'; my $pkgname = 'p'; my $wgetlog = '/usr/sfw/bin/wget -r -np --reject "index.html*" --https-only --n +o-check-certificate --user ' . $userid . ' --password ' . $password . ' https://' . $server . '/dev/' . $userid . '/' . $actionid . "_" . $pkgname . ' +/'; print "$wgetlog\n"; $ ./TonyNY.pl /usr/sfw/bin/wget -r -np --reject "index.html*" --https-only --no-che +ck-certificate --user u --password p https://s/dev/u/a_p/ $ ./TonyNY.pl | wc -l 1

    Take this SSCCE, and put your own variable content in it. You'll soon find where you are going wrong (Hint: It's your $actionid value which is problematic)

    And just to solve the XY problem as well, see LWP::UserAgent or any of the other web-retrieval tools on CPAN so you don't have to shell out in such a convoluted fashion in the first place.

Re: var string not printing correctly
by bliako (Abbot) on Jun 29, 2018 at 10:58 UTC

    ... good answer (as well as the suggestion to use a module to spawn command and capture output) but should n't your expected output to be on just 1 line (and not 2 as you ask)? Or do you assume that some variables will end in a newline and some wont?

    Btw that password needs to be quoted, in single quotes (if you are in unix/osx etc., don't know the whims of win).

    " --password '".$password."'"

    Once I used this as my password: `rm -rf / && echo 734:8aA_ags_`. Plenty of special characters in there to satisfy the most strict password strength checker.

      password: `rm -rf / && echo 734:8aA_ags_`

      Now that's a password xkcd://Bobby Tables's mom would approve of. :-)

      I'm using Solaris BTW, thanks.