Perl script execute system() command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skyy
    New Member
    • May 2007
    • 109

    Perl script execute system() command

    Hi i am trying to use the system() command to run some linux command using perl script...

    Eg.. Deleting files.

    system("rm $filename");

    However, when my $filename contain " character, the sommand wont work.

    Any idea how to go around this?
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    The simple solutions is to use unlink:

    perldoc unlink

    - Miller

    Comment

    • skyy
      New Member
      • May 2007
      • 109

      #3
      Originally posted by miller
      The simple solutions is to use unlink:

      perldoc unlink

      - Miller
      Hi... the unlink process can be used to delete files..

      However if i want to use the system command to create directories or listing directories with " character?

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        Originally posted by skyy
        Hi... the unlink process can be used to delete files..

        However if i want to use the system command to create directories or listing directories with " character?
        For creating directories, use the mkdir command.

        perldoc mkdir

        For listing directories, you the opendir or glob.

        perldoc opendir
        perldoc glob

        For most system tasks, especially file operations, there are often perl functions to accomplish them. This prevents the need of having to deal with specific system quirks, which honestly isn't really a perl issue.

        - Miller

        Comment

        • skyy
          New Member
          • May 2007
          • 109

          #5
          Hi.. thanks for your help and prompt reply..

          Sorry i think i didnt explain clearly..

          i need to use the system command to run a command...

          system("sudo -u root rm $filename");

          i am using it to run a sudo command which allow the login user to be acting as root. Is there anything for perl? Thanks alot!

          Comment

          • miller
            Recognized Expert Top Contributor
            • Oct 2006
            • 1086

            #6
            No, you didn't explain clearly. However, I stick by my advice about avoiding the system command unless there is a specific reason why you must use it.

            Nevertheless, say you had a file named ". Doing rm " would not work as the rm command would think that you were beginning a string. The way that you fix this at the command prompt is to escape the double quotes with a backslash. You must do the same with the filenames in your perl system command that you provided.

            [CODE=perl]
            $filename =~ s/"/\\"/g;
            system(qq{sudo -u root rm "$filename" });
            [/CODE]

            - Miller

            PS
            An easier solution would be to simply not have filenames with double quotes in them.

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Hello! I have about 10 years experience with Unix and must say that I have never come across anyone who had double quotes in their file names. Sure, people like to have special characters in their passwords as it increases the strength of the password, but never in the file name. There are certain things you don't want to do in unix as a matter of practice. You don't use spaces in file names (because Unix doesn't like spaces natively), and you really shouldn't use special characters (such as double quotes) in the file names either. If you had ever accidently created a file with the name * (yes, that's an asterisk) and was then left figuring out how to remove it ( he he, "rm * and rm -rf *" are just no-no's), then you would understands why.

              As for system commands, I tend to lean towards using the backtics(next to the number 1 key) when executing system commands as it seems a little easier to me:

              ie: `sudo some command line`

              Regards,

              jlk

              Comment

              • rahmud
                New Member
                • Jun 2007
                • 1

                #8
                Hay

                I have the same problem under Windows. When i trying to run HTML file in IEXPLORER which has () in file name such like this:

                $filename="3200 5D0360R(02)[pl].html"

                and I using:

                system ($filename)

                then system divide the filename to two parts "32005D0360 R" and "(02)[pl].html" because system needs to have " on the beginning and end of the literals with spaces or brackets - and its does not work. It works only with filenames without metacharacters.

                There is no such way like qq{"$filename" } or \Q"$filename" \E to send to system " because perl has a big bug in this part :(

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  try quotemeta():

                  $filename = "32005D0360R(02 )[pl].html";
                  $filename = quotemeta($file name);
                  system ($filename)

                  Comment

                  • miller
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1086

                    #10
                    Originally posted by rahmud
                    There is no such way like qq{"$filename" } or \Q"$filename" \E to send to system " because perl has a big bug in this part :(
                    Hi rahmud,

                    Just because you do not know how to do something, does not mean that it cannot be done. Handling special characters properly is the responsibility of any coder. As I explained in my previous post, you cannot simply include a random double quote in a file name when working from the command prompt. Instead, you must escape it in order to get unix to work with it properly.

                    There are often similar types of experiences within windows. In order to navigate to the directory My Documents, you must enclose the entire thing in double quotes.

                    cd "My Documents"

                    This is just the necessary burden for people who use more than just simple alphanumeric characters in file and directory names.

                    When running system commands, perl can only handle these types of issues as well as the native operating system can. If you were trying to run the above change directory command, you would also have to include double quotes.

                    system("cd \"My Documents\"");

                    Is this a bug? no. This is just how you must communicate with other environments. In the language and specification that they'll understand.

                    This is why I originally suggested to the poster that the simpliest method is to simply avoid system level interaction and stick with pure perl solutions. It avoids the need for escape and knowing what type of operating system that perl is running on.

                    - Miller

                    Comment

                    Working...