473,390 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 35974
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,509 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

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

Similar topics

4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
0
by: Titof | last post by:
Hello, i want to create an active webpage that displays the status of users quotas on a volume of a server. I installed IIS on this server and installed Win2k server Resource Kit because the...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
1
by: Nathan | last post by:
This may be a basic task to some of you perl guru's, but I have a problem getting a perl script i've written, when executed by httpd, to send out an e-mail. Basically, i have a few variables...
3
by: bossingdarwin | last post by:
Hi Guys, I'm new to Perl and to this forum and I hope you gurus here help me. I am building a perl script that will telnet to one of my cisco router and execute commands. I was able to do this...
4
by: jram01 | last post by:
Hi Folks, I am facing problem to to execute .sql command in sql prompt using perl script. I am able to connect sql promt using " my $odbcinput = "odbcinput.txt"; my...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.