$exit=1; while ($exit) { print "Please enter a command.\n"; print "Enter 'help' to get a list of possible commands.\n"; print "command: "; chop ($command=); # The 'help' command if ($command eq "help") { print "list (lists files in the current directory)\n use: list [regular_expression]\n\n display (displays the contant of a file)\n use: display inputfilename [regular_expression] [outputfilename]\n\n edit (replaces a string by another in a file)\n use: edit inputfilename replaced replacedby [outputfilename]\n\n system commands (allows execution of other unix commands)\n use: (see Unix manuals)\n"; } # The 'list' command elsif ($command=~/list/) { if ($command=~/list /) { $command=~s/list //; $dir= shift || '.'; opendir DIR, $dir or die "Can't open directory $dir: $!\n"; while ($file= readdir DIR) { if ($file=~/$command/) { print "$file\n"; } } } else {$dir= shift || '.'; opendir DIR, $dir or die "Can't open directory $dir: $!\n"; while ($file= readdir DIR) { print "$file\n"; } } } # The 'display' command elsif ($command=~/display/) { @output; @display = split / / , $command; # For 'display inputfilename [regular_expression] [outputfilename]' if ($display[3]=~/./) { open COMMAND, $display[1] or die "Cannot open $command for read :$!"; @list=; print "Lines containing the string '$display[2]' within '$display[1]' are:\n\n"; $index=0; foreach (@list) { if ($_=~/$display[2]/) { print "$_"; $output[$index]=$_; $index++;} } print "\n"; $out=$display[3]; open OUT, ">$out" or die "Cannot open $out for write :$!"; $index=0; foreach (@output) { print OUT $output[$index]; $index++;} } elsif ($display[2]=~/./) { open COMMAND, $display[1] or die "Cannot open $command for read :$!"; @list=; $index=0; print "Content of '$display[1]' is:\n\n"; foreach (@list) { if ($_=~/$display[2]/) { print "$_"; $output[$index]=$_; $index++;} } print "\n"; } else { open COMMAND, $display[1] or die "Cannot open $command for read :$!"; @list=; print "Content of '$display[1]' is:\n\n"; foreach (@list) { print "$_"; } print "\n"; } } # The 'edit' command elsif ($command=~/edit/) { @edit = split / / , $command; if ($edit[4]=~/./) { @ARGV="$edit[1]"; $^I=".bk"; while (<>) { s/$edit[2]/$edit[3]/; print; } open COMMAND, $edit[1]; @list=; open OUTPUT, ">$edit[4]"; $i=0; foreach (@list) { print OUTPUT $list[$i]; $i++;} unlink("$edit[1]"); open BACKUP, "$edit[1].bk"; @list2=; open OUT, ">$edit[1]"; $i=0; foreach (@list2) { print OUT $list2[$i]; $i++;} unlink("$edit[1].bk"); } else { open COMMAND, $edit[1]; print "\nEdited content of '$edit[1]' is:\n"; while () { s/$edit[2]/$edit[3]/; print; } print "\n"; } } # To exit elsif ($command=~/exit/) { $exit=0; } # System commands else { system("$command"); print "\n\nResult: $?\n\n"; } }