Passing --usage as argument to awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing --usage as argument to awk script
# 1  
Old 11-09-2011
Passing --usage as argument to awk script

I have the awk script below and things go wrong when I do
Code:
awk -v dsrmx=25 -f ./checkSRDry.awk --usage

I basically want to override the usual --usage and --help that awk gives.

How do people usually handle this situation when you also want to supply your own usage and help
concerning the script one has written?

Code:
################################################################################
#
#  --Module---------------------------------------------------------------------
#
#                            checkSRDry.awk
#
#    Prints warnings when the source-receiver distances exceed dsrmx.
#
#  --Usage----------------------------------------------------------------------
#
#    awk [optionalArgs] -f ./checkSRDry.awk file > file_error_log
#
#  --Optional Arguments---------------------------------------------------------
#
#    -v dsrmx=dist
#      Source-receiver distance to check.
#
#    file
#      Input ray path file.
#
#    file_error_log
#      Output file containing warnings.
#
#    -h, --help, -u, --usage, -e, --examples
#      Prints a usage message to STDOUT and exits.
#
#  --Operations-----------------------------------------------------------------
#
#    usage()
#    help(Version)
#
#  --Examples-------------------------------------------------------------------
#
#    awk -v dsrmx=65 -f ./checkSRDry.awk file > file_error_log
#    awk -f ./checkSRDry.awk -h
#
#  --History--------------------------------------------------------------------
#
#    V01 - DEC 2009 - Christopher Dimech
#          Initial release.
#    V02 - JAN 2010 - Christopher Dimech
#          Included command line arguments.
#
################################################################################

#  abs: Returns the absolute value of val

function abs(val) {

  return val > 0 ? val  \
                 : -val

}

################################################################################

#  usage: Description of this awk script

function usage() {

  print ""
  print "--Usage---------------------------------------------------------------"
  print ""
  print "  awk [optionalArgs ] ./checkSRDry.awk file > file_error_log"
  print "  awk -f ./checkSRDry.awk -h"
  print ""
  print "--Optional Arguments--------------------------------------------------"
  print ""
  print "  -v dsrmx=dist     Source to receiver distance to check."
  print "  fin.ry            Input ray path file."
  print "  fout.chk          Output file containing warnings."
  print "  -h, --help, -u, --usage, -e, --examples"
  print "                    Prints a usage message."
  print ""
  print "----------------------------------------------------------------------"
  print ""

}

################################################################################

#  examples:

function examples() {

  print ""
  print "--Examples----------------------------------------------------------"
  print ""
  print "  awk -f ./checkSRDry.awk file > file_error_log"
  print "  awk -f ./checkSRDry.awk -h"
  print ""
  print "--------------------------------------------------------------------"
  print ""
}

################################################################################

#  help: Description of this awk script

function help() {

  print ""
  print "--Module--------------------------------------------------------------"
  print ""
  print "                            checkRays.awk"
  print ""
  print "  Prints warning when source-receiver distances exceed dsrmx"
  print ""
  print "--Usage---------------------------------------------------------------"
  print ""
  print "  awk [optionalArgs] -f ./checkSRDry.awk file > file_error_log"
  print ""
  print "--Optional Arguments--------------------------------------------------"
  print ""
  print "  -v dsrmx=value"
  print "    Source to receiver distance to check"
  print ""
  print "  file"
  print "    Input ray path file."
  print ""
  print "  file_error_log"
  print "    Output file containing warnings."
  print ""
  print "  -h, --help, -u, --usage, -e, --examples"
  print "    Prints a usage message."
  print ""
  print "--Examples-------------------------------------------------------------"
  print ""
  print "  awk -v dsrmx=65 -f ./checkSRDry.awk file > file_error_log"
  print ""
  print "----------------------------------------------------------------------"
  print ""

}

################################################################################

BEGIN {

  c = ARGV[ARGC-1]
  print c

  if ((c == "-u") || (c == "--usage")) {
    usage()
    exit 1
  }

  if ((c == "-e") || (c == "--examples")) {
    examples()
    exit 1
  }

  if ((c == "-h") || (c == "--help")) {
    help()
    exit 1
  }

  if ( !dsrmx ) {
    usage()
    print ""
    print "--Error-------------------------------------------------------------"
    print ""
    print "  Source to receiver distance not set.                     Mandatory"
    print "  Set source to receiver distance using -v dsrmx=dist"
    print ""
    print "--------------------------------------------------------------------"
    print ""
    exit 1
  }

  RS = ">"                                             # Change record separator

  ARGV[ARGC++] = ARGV[ARGC-1]                                  # Read file twice

}

