I am trying to argument processing on awk. It works fine if I have no body in it:
PK@rhel8:~/tmp-> cat testARG.awk
#!/usr/bin/awk -f
BEGIN{
argc = ARGC ;
CmdName = ARGV[0] ;
FirstArg = ARGV[1]
printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
}
#{
# printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
# print $0
#}
PK@rhel8:~/tmp-> ./testARG.awk 1 2 3 4
Argument count = 5; command name = awk; first argument = 1
PK@rhel8:~/tmp->
However when I uncomment the body it doesn't like it at all:
PK@rhel8:~/tmp-> cat testARG.awk
#!/usr/bin/awk -f
BEGIN{
argc = ARGC ;
CmdName = ARGV[0] ;
FirstArg = ARGV[1]
printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
}
{
printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
print $0
}
PK@rhel8:~/tmp-> ./testARG.awk 1 2 3 4
Argument count = 5; command name = awk; first argument = 1
awk: ./testARG.awk:6: fatal: cannot open file `1' for reading (No such file or directory)
PK@rhel8:~/tmp->
Is there some different way I have to use awk to allow it to see the arguments as arguments and not files?