computing
  • 0

awk command

  • 0

Hi

Can anyone help is there a command in Unix in side the awk function which i can check if the file exist or a dierctory does exist?.

regards,
Raim

Share

1 Answer

  1. awk uses the system function to execute any OS command, and the return code can be captured and checked, as shown below. You can also read in all the output of a system call (very handy).

    When I check the code returned from the OS, zero means success. But within awk, a zero expression means false and non-zero means true. If you run the code below, you will see that “one means true”.

    awk ‘BEGIN {

    rc=system(“test -f myfile”)
    if (rc==0)
       print “exists”
    else
       print “does NOT exist”

    if (system(“test -f myfile”))
       print “does NOT exist”
    else
       print “exists”

    if (0)
       print “zero means true”

    if (1)
       print “one means true”

    exit}’

    • 0