Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, I have a shell script that reads the ip address of my machine and stores it in environment variable IP. when i give echo "$IP" in the terminal, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    9

    printing environment variables in c


    Hi all,

    I have a shell script that reads the ip address of my machine and stores it in environment variable IP. when i give

    echo "$IP"

    in the terminal, the ip address is printed. But when i try getting it through getenv in my c code, it prints null. I am able to get the already existing environment variable PATH using getenv but it doesn't work for the env variable IP. How do i get it working?

    Edit:I had said i was able to get the value of IP from terminal but i was wrong. I am not getting the value of IP using echo.
    This is my script

    #!/bin/sh
    IP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;
    echo "$IP" >> my_ip.txt

    the ip address is written in my_ip.txt file. but $echo $IP prints nothing.
    Last edited by janhar; 09-05-2012 at 10:44 AM.

  2. #2
    Just Joined! krokoziabla's Avatar
    Join Date
    Sep 2012
    Location
    Russia
    Posts
    20
    I've tried to launch you script and it's perfectly worked-I see my IP in my my_ip.txt.

    As far as I know if you declare a shell variable then only the shell knows about its existense. Try to use export keyword in your script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •