linux下獲取當前系統的環境變量

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

using namespace std;
extern char ** environ;


int main()
{
    char **var;
    for(var = environ;*var != NULL;var++)
        cout<<*var<<endl;

    char *home = getenv("HOME");//獲取當前系統的HOME變量,若系統中當前無該系統變量,返回NULL
    cout<<"HOME="<<home<<endl;

    return 0;
}

  運行結果:linux

TOOLKIT_HOME=/usr/local/toolkit
RANK_HOME=/usr/local/rank
EMAILMONITOR_HOME=/usr/local/emailmonitor
NOVA_HOME=/usr/local/include/nova
HOSTNAME=sjs_131_236
DISTRIBUTION_HOME=/usr/local/distribution
TERM=linux
SHELL=/bin/bash
HISTSIZE=1000
BOOST_DATE_TIME_LIB=boost_date_time-gcc41-mt-d-1_34
BOOST_THREAD_LIB=boost_thread-gcc41-mt-d-1_34
NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
SSH_CLIENT=10.129.148.245 35893 22
PROPHET_TEST_HOME=/usr/local/prophet_test
BRANDETL_HOME=/usr/local/brandetl
GALAXYGAME_HOME=/usr/local/galaxygame
FCGI_HOME=/usr/local/fcgi
SSH_TTY=/dev/pts/8
LC_ALL=zh_CN.gbk
PROPHET_FORECAST_XML=forecast_test.xml
USER=root
LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.4/client64/lib:
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:
CPATH=:
BOOST_HOME=/usr/local/include/boost-1_34
OCI_HEADERS_HOME=/usr/include/oracle/10.2.0.4/client64
WEBCAT_HOME=/usr/local/webcat
PROPHET_HOME=/usr/local/prophet
BRANDSTAT_HOME=/usr/local/brandstat
TNS_ADMIN=/usr/lib/oracle/10.2.0.4/network/admin
MAIL=/var/spool/mail/root
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/
INPUTRC=/etc/inputrc
PWD=/opt/liuss/test
PROPHET_NATUREFORCE_XML=nature_test.xml
LANG=zh_CN.gbk
PS1=[\u@\H \W]\$ 
BOOST_REGEX_LIB=boost_regex-gcc41-mt-d-1_34
SHLVL=1
HOME=/root
KYOTOCABINET_HOME=/usr/local/include
BRANDREPORT_HOME=/usr/local/brandreport
PROPHET_TELEPORTATION_XML=telep_test.xml
LOGNAME=root
CVS_RSH=ssh
SSH_CONNECTION=10.129.148.245 35893 10.16.131.236 22
CMW_HOME=/usr/local/include/cmw
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/:/usr/lib64/pkgconfig/
LESSOPEN=|/usr/bin/lesspipe.sh %s
PROPHET_SPROUT_XML=sprout_test.xml
BFC_CFG=adonline_test
OCI_LIBS_HOME=/usr/lib/oracle/10.2.0.4/client64/lib
ORACLE_HOME=/usr/lib/oracle/10.2.0.4
HISTTIMEFORMAT=%Y-%m-%d %H:%M:%S 
G_BROKEN_FILENAMES=1
BFC_HOME=/usr/local/include/bfc
_=./getenv
OLDPWD=/opt/liuss
//getenv結果
HOME=/root

 改變環境變量用setenv函數,ios

 

 

通常狀況下,能夠在系統中export variable=value,而後再c++程序中獲取該系統變量,若無則用一個默認值c++

相關文章
相關標籤/搜索