Double backslash behavior not as expected

Discussion in 'Perl Misc' started by alt.testing@{g}mail.com, May 4, 2007.

  1. Hi all,
    the simple concept of escaping a "backslash", thus translating to a
    literal backslash; does not provide the implied result, under the
    following example.

    $mount_result = qx#strace /bin/mount -t smbfs \\\\$machine\\documents
    /mnt/workstation_shares/$mount/documents/ -o ro -o username=$user -o
    password=$pass#;

    The top part of the "strace" output gives this:

    execve("/bin/mount", ["/bin/mount", "-t", "smbfs", "\\an2documents",
    "/mnt/workstation_shares/xxxx/doc"..., "-o", "ro", "-o",
    "username=xxxx", "-o", "password=xxxxxxxxxx"], [/* 22 vars */]) = 0

    "\\an2documents"

    Note; that there is no "\" in between the machine name, and the share.
    It should be "\\an2\documents".

    [root@mercedes sbin]# perl -e 'print "\\\\an2\\documents\n"'
    \\an2\documents


    I'm sure it's a simple thing, but can someone enlighten me on this
    parlay?

    ta.



    Full Context
    ===============================================================================


    #!/usr/bin/perl

    use strict;
    use warnings;

    my $input_vars_file = "/usr/local/etc/priv/users_test";
    my $tmp_vars;
    my $input_vars;
    my @input_vars_array;
    my @target_vars_array;

    my $user;
    my $pass;
    my $machine;
    my $mount;
    my $mount_result;

    open ( IN_FILE, "< $input_vars_file") or die "$!";

    while ( <IN_FILE> ){
    $tmp_vars = $_;
    $tmp_vars =~ s/(\s+)/,/g;
    $tmp_vars =~ s/;.*$//g;
    @input_vars_array = split( /,/, $tmp_vars );
    push @target_vars_array, [ @input_vars_array ] unless ( /^\W/ );
    }
    close IN_FILE;

    for my $counter ( 0 .. $#target_vars_array ) {

    $user = $target_vars_array[$counter][0];
    $pass = $target_vars_array[$counter][1];
    $machine = $target_vars_array[$counter][2];
    $mount = $target_vars_array[$counter][3];

    print "$user, $pass, $machine, $mount\n";
    $mount_result = qx#/bin/mount#;

    if ( $mount_result =~ /$machine\/documents/ ) {
    print "$machine: already mounted\n";
    }
    else {
    $mount_result = qx#strace /bin/mount -t smbfs \\\\$machine\\documents
    /mnt/workstation_shares/$mount/documents/ -o ro -o username=$user -o
    password=$pass#;
    }

    print "$mount_result\n";

    }

    exit
     
    alt.testing@{g}mail.com, May 4, 2007
    #1
    1. Advertising

  2. alt.testing@{g}mail.com wrote:
    > Hi all,
    > the simple concept of escaping a "backslash", thus translating to a
    > literal backslash; does not provide the implied result, under the
    > following example.
    >
    > $mount_result = qx#strace /bin/mount -t smbfs \\\\$machine\\documents
    > /mnt/workstation_shares/$mount/documents/ -o ro -o username=$user -o
    > password=$pass#;
    >
    > The top part of the "strace" output gives this:
    >
    > execve("/bin/mount", ["/bin/mount", "-t", "smbfs", "\\an2documents",
    > "/mnt/workstation_shares/xxxx/doc"..., "-o", "ro", "-o",
    > "username=xxxx", "-o", "password=xxxxxxxxxx"], [/* 22 vars */]) = 0
    >
    > "\\an2documents"
    >
    > Note; that there is no "\" in between the machine name, and the share.
    > It should be "\\an2\documents".
    >
    > [root@mercedes sbin]# perl -e 'print "\\\\an2\\documents\n"'
    > \\an2\documents
    >
    >
    > I'm sure it's a simple thing, but can someone enlighten me on this
    > parlay?


    Your string is interpolated twice, once by perl and then by the shell.

    $ perl -le'
    $machine = "an2";
    $mount = "xxxx";
    print qx#echo /bin/mount -t smbfs
    \\\\$machine\\documents/mnt/workstation_shares/$mount/documents/#;
    '
    /bin/mount -t smbfs \an2documents/mnt/workstation_shares/xxxx/documents/


    You need to double up on the back-slashes.

    $ perl -le'
    $machine = "an2";
    $mount = "xxxx";
    print qx#echo /bin/mount -t smbfs
    \\\\\\\\$machine\\\\documents/mnt/workstation_shares/$mount/documents/#;
    '
    /bin/mount -t smbfs \\an2\documents/mnt/workstation_shares/xxxx/documents/





    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, May 4, 2007
    #2
    1. Advertising

  3. On Fri, 04 May 2007 02:44:00 GMT, "John W. Krahn"
    <> wrote:

    >alt.testing@{g}mail.com wrote:
    >> Hi all,
    >> the simple concept of escaping a "backslash", thus translating to a
    >> literal backslash; does not provide the implied result, under the
    >> following example.
    >>
    >> $mount_result = qx#strace /bin/mount -t smbfs \\\\$machine\\documents
    >> /mnt/workstation_shares/$mount/documents/ -o ro -o username=$user -o
    >> password=$pass#;
    >>
    >> The top part of the "strace" output gives this:
    >>
    >> execve("/bin/mount", ["/bin/mount", "-t", "smbfs", "\\an2documents",
    >> "/mnt/workstation_shares/xxxx/doc"..., "-o", "ro", "-o",
    >> "username=xxxx", "-o", "password=xxxxxxxxxx"], [/* 22 vars */]) = 0
    >>
    >> "\\an2documents"
    >>
    >> Note; that there is no "\" in between the machine name, and the share.
    >> It should be "\\an2\documents".
    >>
    >> [root@mercedes sbin]# perl -e 'print "\\\\an2\\documents\n"'
    >> \\an2\documents
    >>
    >>
    >> I'm sure it's a simple thing, but can someone enlighten me on this
    >> parlay?

    >
    >Your string is interpolated twice, once by perl and then by the shell.
    >
    >$ perl -le'
    >$machine = "an2";
    >$mount = "xxxx";
    >print qx#echo /bin/mount -t smbfs
    >\\\\$machine\\documents/mnt/workstation_shares/$mount/documents/#;
    >'
    >/bin/mount -t smbfs \an2documents/mnt/workstation_shares/xxxx/documents/
    >
    >
    >You need to double up on the back-slashes.
    >
    >$ perl -le'
    >$machine = "an2";
    >$mount = "xxxx";
    >print qx#echo /bin/mount -t smbfs
    >\\\\\\\\$machine\\\\documents/mnt/workstation_shares/$mount/documents/#;
    >'
    >/bin/mount -t smbfs \\an2\documents/mnt/workstation_shares/xxxx/documents/
    >


    Thanks John,
    Nick



    >
    >
    >John
     
    alt.testing@{g}mail.com, May 4, 2007
    #3
  4. On Fri, 04 May 2007 02:44:00 +0000, John W. Krahn wrote:

    > Your string is interpolated twice, once by perl and then by the shell.
    >
    > $ perl -le'
    > $machine = "an2";
    > $mount = "xxxx";
    > print qx#echo /bin/mount -t smbfs
    > \\\\$machine\\documents/mnt/workstation_shares/$mount/documents/#; '
    > /bin/mount -t smbfs \an2documents/mnt/workstation_shares/xxxx/documents/
    >
    >
    > You need to double up on the back-slashes.


    Or use forward slashes. That also works for smb paths with smbmount and
    this shows why....

    HTH,
    M4
     
    Martijn Lievaart, May 4, 2007
    #4
  5. alt.testing@{g}mail.com

    Dan Mercer Guest

    "John W. Krahn" <> wrote in message news:Q5x_h.1690$Vi6.1400@edtnps82...
    : alt.testing@{g}mail.com wrote:
    : > Hi all,
    : > the simple concept of escaping a "backslash", thus translating to a
    : > literal backslash; does not provide the implied result, under the
    : > following example.
    : >
    : > $mount_result = qx#strace /bin/mount -t smbfs \\\\$machine\\documents

    Use:
    $mount_result = qx#strace /bin/mount -t smbfs '\\\\$machine\\documents'

    which the shell will see as '\\an2\documents'

    Dan Mercer


    : > /mnt/workstation_shares/$mount/documents/ -o ro -o username=$user -o
    : > password=$pass#;
    : >
    : > The top part of the "strace" output gives this:
    : >
    : > execve("/bin/mount", ["/bin/mount", "-t", "smbfs", "\\an2documents",
    : > "/mnt/workstation_shares/xxxx/doc"..., "-o", "ro", "-o",
    : > "username=xxxx", "-o", "password=xxxxxxxxxx"], [/* 22 vars */]) = 0
    : >
    : > "\\an2documents"
    : >
    : > Note; that there is no "\" in between the machine name, and the share.
    : > It should be "\\an2\documents".
    : >
    : > [root@mercedes sbin]# perl -e 'print "\\\\an2\\documents\n"'
    : > \\an2\documents
    : >
    : >
    : > I'm sure it's a simple thing, but can someone enlighten me on this
    : > parlay?
    :
    : Your string is interpolated twice, once by perl and then by the shell.
    :
    : $ perl -le'
    : $machine = "an2";
    : $mount = "xxxx";
    : print qx#echo /bin/mount -t smbfs
    : \\\\$machine\\documents/mnt/workstation_shares/$mount/documents/#;
    : '
    : /bin/mount -t smbfs \an2documents/mnt/workstation_shares/xxxx/documents/
    :
    :
    : You need to double up on the back-slashes.
    :
    : $ perl -le'
    : $machine = "an2";
    : $mount = "xxxx";
    : print qx#echo /bin/mount -t smbfs
    : \\\\\\\\$machine\\\\documents/mnt/workstation_shares/$mount/documents/#;
    : '
    : /bin/mount -t smbfs \\an2\documents/mnt/workstation_shares/xxxx/documents/
    :
    :
    :
    :
    :
    : John
    : --
    : Perl isn't a toolbox, but a small machine shop where you can special-order
    : certain sorts of tools at low cost and in short order. -- Larry Wall
     
    Dan Mercer, May 4, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,503
    Victor Bazarov
    Feb 17, 2005
  2. Harry George
    Replies:
    5
    Views:
    741
    Gerrit Holl
    Jan 19, 2004
  3. Vincent Texier
    Replies:
    3
    Views:
    2,273
    Vincent Texier
    Jul 1, 2004
  4. Fred
    Replies:
    2
    Views:
    369
    John Machin
    Mar 9, 2005
  5. Stef Mientki

    Double backslash in filepaths ?

    Stef Mientki, Apr 14, 2007, in forum: Python
    Replies:
    3
    Views:
    765
    Stef Mientki
    Apr 14, 2007
Loading...

Share This Page