Results 1 to 10 of 13
how to obtain shell variable value in C program?
For Example:
#cat test.sh
read re
echo $re
I want to get above shell variable's value in a C program...
main(){
...
- 12-02-2008 #1
How to get shell variable in C program?
how to obtain shell variable value in C program?
For Example:
I want to get above shell variable's value in a C program...#cat test.sh
read re
echo $re
main(){
..........
..........
//printf("%s",mesg); //get the value from shell variable.
}- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 12-02-2008 #2Just Joined!
- Join Date
- Feb 2008
- Posts
- 50
I think you can access this variable using "getenv". But for this you must have to export this variable so other shell can also access this variable.
VarValue=getenv(re);
printf("VarValue=%s\n",VarValue);
- 12-03-2008 #3
I checked this method last night ....but its not working...
shell variable is exported ....re=1
echo $rere
export rere
I re-checked it using env command...(I can see re=1)
but when i to use it in C program...something like
main(char argc,char *argv[],char ** environ)
{
char re[25];
::::
printf("%s",getenv(re));
}
It displays :
(null) value
From getenv i found ,if environment value is not set it will display null.....
but i can see the value using env
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 12-03-2008 #4
You're not doing it quite right. Run this shell script as a corrected example.
Hope this helps.Code:#!/bin/bash re=1 echo $re export re cat > wednesday.c <<EOD; cc -Wall wednesday.c -o wednesday; ./wednesday #include <stdio.h> #include <stdlib.h> int main(void) { printf("%s\n",getenv("re")); return 0; } /* main() */ EOD--
Bill
Old age and treachery will overcome youth and skill.
- 12-11-2008 #5Sorry for delay reply,no ...it's not working for me..#!/bin/bash
re=1
echo $re
export re
cat > wednesday.c <<EOD; cc -Wall wednesday.c -o wednesday; ./wednesday
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%s\n",getenv("re"));
return 0;
} /* main() */
EOD
This is what i tried -around way method.
I store these values in a file and then use C program to read this value..
test.sh
echo $re >> /tmp/config_file
test.c - C program
Note both C and Shell are independent programs...When i tried to use getenv()
main (){
int choice;
char a;
--
--
fp=open (/tmp/config_file,0) //open temp file
read(fp,a,1); //read value
close(fp)
if(a == '1')
choice=1;
else
choice=0;
::::::
::::::
}
is still returns value as
Any idea.....(null)- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 12-11-2008 #6Now I'm really confused. Please be patient with me, and let's take this one step at a time. So, when I run this bash script:no ...it's not working for me..
I get this output:Code:#!/bin/bash re=1 echo $re export re cat > wednesday.c <<EOD; cc -Wall wednesday.c -o wednesday; ./wednesday #include <stdio.h> #include <stdlib.h> int main(void) { printf("%s\n",getenv("re")); return 0; } /* main() */ EOD
What output do you get?Code:1 1
--
Bill
Old age and treachery will overcome youth and skill.
- 12-11-2008 #7Just Joined!
- Join Date
- Feb 2008
- Posts
- 50
i think you are missing allocating space for buffer "a"
see this :
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int choice;
char *a;
int main()
{
int fp, size;
a= malloc(10);
fp=open("/tmp/config", O_RDONLY);
printf("open returns: %d\n",fp);
size = read(fp, a, 1);
printf("read returns: %d\n",size);
close(fp);
printf("value of a: %s\n",a);
return 0;
}
$ ./readf.o
open returns: 3
read returns: 1
value of a: 1
- 12-11-2008 #8
I got an error message on cc -wall
Thanks wje_lf and ypankaj...
Please be patient with me too.
I'll explaning what i'm trying : I'm implemeting a GUI interface for my C project.
Since it's nearly 4k lines of c program. I used a tool called zenity.
Zenity provides gui interface to shell script.
My c program can be involved options like
In C program , If the variable choice = 1 then do this else do that.option 1:
option 2:
Enter your choice :
As i'm using zenity,I can easily create a gui with these two options in script program.
It will store the value chosen/clicked by user.ans=$(zenity --list --text "Chose your option?" --radiolist --column "Pick" --column "Opinion" TRUE Option 1 FALSE Option 2);
echo $ans
For more about zenity check here A complete zenity dialog examples 2 Linux by Examples
If i can some how export this shell vairable $ans value to C program.
In simple terms,I want to assign value of shell variable ans to C program variable choice
I hope Its clear now...- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 12-11-2008 #9The first part of that is to get access to the shell variable. My little sample shell script shows how. But that didn't work for you, because:In simple terms,I want to assign value of shell variable ans to C program variable choice
Yes, because -wall is meaningless, but -Wall means something. Use upper case W, not lower case w. Rather than typing the script by hand, maybe can you cut and paste it into a terminal window or something, and try again? :)I got an error message on cc -wall
If it then doesn't work for you, let's find out why. If it does work for you, perhaps it will illustrate how a C program can access an environment variable. (A bash variable becomes an environment variable when one exports it.)
But ypankaj is correct when he says you have a problem with the C variable a. You declare it as a single char; he, in his revision of your program, declares it as a pointer to (zero or more) chars.
That pointer doesn't yet point to any actual buffer space; he fixes that by calling malloc(). Please do this at the command prompt:
He makes two errors, though:Code:man malloc
- Whenever you call malloc(), you should make sure that the resultant pointer is not NULL before continuing. If it's NULL, you should exit with an error message. In this case, that error shouldn't happen, but you should always check for it.
- We have no way of knowing that there will be only 9 characters in the string. (The 10th one will be the NUL character which indicates the end of the string.) So instead of doing malloc(), you should be doing strdup(), which will automatically allocate sufficient memory to hold the string. (But don't for get to check that for a returned NULL pointer, either.)
For more information, please do this at the command line:
Code:man strdup
Please do not be offended by the following observation. If you declared a to be a single character, perhaps some additional reading on how to program in C could save you a lot of debugging time in the long run. I'd recommend that you scroogle the following search terms:
and find the tutorial that suits you best.Code:C tutorial
--
Bill
Old age and treachery will overcome youth and skill.
- 12-11-2008 #10I missed & in above stmt .... it should be like this:read(fp,a,1); //read value
That's what i learned from my teacher....read(fp,&a,1); //read value
I need to re-fresh me C skills again instead of working on things like (php,drupal,j2ee
)
...thanks for the links i'll look into them....
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------


Reply With Quote
