system function wont redirect output

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default system function wont redirect output

    hey, in my program im just trying to redirect the commands output but i
    get the error:
    sh: line 1: 1: command not found

    code:
    $return = system("tar czvf $tar $files > $logfile >2&1");

    if i take out the > $logfile >2&1 , it works fine but i get that error
    with it in, but the werid thing is it did work before this is very
    puzzling as i dont believe i changed anyhting and it did indeed work
    before, now as i understand it Learning Perl 3rd Edition says if its a
    simple cmd like ls then perl will just excute it if its anyhting more
    complicated the shell will, but yeah i need to redirct the output any1
    know why this isnt working as it should right?
    thanks.

    Steve Guest

  2. Similar Questions and Discussions

    1. Some 'System Calls' was Capturing system call output value
      On Friday, Nov 14, 2003, at 18:39 US/Pacific, Jerry Rocteur wrote: Wiggins is the one who deserves the point, since he was the one with the...
    2. why wont my output work
      <cfquery name="message" datasource="#Request.Site.DataSource#"> SELECT n.message, p.pagename from noticetext n, pages p WHERE...
    3. How to handle input and output of a program executed by System function in perl
      "Jürgen Exner" <jurgenex@hotmail.com> wrote in message news:<JmBKc.4704$Iz3.2470@nwrddc01.gnilink.net>... Hi, I think Perl is good at dealing...
    4. redirect output to a file
      Hi everybody I was wondering I've made a little script to make the 'w'-command a bit nicer to work with.. ---~/bin/w--- #/bin/bash cd w -sh...
    5. can't redirect output to a file?!
      i wrote a simple program that read data from STDIN but redirected in a pipe from a file parse it and wrote output to STDOUT,here is the code: ...
  3. #2

    Default Re: system function wont redirect output

    Quote Originally Posted by Steve View Post
    hey, in my program im just trying to redirect the commands output but i

    code:
    $return = system("tar czvf $tar $files > $logfile >2&1");

    I am new to this, but AFAIK 'system' takes an *array* as its only argument:

    my $args = ("tar", "czvf", "$tar", "$files", ">$logfile>2&1");
    $return = system($args);

    But that's just a guess.
    -rob
    Cranbo Guest

  4. #3

    Default Re: system function wont redirect output

    Quote Originally Posted by Cranbo View Post
    I am new to this, but AFAIK 'system' takes an *array* as its only argument:

    my $args = ("tar", "czvf", "$tar", "$files", ">$logfile>2&1");
    $return = system($args);

    But that's just a guess.
    -rob
    I figure this has been solved by the submitter at some time in the last 8 years ... but for completeness ....

    system will take either a string or a list/array ... if a string is supplied it the system command will check the contents and determine if any shell meta-chars are included in it (.e.g. > < & ; ). If they are then the string is executed in a sub-shell, if they are not as simple exec is done...

    So, for redirections it has to be the single-string version of the system command ...

    But back to the original issue ... I reckon its a typo .... the syntax for redirecting STDERR into the same place as STDOUT is 2>&1 NOT >2&1

    The typo causes the string is being mis-interpreted as 'redirect STDOUT into a file called '2' and put the job in the background, and then execute another command called '1'....
    j
    Matt Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139