469,326 Members | 1,351 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Perl script execute system() command

109 100+
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?
May 29 '07 #1
9 35640
miller
1,089 Expert 1GB
The simple solutions is to use unlink:

perldoc unlink

- Miller
May 29 '07 #2
skyy
109 100+
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?
May 29 '07 #3
miller
1,089 Expert 1GB
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
May 29 '07 #4
skyy
109 100+
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!
May 29 '07 #5
miller
1,089 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. $filename =~ s/"/\\"/g;
  2. system(qq{sudo -u root rm "$filename"});
  3.  
- Miller

PS
An easier solution would be to simply not have filenames with double quotes in them.
May 29 '07 #6
numberwhun
3,503 Expert Mod 2GB
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
May 29 '07 #7
rahmud
1
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="32005D0360R(02)[pl].html"

and I using:

system ($filename)

then system divide the filename to two parts "32005D0360R" 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 :(
Jun 1 '07 #8
KevinADC
4,059 Expert 2GB
try quotemeta():

$filename = "32005D0360R(02)[pl].html";
$filename = quotemeta($filename);
system ($filename)
Jun 1 '07 #9
miller
1,089 Expert 1GB
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
Jun 1 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Mark Wilson CPU | last post: by
52 posts views Thread by Olivier Scalbert | last post: by
reply views Thread by Titof | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
1 post views Thread by Nathan | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.