# Error header

NR == 1 {
  print ""
  print "--Warnings------------------------------------------------------------"
  print ""
  print "  Source to receiver distance exceeding",dsrmx,"Mm"
  print ""
  print "----------------------------------------------------------------------"
  print ""
}

# --Read file: First time round-------------------------------------------------

FNR == NR {                                              # Set number of records
  ++i
  next
}

# --Read file: Second time round------------------------------------------------

NF > 2 {
  srdist = abs( $1 - $(NF-2) )
  if (srdist > dsrmx) {                                            # Print error
    print "WARNING: FNR="FNR", dist("$1","$(NF-2)")="srdist
  }
}

END {
#  if (c !~ /(-u|--usage|-e|--examples|-h|--help)/) {
#  print c
#  print ""
#  print "--End of checkTrvtdat.awk---------------------------------------------"
#  print ""
#  }
}

################################################################################

# 2  
Old 11-09-2011
I think you can't used awk with --usage directly, because awk doesn't accept it.
you have to create another script , such as call it kawk.sh (kristinu awk), with it, you can adjust the option (man getopts)

Code:
kawk.sh -v dsrmx=25 -f ./checkSRDry.awk --usage

# 3  
Old 11-09-2011
I will just give them another name such as --hlp and --usg then. Don't want to end up with lot of scripts unnecessarily.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable as an argument to another script

Hi, I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem. varaiable "a" value should be -b "FPT MAIN". When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting... (9 Replies)
Discussion started by: Manasa Pradeep
9 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

passing argument in script?

hi, I want to implement some function to perform following task if ; then $TEXT = "Data_0" else $TEXT = $1 fi if ; then $Lines = 45 else $Lines = $2 fi Kindly suggest, thanks (11 Replies)
Discussion started by: nrjrasaxena
11 Replies

4. Shell Programming and Scripting

Passing value as a command line argument in awk script.

I have one working awk command line. Which taking data from the “J1202523.TXT” file and generating the “brazil.dat” file. PFB code. awk '{ DUNS = substr($0,0,9);if ( substr($0,14,3) == "089" ) print DUNS }' J1202523.TXT > Brazil.dat But now I want to pass two parameter as a command line argument... (4 Replies)
Discussion started by: humaemo
4 Replies

5. Shell Programming and Scripting

Problem passing directory as argument with awk

I'm trying to figure out what's getting passed as the argument when I try to pass a directory as an argument, and I'm getting incredibly strange behavior. For example, from the command line I'm typing: nawk -f ./test.awk ~ test.awk contains the following: { directory = $NF print... (13 Replies)
Discussion started by: mrplainswalker
13 Replies

6. Shell Programming and Scripting

Passing argument to system call in awk script

So, I have this script. It reads a CSV file that has a mixture of object names with IP addresses (parsing out that part I have working), and object names which have a DNS name. I want to be able to run a "dig +short" based off of the name given to me in the line of the awk script, and then deal... (6 Replies)
Discussion started by: mikesimone
6 Replies

7. Shell Programming and Scripting

passing argument from Cshelll to awk command

Hi all I have got a file digits.data containing the following data 1 3 4 2 4 9 7 3 1 7 3 10 I am writing a script that will pass an argument from C-shell to nawk command. But it seems the values in the nawk comman does not get set. the program does not print no values out. Here is the... (1 Reply)
Discussion started by: ganiel24
1 Replies

8. Shell Programming and Scripting

passing argument into awk

i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect. ie xyx 3 (prints the 3rd line, separated by ':' of filename) function xyz() { arg1=$1 cat filename | awk -F: -v x=$arg1 '{print $x}' } any ideas? (4 Replies)
Discussion started by: prkfriryce
4 Replies

9. Shell Programming and Scripting

Passing argument from one script to other

Dear All, I have one script which accepts database name and user_id from the user, i have another script that will unload the data from all the tables based on the user_id accepted by the user. How can i pass the user_id from the 1st script to the other. My OS is sun solaris. Thanks in... (3 Replies)
Discussion started by: lloydnwo
3 Replies

10. UNIX for Dummies Questions & Answers

Passing argument to awk script

I am writing a shell script. Now i need to read in a string and send it to an awk file to compare and search for compatible record. I wrote it like tat: read serial | awk -f generate.awk data.dat p/s: the data file got 6 field. According to an expert, we can write it like tat: read... (1 Reply)
Discussion started by: AkumaTay
1 Replies
Login or Register to Ask a Question