Thursday, October 15, 2009

AWK terminal, consol or shell

This is a small awk program, which makes you able to execute commands in a simple shell:

BEGIN {
ARGC = 2; ARGV[1] = "-"
do {
printf("prompt>")
while ((input = getline) > 0)
if ($0 ~ "exit") {
print "Exit!"
input = 0;
break
} else if ($0 ~ "help") {
print "commands: help, cmd1, cmd2, exit"
break
} else if ($0 ~ "cmd1") {
print "executing custom command 1"
break
} else if ($0 ~ "cmd2") {
print "executing custom command 2"
break
}else {
print "not understood"
break
}
} while (input > 0)
}


This is a sample run:
prompt>help
commands: help, cmd1, cmd2, exit
prompt>cmd1
executing custom command 1
prompt>blabla
not understood
prompt>exit
Exit!

It's nothing special, but it's a good start, if you want to use awk not from command line, but from within a more user-friendly shell.

No comments: