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

I write scripts in tcsh (sorry, not bash) and programs in Perl (in the past, I've written a lot in "C"). In both cases (tcsh and Perl), I have problems with filenames that contain special characters. I use (){}[]!# in most of my filenames and folder names, some of which came from the old days of windoz. My laptops run either Ubuntu or Debian and I use nautilus to access files and it understands these files and folder.

A Perl snippet as an example of the problem:

use String::ShellQuote; # this package almost n +ever works my $Temp = shell_quote_best_effort('BulletinV3, 1 Montana 20xx-xx-xx ( +print).pdf_FN_TEMPLATE'); if (-e $Temp) { `zenity --width 300 --info --text 'DBG> Got it!'`; } else { `zenity --width 300 --error --text 'DBG> Nope, not found!'`; # thi +s is what I get all the time } $rc = rename("BulletinV3, 1 Montana 20xx-xx-xx (print).pdf_FN_TEMPLATE +", "BulletinV3, 1 Montana $SundayDate (print).pdf_FN_TEMPLAT +E"); if($rc == 1) { $ExtTypes{'pdf_FN_TEMPLATE'}++; } else { `zenity --title \"Rename problem\" --width 400 --error --text \"Fi +le rename error (\\\"$!\\\"; \\\"BulletinV3, 1 Montana 20xx-xx-xx (pr +int).pdf_FN_TEMPLATE\\\")\"`; }

I know the file is there, I can see it, I can show it with ls (on the command-line), but the Perl program does not. I've tried all manner of quoting with backslashes, single and double quotes etc. I've tried using back-ticks with Linux 'mv' commands instead of rename() but in all the cases, nothing works. I've been plagued with this problem for years and, sometimes, I can find unique ways to deal with it. I'd like a way that I can use for all situations (it seems, nautilus has a solution that works).

Any help would be greatly appreciated.

Thanks,
  EigenFunctions
  OpSys: Ubuntu 18.04; Perl: Perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi (with 71 registered patches)

Replies are listed 'Best First'.
Re: Problems with special characters in Linux file names
by ikegami (Patriarch) on Sep 13, 2022 at 15:13 UTC

    You use shell_quote where you shouldn't. (And, arguably, you don't use it where you should.)

    -e needs a path, not a piece of shell code.

    my $src_qfn = 'BulletinV3, 1 Montana 20xx-xx-xx (print).pdf_FN_TEMPLAT +E'; if (-e $src_qfn) { ... } else { ... }

    Or with error checking:

    my $src_qfn = 'BulletinV3, 1 Montana 20xx-xx-xx (print).pdf_FN_TEMPLAT +E'; if ( stat( $src_qfn ) ) { ... } else { die( "Can't stat `$src_qfn`: $!\n" ) if !$!{ ENOENT }; ... }

    As for that message of command at the bottom,

    system( "zenity", "--title" => "Rename problem", "--width" => 400, "--error", "--text" => "Can't rename `$src_qfn` to `$dst_qfn`: $!", )

      I put the code snippet, which came directly from my program, into a file and, it works! There is so little code, I don't know how it works in one place and NOT the other.

      I'll post another comment as soon as I find what I did wrong.

      Sorry

      Thanks,
        EigenFunctions
        OpSys: Ubuntu 18.04; Perl: Perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi (with 71 registered patches)

        Sorry, sorry, sorry! Operator error! It wasn't the filename, the problem was I didn't include the path. I added the path and all is well.

        I'm so embarrassed. Please ignore this post!

        Thanks,
          EigenFunctions
          OpSys: Ubuntu 18.04; Perl: Perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi (with 71 registered patches)

Re: Problems with special characters in Linux file names
by afoken (Chancellor) on Sep 13, 2022 at 18:30 UTC
    use String::ShellQuote; # this package almost n +ever works

    I couldn't agree more with that comment: Problems with String::ShellQuote

    Are you sure that tcsh uses the same quoting rules as bourne shells? If not, you are probably in trouble. I don't really know, but a quick Google search suggests that there are differences. I found a pirated copy of "Unix Power Tools" (third edition) that has an entire chapter named "Differences Between Bourne and C Shell Quoting". It lists three differences, of which at least two are relevant for String::ShellQuote. In csh, ! can only be quoted by a backslash, it keeps its special meaning even inside single quotes. And the csh needs a backslash before newlines inside single and double quotes, unlike bourne shells.

    And by the way, shell_quote_best_effort() may damage data and hides errors. See Problems with String::ShellQuote.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Are you sure that tcsh uses the same quoting rules as bourne shells? If not, you are probably in trouble.

      This is completely bogus. -e does not use tcsh. Or any shell. Your entire post is a baseless irrelevant rant.

Re: Problems with special characters in Linux file names
by Anonymous Monk on Sep 16, 2022 at 06:17 UTC