亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: AIX 用程序使profile文件立即生效 [打印本頁(yè)]

作者: guojinshuai    時(shí)間: 2014-10-28 14:43
標(biāo)題: AIX 用程序使profile文件立即生效
  1. int modify_profile(char *str,char *value)
  2. {
  3.         assert(str != NULL && value != NULL);
  4.         char strcmd[1200] = {0};
  5.         int state =0;
  6.         sprintf(strcmd,"sed 's/^export %s=.*/export %s=%s%s/' /home/bspdev1/.profile > /home/bspdev1/.profile_tmp",str,str,value,";"); /****替換字符串**且每個(gè)字段的值有;作為結(jié)尾的**/
  7.         printf("%s\n",strcmd);
  8.         system("cp /home/bspdev1/.profile /home/bspdev1/.profile_bak"); /*back up*/
  9.         state = system(strcmd);
  10.         printf("WEXITSTATUS(state) = %d\n",WEXITSTATUS(state));
  11.         system("mv /home/bspdev1/.profile_tmp /home/bspdev1/.profile");
  12.        
  13.         return 0;
  14. }
復(fù)制代碼
  1. int main()
  2. {
  3.         int state = 0;
  4.         char str[100] = "DBPASS";
  5.         char value[1024] = "test888";
  6.         printf("Before modify,%s=%s\n",str,getenv(str));
  7.         modify_profile(str,value);
  8.         state  = system("cd /home/bspdev1 && . ~/.profile");/***修改.profile文件并使之立即生效,生效后用getenv()得到新的環(huán)境變量,但是目前不好用。。***/
  9.         printf("WEXITSTATUS(state) = %d\n",WEXITSTATUS(state));
  10.         printf("After modify,%s=%s\n",str,getenv(str));
  11. }
復(fù)制代碼
各位老手,我寫了個(gè)程序,功能是:修改并保存profile文件,且使它立即生效,代碼如上。
現(xiàn)在的問(wèn)題是:profile文件可以被修改,但是并沒(méi)有立即生效,程序中已經(jīng)執(zhí)行了system("cd /home/bspdev1 && sh .profile");為什么最后getenv()得到的環(huán)境變量還是修改之前的呢?我手動(dòng)敲命令cd /home/bspdev1 && . ~/.profile,之后echo $DBPASS,就能得到新的環(huán)境變量。很疑惑,為什么用程序的方法不行呢?


作者: guojinshuai    時(shí)間: 2014-10-28 16:29
樓主找了好多帖子,也問(wèn)了很多人,終于懂了。
環(huán)境變量是進(jìn)程私有的。。。。感覺(jué)這句話就能解決好多疑問(wèn)了!




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2