LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How are arguments passed to a bash script? (https://www.linuxquestions.org/questions/programming-9/how-are-arguments-passed-to-a-bash-script-718051/)

jiml8 04-09-2009 04:59 PM

How are arguments passed to a bash script?
 
I am using SNMP traps to pass information via snmp from a module on one system to a module on another system. Specifically, I am using SNMPv2 Notify messages, as described on this page:

http://www.net-snmp.org/wiki/index.p...ring_snmptrapd

Now, for reasons that are not at all clear to me, snmptrapd expects that the command that it invokes when it processes a trap will be a shell script.

I do not wish to use a shell script; I want to have snmptrapd invoke a dedicated C program that I will provide. This C program will accept the data that is being sent via the trap, and will forward that data via some IPC mechanism (I haven't decided yet...probably a TCP socket) to its ultimate destination.

I am correctly building the SNMP trap message in my C module, and sending it. The snmptrapd is properly processing it and invoking an appropriate command. When that command is the demo handler script as described on the net-snmp page (and reproduced here), everything works as expected and the output is correct (a text file in /tmp, for now).

When I replace that script with a dedicated C program, the result is not as desired; I am not picking up the trap information although the program is being invoked.

Specifically, here is the demo trap handler script I am using, from the net-snmp site:

Code:

#!/bin/sh
 echo "executing script" >/tmp/mytstfile
 read host
 read ip
 vars=
 
 while read oid val
 do
  if [ "$vars" = "" ]
  then
    vars="$oid = $val"
  else
    vars="$vars, $oid = $val"
  fi
  vars="$vars ==spacer=="
 done
 
 echo trap: $1 tag1 $host tag2 $ip tag3 $vars >>/tmp/mytstfile

And here is my C program, which I would like to have do the same thing (output the same data, though the format might be different):

Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int nn=0;
char *arg;
FILE *fp;
fp=fopen("/tmp/mytstfile","w");
fprintf(fp,"arg count is %s\n");
fclose(fp);
fp=fopen("/tmp/mytstfile","a+");
arg = argv[nn];
while(argv[nn] != NULL)
//while(nn < 5)
{
        fprintf(fp,"arg is %s\n",argv[nn]);
        nn++;
}
fclose(fp);
return 0;
}

Now, here is the text from /tmp/mytstfile after I invoke a trap with the shell script as the command:

Code:

executing script
trap: tag1 dadsbox.homegroup tag2 UDP: [192.168.0.2]:60925 tag3 DISMAN-EVENT-MIB::sysUpTimeInstance = 74695283 ==spacer==, SNMPv2-MIB::snmpTrapOID.0 = ExacqSCAN-MIB::exacqnotiftrap ==spacer==, ExacqSCAN-MIB::scanLastData.1 = "abc=def" ==spacer==

And here is the text from /tmp/mytstfile after I invoke a trap with the C program as the command:

Code:

arg count is
arg is /home/jiml/scan_snmp/testscript

Note in the C code that there is a commented out while statement, which should have the effect of wandering off into random memory if I invoke it when the actual argument count is 0 or one. Well, when I invoke it, I do get the command name listed as the first argument and the subsequent "arguments" are clearly environment variables and associated values.

So, the problem here is quite obviously that I do not know where to pick up the argument list that is passed to a shell script when it is invoked.

Could anyone here tell me where that argument list is kept? Perhaps a code fragment that will pick it up?

jiml8 04-09-2009 05:28 PM

I got it.

An fread to stdin picks everything up. Sometimes, though, you have to go through this kind of exercise (post all the details) before the answer becomes obvious.

In this case, it was a mention in a manpage about bash reading stdin that got me to recognize that I simply needed to read stdin.


All times are GMT -5. The time now is 05:13 AM.