All pastes #329580 Raw Edit

Something

public text v1 · immutable
#329580 ·published 2007-01-26 21:24 UTC
rendered paste body
$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=<STDIN>);

# 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=<COMMAND>;
		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=<COMMAND>;
		$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=<COMMAND>;
		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=<COMMAND>;
		open OUTPUT, ">$edit[4]";
		$i=0;
		foreach (@list) {
			print OUTPUT $list[$i];
			$i++;}
		unlink("$edit[1]");
		open BACKUP, "$edit[1].bk";
		@list2=<BACKUP>;
		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 (<COMMAND>) {
        		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";
}

}