Next Unix Command Internals 13

World's Smallest Shell That Doesn't Completely Suck

        $| = 1;
        while (1) {
          print "perl% ";
          my $command = <STDIN>;
          last if $command eq '';
          chomp $command;
          my (%command) = parse($command);
          my $pid = fork;
          if (! defined $pid) { print STDERR "Couldn't fork: $!\n" }
          elsif ($pid != 0) { wait }
          # Handle redirections
          else { 
            if (defined $command{stdin}) {
              open STDIN, "<", $command{stdin} 
                or die "Couldn't redirect stdin from $command{stdin}: $!\n";
            }
            if (defined $command{stdout}) {
              open STDOUT, $command{append}, $command{stdout} 
                or die "Couldn't redirect stdout to $command{stdout}: $!\n";
            }
            exec $command{program}, @{$command{args}};
            die "Couldn't exec: $!\n";
          }
        }


Next Copyright © 2003 M. J. Dominus