LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-17-2007, 01:05 AM   #1
navin_karnam
Member
 
Registered: May 2007
Posts: 52

Rep: Reputation: 15
How to access shell variables in C program


Hi all,

How can i access the Shell variable(PATH) in c program.

Actually i am running a command in C program using "system", which will update the shell variable "COUNT" continuously. I need to access the COUNT after every second in a c program.

Thanks and Regards,
Naveen.
 
Old 07-17-2007, 02:27 AM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
Hello. Use getenv to get a specific variable, eg
Code:
char *mypath=getenv("PATH")
or use the special variable environ,
Code:
extern char **environ
which contains all environment variables.

Edit
equally, to set a variable you would use setenv

Last edited by nc3b; 07-17-2007 at 03:31 AM.
 
Old 07-17-2007, 02:37 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by navin_karnam
Actually i am running a command in C program using "system", which will update the shell variable "COUNT" continuously.
If I understand correctly what you are doing, this isn't going to work.
The COUNT variable you are modifying with system is local to the system subshell, and unreachable from anything else.
 
Old 07-18-2007, 12:10 AM   #4
navin_karnam
Member
 
Registered: May 2007
Posts: 52

Original Poster
Rep: Reputation: 15
Thank you for the replies ..

I came to know how i can access the environment variables in c program..
But it is not solved my main problem still .. I will explain my problem clearly.


1) I have to display the progress of the command "tar zxf largesize.tgz"
2) To uncompress it is taking more than 10 min.
3) The above things i have to do in a c program and i have to display the progress of uncompressing.

4) To do the above I tried running the process in background using :
system ( " tar zxf largesize.tgz > temp & " );

But i dont know how i can check the progress of the background process.

Please help me out in solving this problem, or guide me is there any other good procedure which suits my requirement..

Thanks and Regards,
Naveen.
 
Old 07-18-2007, 12:40 AM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by navin_karnam
Thank you for the replies ..

I came to know how i can access the environment variables in c program..
But it is not solved my main problem still .. I will explain my problem clearly.


1) I have to display the progress of the command "tar zxf largesize.tgz"
2) To uncompress it is taking more than 10 min.
3) The above things i have to do in a c program and i have to display the progress of uncompressing.

4) To do the above I tried running the process in background using :
system ( " tar zxf largesize.tgz > temp & " );

But i dont know how i can check the progress of the background process.

Please help me out in solving this problem, or guide me is there any other good procedure which suits my requirement..

Use popen() instead of system(), and read the output of your command (perhaps use tar xvzf).

 
  


Reply



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
Shell script variables Fredde87 Linux - Newbie 13 11-01-2006 03:35 AM
Shell variables Milosevic Linux - Newbie 2 12-10-2005 10:32 PM
Sub shell environment variables tnine9 Programming 1 06-08-2004 01:27 PM
Setting Shell variables tmoorman Linux - Software 6 11-19-2003 03:41 PM
Shell Variables. ducka Programming 5 09-03-2003 05:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:39 PM.

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