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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ARGC question 1

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All,

Can I use a for loop to read the ARGC input parameters instead of me using ksh to do this for e.g.

Chris.sh NE DT 20061105 20061106 and must read
BEGIN{for (i=1;i<ARGC;i++){
if(ARGV != 4){
exit 1
}
else
{
$1=NE ,$2=DT etc..
}


Many Thanks
Chris
 
You can, but it's not very clear what you're trying to do, i.e. what is the context of the problem?

You can use ARGC and ARGV to process the parameters provided to the awk script, but I get the impression you want to use it to parse the parameters of the calling shell script; is that right?

Or are you considering replacing your ksh script entirely with awk? You can do that by changing the shebang line to:

[tt]#!/usr/bin/awk -f[/tt]

Annihilannic.
 
Annihilannic,

Thanks for your response.

I would like my script to be fully awk (Chris) for e.g.

Chris NE DT 20061105 20061106 and must read

BEGIN{for (i=1;i<ARGC;i++){
if(ARGV != 4){
exit 1
}
else
{
if($1!=NE){
print something
exit 2
}
if($2!=DT){
print something
exit 3
}
etc
END{
}

I hope this helps

Many Thanks
Chris
 
I think you mean something like this:

Code:
#!/usr/bin/awk -f
BEGIN{
        # 5 because script name is ARGV[0]
        if(ARGC != 5){
                print "four parameters required"
                exit 1
        }
        if(ARGV[1] != "NE"){
                print "first parameter is not NE"
                exit 2
        }
        if(ARGV[2] != "DT"){
                print "second parameter is not DT"
                exit 3
        }
        # insert rest of code here...
}

Annihilannic.
 
Hi Annihilannic

Thanks. That should give me a start.

Thanks Again
Chris
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top