"; exit} calc = 0;....} I am trying to print a u...">

Using FILENAME in awk script

#!/usr/bin/awk -f

{if(!FILENAME) {print "<Gasp.sh> <inputfile>"; exit} calc = 0;....}

I am trying to print a usage instruction in my awk script, so if the script is run without an input file, it shows usage. This is my attempt, but I believe that I am not using the variable FILENAMEcorrectly. I also tried putting the if statement in a block BEGINand END, but nothing worked.

+4
source share
2 answers
BEGIN{if (ARGC!=2) {print "<Gasp.sh> <inputfile>"; exit} {calc = 0;....}
+5
source

Use ARGCinstead;

BEGIN {
    if(ARGC==1) {
        print "<Gasp.sh> <inputfile>"
        exit
    } 
    calc = 0
    # ...
}

Edit: Unfortunately, @shaikisiegal deleted his answer, so I will add this quote from the GNU awk documentation section. The built-in variables are here:

_   , awk . , awk , FILENAME "-". FILENAME , (. ). BEGIN FILENAME "", .

Unix awk FILENAME "-", . , .

+4

Source: https://habr.com/ru/post/1693473/


All Articles