5
#!/usr/bin/awk -f

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

I am trying to print out a usage statement in my awk script so if the script is run without an input file, it shows the usage. This is my attempt, but I assume I am not using the FILENAME variable correctly. I also tried putting the if statement in a BEGIN and END block but nothing has worked.

2 Answers 2

5
BEGIN{if (ARGC!=2) {print "<Gasp.sh> <inputfile>"; exit} {calc = 0;....}
Sign up to request clarification or add additional context in comments.

1 Comment

... and ARGC!=2 was exact.
5

Use ARGC instead;

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

Edit: Unfortunately @shaikisiegal deleted his answer so I will add this quote from GNU awk documentation section Built-in Variables here:

FILENAME This is the name of the file that awk is currently reading. When no data files are listed on the command line, awk reads from the standard input, and FILENAME is set to "-". FILENAME is changed each time a new file is read (see section Reading Input Files). Inside a BEGIN rule, the value of FILENAME is "", since there are no input files being processed yet.

Some early implementations of Unix awk initialized FILENAME to "-", even if there were data files to be processed. This behavior was incorrect, and should not be relied upon in your programs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.