Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-09-2009, 04:59 PM   #1
Senior Member
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
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:

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:

 echo "executing script" >/tmp/mytstfile
 read host
 read ip
 while read oid val
   if [ "$vars" = "" ]
     vars="$oid = $val"
     vars="$vars, $oid = $val"
   vars="$vars ==spacer=="
 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):

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

int main(int argc, char *argv[])
int nn=0;
char *arg;
FILE *fp;
fprintf(fp,"arg count is %s\n");
arg = argv[nn];
while(argv[nn] != NULL)
//while(nn < 5)
	fprintf(fp,"arg is %s\n",argv[nn]);
return 0;
Now, here is the text from /tmp/mytstfile after I invoke a trap with the shell script as the command:

executing script
trap: tag1 dadsbox.homegroup tag2 UDP: []: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:

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?
Old 04-09-2009, 05:28 PM   #2
Senior Member
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 115Reputation: 115
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script arguments R3N3G4D3 Programming 5 11-01-2007 10:16 AM
bash script - variable arguments indikabandara19 Linux - Newbie 4 05-21-2007 10:40 PM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 08:03 PM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 06:57 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:04 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration