including shell env variables into C code


 
Thread Tools Search this Thread
Top Forums Programming including shell env variables into C code
# 1  
Old 01-26-2007
including shell env variables into C code

Hey, I'm brushing up on C code and I'm trying to incorporate the shell environmental variables into standard ansi C code.

For example I just want to do the equivalent of
--echo $PATH-- and implement that in standard ansi C code.

How would I do that?


Thanks
# 2  
Old 01-26-2007
Quote:
Originally Posted by bdsffl
Hey, I'm brushing up on C code and I'm trying to incorporate the shell environmental variables into standard ansi C code.

For example I just want to do the equivalent of
--echo $PATH-- and implement that in standard ansi C code.

How would I do that?

Thanks
Here it is..:
Code:
/* ShellCommsInC.c: sample program to see how shell commands can be incorporated
   in standard C ANSI code
*/

# include <stdio.h>

int main ( void )
{
     printf("\n* * * * * * * * * * * * * * * *\n") ;
     printf("This is a test for incorporating shell comm's into ANSI C code\n") ;

     system("echo") ;
     system("echo '$PATH' is: $PATH") ;
     system("echo") ;
     system("echo '$HOME' is: $HOME") ;
     system("echo") ;
     system("echo '$USER' is: $USER") ;

     printf("\nAnd so on..\n") ;
     printf("\n* * * * * * * * * * * * * * * *\n") ;

return 0 ;
}

# 3  
Old 01-26-2007
I would just do:
puts(getenv("PATH"));
# 4  
Old 01-26-2007
All of the env variables:
Code:
#include <stdio.h>
int main(int argc, char **argv, char **envp)
{
	while(*envp)
	{
		printf("%s\n",*envp++);
	}
	return 0;
}

# 5  
Old 01-26-2007
Thanks guys! This site is great!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Set env variables for user

Hi , I have installed oracle in Solaris machine and unable to set the env variable. I tried to put the env variable in .dtprofile file but didn't help. So everytime I login in need to run the command and export the variable. Kindly suggest where I am doing wrong.Pls excuse as I am not too... (2 Replies)
Discussion started by: Rossdba
2 Replies

2. Shell Programming and Scripting

Shell Script for Setting Env Variables

Hello All. Good Afternoon. I need one small help regarding setting of env variables for a particular host by getting it from the DB. For ex : 1. I am using LOCALHOST. 2. When I run a ./hostset.sh it should pick up the Oracle home details from associated DB and set it. Please... (1 Reply)
Discussion started by: PavanPatil
1 Replies

3. Shell Programming and Scripting

Env variables in script

Hi All, I have script and it's hardcoded the script ca invoke in user home dir and logs will be redirected to home dir of user. how to make the same script will be invoke from /usr/bin with out chg the logs and other functions path from /user/homedir . code is below: pls check how to... (1 Reply)
Discussion started by: saku
1 Replies

4. Red Hat

how to use env variables within ed

i have a file that i need to edit and replace a single value with another. so i have two variables, $oldvalue and $newvalue but below doesn't work: ed file.txt << EOF ,s/$oldversion/$newversion/g wq EOFi presume it's the $ that is the issue since it's actually special to ed. any suggestions?... (1 Reply)
Discussion started by: crimso
1 Replies

5. Solaris

env variables again

What is the difference between ${variable} and $variable when used in a script? (2 Replies)
Discussion started by: orange47
2 Replies

6. UNIX for Dummies Questions & Answers

How to update env variables.

I am newbie on Unix system and seek help for updating env variables. The condition is like this: On Unix server, I log in as oracle user (this is the super for database on Unix), I type > env all envirnment variables show up. I saw one variable DBA_LIST contains a few email addreses. I need... (2 Replies)
Discussion started by: duke0001
2 Replies

7. UNIX for Advanced & Expert Users

How to export ENV variables, which remains set for all the shell

Hi ! How to export ENV variables, which remains set for all the shell Example :- Login :myID Pwd : **** -> Here my ID .profile is executed. Let say I set MYENV variable Kisses% rlogin ABC -l XXXGroupID -> I login into a remote Solaris Server ABC password : **** -> "XXXGroupID's... (1 Reply)
Discussion started by: dashok.83
1 Replies

8. Shell Programming and Scripting

Getting the value of env variables

Hi, I want to get the value of the env varables using the ksh script. All the env variables are stored in a file. Eg. file1 $INPATH $OUTPATH myscirpt: for name in `awk { print $1 } file1` do cd $name done i'm getting the error like $INPATH not found. in the same script... (1 Reply)
Discussion started by: vij_krr
1 Replies

9. UNIX for Advanced & Expert Users

Ksh - Env. Variables ??

Hey all, I have been using Ksh and in that I am setting Environment variables. To set Env. Variables I have created my own file "BuildScript.sh" in which i have written : export CLASSPATH=/somedir/some other dir/file:. export PATH=/some dir/file:. But when i am calling this... (4 Replies)
Discussion started by: varungupta
4 Replies

10. Shell Programming and Scripting

export env variables

hi i want to write a shell script to set environment variables . But i am not been able to set that for the current shell instead i have to spawn a new shell. Is there a way to set the env variable for the current shell using shell script in bash shell ? Thnx (2 Replies)
Discussion started by: varun.81
2 Replies
Login or Register to Ask a Question