I am trying to redirect perl system command to output file with the following code along with time but its not working ??
$cmd="echo hi";
($second, $minute, $hour) = localtime();
$time="$hour:$minute:$second";
system("$time>new.txt");
system("$cmd 1>>new.txt 2>>&1");
If you want to write the variable
$timeto a text file,opena writeable filehandle and print it to your file instead.Secondly, your output redirection should read:
Which means “append STDOUT (1) to new.txt, redirect STDERR (2) to STDOUT (1)”. Having
>>makes no sense for the second part.Finally, I (and every other perl programmer) would strongly recommend using
strictandwarningspragmas in your scripts. This will help you pick up on any errors or potential problems in your scripting. Once you’ve done this, all variables must be declared withmy, which is a good habit to get in to anyway. So after all that, your script should look something like this: