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

Why is it that I am unable to successfully use the bang(!) in order to indicate that an output redirect ought to overwrite an existing file, if it encounters one? For example the command:
$ pwd >! tmp_1

works fine, but an equivalent system command executed in perl:
#!/usr/bin/perl -w use strict; use warnings; my ($util,$command,$redirect,$file); $util = "pwd"; $redirect = ">!"; $file = "tmp_1"; $command = sprintf("%s %s %s", $util, $redirect, $file); system($command); system("pwd >! tmp_1");
does nothing (in either of the two variations). Eliminating the bang(!) from the redirect, of course, accomplishes the desired effect, but I'm interested in knowing what's behind the apparent inconsistency.
Does this have something to do with the particular flavor of linux I'm using, or is it something to do with the way that perl handles system commands or...

Thanks!
joe

Replies are listed 'Best First'.
Re: Using the bang(!) in perl system commands
by ikegami (Patriarch) on Jun 06, 2006 at 05:02 UTC

    system uses sh on unix platforms (if the string to execute includes shell meta characters). Is >! an sh feature on your system? On mine, as best as I can tell, your code would be a misuse of the ! reserved word.

    Use the bourne shell syntax for what you are trying to do, or specify your shell explicitely:
    system('/bin/mysh', '-c', 'pwd >! tmp_1');

      Thanks! That was a great little explanation.
      I am using the tcsh shell, by the way - for which '>!' is legal, though redundant (with regard to my setup). Happy trails.

      jOe

        More than likely your version of perl was built with some version of sh and system uses that. Some versions of sh (bash) have a noclobber setting that prevents overwriting of existing files. I would check either the user's shell settings (~/.bashrc for bash) or the default system settings (/etc/bashrc) for a noclobber setting and reset appropriately.

        If you cannot unset the noclobber setting, you will have to use the syntax appropriate for the shell being used by perl -- which for bash is the rather clumsy >|

        -derby
A reply falls below the community's threshold of quality. You may see it by logging in.