Hey so I'm trying to get one of my c variables to be a bash variable. Is there a way that I can do this?

Recommended Answers

All 7 Replies

Hey so I'm trying to get one of my c variables to be a bash variable. Is there a way that I can do this?

Exactly what do mean by 'to be a bash variable'. Do you mean the variable is shared between the C program and the shell or do you mean passing values between the C program and the shell script...

Exactly what do mean by 'to be a bash variable'. Do you mean the variable is shared between the C program and the shell or do you mean passing values between the C program and the shell script...

I mean I want to take a c variable and be able to use it in a script

I mean I want to take a c variable and be able to use it in a script

The check out

NAME
setenv - change or add an environment variable

SYNOPSIS
#include <stdlib.h>

int setenv(const char *name, const char *value, int overwrite);

int unsetenv(const char *name);

SEE ALSO
clearenv(3), getenv(3), putenv(3), environ(7)

I'm assuming you want to pass values to the shell script..Are you talking about some shared memory scheme where the script has access to the C program's memory?

The check out

I'm assuming you want to pass values to the shell script..Are you talking about some shared memory scheme where the script has access to the C program's memory?

Let me see if I can clarify what I want.

I want to take a string of numbers (2009-0330) from c and basically have some script variable equal to the same numbers so I can then use these numbers in my script.

Does that answer your question?

Let me see if I can clarify what I want.

I want to take a string of numbers (2009-0330) from c and basically have some script variable equal to the same numbers so I can then use these numbers in my script.

Does that answer your question?

Not really. Some example would be helpful. If you want to pass parameters to the script, you may form the command line dynamically via sprintf() .

Not really. Some example would be helpful. If you want to pass parameters to the script, you may form the command line dynamically via sprintf() .

Sorry for the delay in response. Here is my code.

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

void main(int argc, char *argv[]){
	char * filename = argv[1];
	char t[sizeof(filename)+2] ;
	sprintf(t, "v=%s", filename);
	printf("%s\n",t);
	system(t);
	system("echo $v");

}

So far it creates the string correctly but it does not set the variable to anything.

Sorry for the delay in response. Here is my code.

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

void main(int argc, char *argv[]){
	char * filename = argv[1];
	char t[sizeof(filename)+2] ;
	sprintf(t, "v=%s", filename);
	printf("%s\n",t);
	system(t);
	system("echo $v");

}

So far it creates the string correctly

Careful. sizeof(filename) is 4. You are heading to a segfault.

but it does not set the variable to anything.

Of course. Two invocations of system invoke different shells. The first one sets the variable, and dies. All changes you made to the environment are gone. Try

sprintf(cmdline, "v=%s; echo $v", filename);
    system(cmdline);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.