Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass arguments 1

Status
Not open for further replies.

daula

Programmer
May 29, 2006
38
US
hi everyone;
i have a script that is supposed to take an inputfile arg and an optional outputfile.
what i'd like to happen is:
1. quit if no arg passed
2. please, see the psudo code;
Code:
if (arg 1 exists and is a valid file)
{     
   if(arg 2 exist)
      then process inputfile and save result to outputfile.
   otherwise
      process inputfile and spit the result onto the screen. 
}
otherwise 
{
   quit
}

i'm seeking some help with number 2 above.

thanks in advance

daula
 
Hi

Like this ? But would be better to tell us more about what you want exactly.
Code:
awk -v myarg=myfilename '
BEGIN{
  if (myarg=="") exit;
  if ((getline < myarg)==-1) exit;
  close(myarg)
}
' /input/file

Feherke.
 
actually my script is for transforming an xml file and as you can see on the usage, the script is supposed to take an xml file and an optional output file.
currently, both args are reguired coz i'm feeding 'em at the end of the awk block as you may notice on the code bellow.

i would like the capability to be able to either run the script in two different ways:
(1) pass two args thus, saving result to output file
./tally.sh tally.xml output_file
(2) pass one arg... display result to screen
./tally.sh tally.xml

so, is it possible to incorporate my bash portion of the script to achieve the above. or, is there an awk way to do this??

Thanks in advance.

daula
Code:
##! /bin/bash

### Script Name: tally.sh
### Usage:     ./tally.sh tally.xml [output_file]

TALLY="$1"
OUT_FILE=""
E_BADARGS=65

if [[ -z "$TALLY" ]]; then   # blank
   echo "Usage: ./`basename $0` tally.xml [output_file]"
   exit $E_BADARGS
else
  if [[ -e "$TALLY" ]]; then # tally.xml file exist
     if [[ -z "$2" ]]; then
        echo "Usage: ./`basename $0` $TALLY"
     else
        OUT_FILE="$2"
        echo "Usage: ./`basename $0` $TALLY $OUT_FILE"
     fi
  fi
fi

awk '
BEGIN{FS=" *<[^>]+>";f="%s,%d,%04.2f,%d,%04.2f,%d,%04.2f,%d,%04.2f,%d,%04.2f\n"}
/:name="cSCF">/{++r;next}
/>00-06/{c=2;next}
/>07-12/{c=4;next}
/>13-24/{c=6;next}
/>25-36/{c=8;next}
/>37-48/{c=10;next}
/:value>/{a[r,1]=$2;c=0;next}
/:count>/{a[r,c]=$2;t[c]+=$2}
END{
  t[4]+=t[2];t[6]+=t[4];t[8]+=t[6];t[10]+=t[8]
  for(i=1;i<=r;++i){
    a[i,4]+=a[i,2];a[i,6]+=a[i,4];a[i,8]+=a[i,6];a[i,10]+=a[i,8]
    printf f,a[i,1],0+a[i,2],100*a[i,2]/t[2],a[i,4],100*a[i,4]/t[4],a[i,6],100*a[i,6]/t[6],a[i,8],100*a[i,8]/t[8],a[i,10],100*a[i,10]/t[10]
  }
  printf "%d,%d,%d,%d,%d\n",t[2],t[4],t[6],t[8],t[10]
}
' "$TALLY" > "$OUT_FILE"
 
Something like this perhaps?

Code:
if [[ -z "$TALLY" ]]; then   # blank
   echo "Usage: ./`basename $0` tally.xml [output_file]"
   exit $E_BADARGS
else
  if [[ -e "$TALLY" ]]; then # tally.xml file exist
     if [[ -z "$2" ]]; then
        OUT_FILE=/dev/stdout
     else
        OUT_FILE="$2"
     fi
  fi
fi


Annihilannic.
 
You could also use this more concise syntax:

Code:
if [[ -z "$TALLY" ]]; then   # blank
   echo "Usage: ./`basename $0` tally.xml [output_file]"
   exit $E_BADARGS
else
  if [[ -e "$TALLY" ]]; then # tally.xml file exist
     OUT_FILE="${2:-/dev/stdout}"
  fi
fi

It sets OUT_FILE to "/dev/stdout" if the value of $2 is undefined.

Annihilannic.
 
that's exactly what i neeeded.
thanks Annihilannic

daula
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top