Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Syntax details with "system"

by thewebsi (Scribe)
on Jul 20, 2012 at 05:49 UTC ( [id://982751]=note: print w/replies, xml ) Need Help??


in reply to Syntax details with "system"

Yes, the two forms are different.

In the first form, a system call (eg, the C function as implemented internally by the operating system for executing commands) is made with the arguments provided. In the second form, the string is passed to the shell to deal with. The difficulty that you are running into is with escaping of meta-characters (characters special to the shell, but not special to the internal system call).

The first form (system call) is equivalent to automatically escaping all arguments. So when you say above that the first form "works", it really doesn't, as it just passes ">$output_file" as an argument to the command, which in general won't do what you want. I don't know of any way to do something like piping to a file using the first form (you normally use open() for that instead).

The second form (shell command) probably doesn't work for you because you are not escaping characters that you want escaped. Escaping can be done automatically in Perl using quotemeta() or \Q...\E.

As an example, consider:

>echo two > one

If you want this to echo "two > one", then you need to escape the ">":

system "echo", "two", ">", "one";
system "echo two \\> one";
system "echo " . quotemeta ( "two > one" );
system "echo \Qtwo > one\E";

If you want "two" to be piped to a file called "one", then you don't:

system "echo two > one";


Replies are listed 'Best First'.
Re^2: Syntax details with "system"
by Anonymous Monk on Jul 20, 2012 at 17:57 UTC

    Thanks Arnon. That answer was useful. I suspect the 1st form works because of the specifics here (qrsh is a Sun Grid Engine command which opens a shell on a node and runs "lens ... 2>$logfile"). I didn't realize it made a difference for the answer.

    I finally found out what characters exactly needed to be escaped how many times

    system "qrsh $qrsh_options \"lens -n \\\"source $qscript ; test_model +$arguments\\\" >$output_file 2>$logfile\"";
    I just read about quotemeta and I suspect it wouldn't work for me because all the $ would be escaped and the perl variables wouldn't be resolved. But I will remember \Q...\E for the future - I'm sure it will come in handy sometime :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://982751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-23 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found