LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
123
返回列表 发新帖
楼主: herberteuler

环境变量不作为位置变量吗?

[复制链接]
 楼主| 发表于 2005-5-4 00:05:20 | 显示全部楼层
Post by yongjian
那就不行了, 我以为环境变量已经在用户环境中配置好了呢。如果你在程序中source一遍的话,export的值会被覆盖。我看只有做成interactive的程序了,就是提问用户。不过就烦人很多了。

一个可能的方法是,可以生成另一个文件,先这样:
  1. grep '^[^#]' $file
复制代码

找到所有的变量,然后对每个上面的结果这样:

  1. VARNAME=`echo $i | sed 's/^[         ]*//
  2.     s/[         ]*=.*//'`
  3. echo "if test -z $VARNAME ; then
  4.     $i" >>file2
复制代码

这里 $i 是上面输出的结果里的一项。最后,
  1. source file2
复制代码

就行了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-4 00:18:21 | 显示全部楼层
这个算是比较完备的了:
  1. # int sourcefile(file f)
  2. sourcefile()
  3. {
  4.         local tmpfile='/tmp/dipmtcfg'
  5.         local varname

  6.         rm -f $tmpfile
  7.         for i in `grep '^[^#]' $1 | sed 's/[         ]//g'` ; do
  8.                 varname=`echo $i | sed 's/=.*//'`
  9.                 echo "if test -z $varname ; then
  10.                         $i
  11.                 fi" >>$tmpfile
  12.         done
  13.         source $tmpfile
  14. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-5-4 00:32:39 | 显示全部楼层
Post by herberteuler
这个算是比较完备的了:
  1. # int sourcefile(file f)
  2. sourcefile()
  3. {
  4.         local tmpfile='/tmp/dipmtcfg'
  5.         local varname

  6.         rm -f $tmpfile
  7.         for i in `grep '^[^#]' $1 | sed 's/[         ]//g'` ; do
  8.                 varname=`echo $i | sed 's/=.*//'`
  9.                 echo "if test -z $varname ; then
  10.                         $i
  11.                 fi" >>$tmpfile
  12.         done
  13.         source $tmpfile
  14. }
复制代码

[PHP]$cat file
a=123
echo $a
source tmpfile
echo $a
$cat tmpfile
a=abc
$./file
123
abc
$[/PHP]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-4 00:54:35 | 显示全部楼层
刚才修改了一下,然后用 Gentoo 的配置文件试验了一把,成功了
  1. $ ls
  2. cfgpri
  3. $ cat cfgpri
  4. #!/bin/sh

  5. # int sourcefile(file f)
  6. sourcefile()
  7. {
  8.         local tmpfile='/tmp/dipmtcfg'
  9.         local varname
  10.         local line

  11.         rm -f $tmpfile
  12.         for i in `grep -n '^[^#]' $1 | sed 's/:.*//'` ; do
  13.                 varname=`sed -n $i','${i}p $1 | sed 's/^[       ]*//
  14.                         s/[     ]*=.*//'`
  15.                 line=`sed -n $i','${i}p $1 | sed 's/^[  ]*//
  16.                         s/[     ]*=[    ]*/=/'`
  17.                 echo "if test -z $varname ; then $line ; done" >>$tmpfile
  18.         done
  19.         cat $tmpfile
  20. }

  21. # int sourcecfg(file cfg1, file cfg2, ...)
  22. sourcecfg()
  23. {
  24.         :
  25. }

  26. sourcefile $1
  27. $ cat ~/tmp/make.globals
  28. # Copyright 1999-2004 Gentoo Foundation
  29. # Distributed under the terms of the GNU General Public License v2
  30. # $Header: /var/cvsroot/gentoo-src/portage/cnf/make.globals,v 1.56.2.3 2005/04/17 09:01:55 jstubbs Exp $
  31. # System-wide defaults for the Portage system

  32. #            *****************************
  33. #            **  DO NOT EDIT THIS FILE  **
  34. # ***************************************************
  35. # **** CHANGES TO make.conf *OVERRIDE* THIS FILE ****
  36. # ***************************************************
  37. # ** Incremental Variables Accumulate Across Files **
  38. # **  USE, CONFIG_*, and FEATURES are incremental  **
  39. # ***************************************************


  40. # Default rsync mirror
  41. SYNC="rsync://rsync.gentoo.org/gentoo-portage"

  42. # Default distfiles mirrors
  43. GENTOO_MIRRORS="http://distfiles.gentoo.org http://distro.ibiblio.org/pub/Linux/distributions/gentoo"

  44. # Repository Paths
  45. PORTDIR=/usr/portage
  46. DISTDIR=/usr/portage/distfiles
  47. PKGDIR=/usr/portage/packages
  48. RPMDIR=/usr/portage/rpm

  49. # Temporary build directory
  50. PORTAGE_TMPDIR=/var/tmp

  51. # Fetching command (5 tries, passive ftp for firewall compatibility)
  52. FETCHCOMMAND="/usr/bin/wget -t 5 --passive-ftp -P \${DISTDIR} \${URI}"
  53. RESUMECOMMAND="/usr/bin/wget -c -t 5 --passive-ftp -P \${DISTDIR} \${URI}"

  54. # Default user options
  55. FEATURES="sandbox distlocks"

  56. # Default chunksize for binhost comms
  57. PORTAGE_BINHOST_CHUNKSIZE="3000"

  58. # By default wait 5 secs before cleaning a package
  59. CLEAN_DELAY="5"

  60. # By default wait 10 secs on an important warning
  61. EMERGE_WARNING_DELAY="10"

  62. # Automatically clean installed packages after they are updated.
  63. # This option will be removed and forced to yes.
  64. AUTOCLEAN="yes"

  65. # Number of times 'emerge --sync' will run before giving up.
  66. RSYNC_RETRIES="3"

  67. # Number of seconds rsync will wait before timing out.
  68. RSYNC_TIMEOUT="180"


  69. #            *****************************
  70. #            **  DO NOT EDIT THIS FILE  **
  71. # ***************************************************
  72. # **** CHANGES TO make.conf *OVERRIDE* THIS FILE ****
  73. # ***************************************************
  74. # ** Incremental Variables Accumulate Across Files **
  75. # **  USE, CONFIG_*, and FEATURES are incremental  **
  76. # ***************************************************
  77. $ ./cfgpri ~/tmp/make.globals
  78. if test -z SYNC ; then SYNC="rsync://rsync.gentoo.org/gentoo-portage" ; done
  79. if test -z GENTOO_MIRRORS ; then GENTOO_MIRRORS="http://distfiles.gentoo.org http://distro.ibiblio.org/pub/Linux/distributions/gentoo" ; done
  80. if test -z PORTDIR ; then PORTDIR=/usr/portage ; done
  81. if test -z DISTDIR ; then DISTDIR=/usr/portage/distfiles ; done
  82. if test -z PKGDIR ; then PKGDIR=/usr/portage/packages ; done
  83. if test -z RPMDIR ; then RPMDIR=/usr/portage/rpm ; done
  84. if test -z PORTAGE_TMPDIR ; then PORTAGE_TMPDIR=/var/tmp ; done
  85. if test -z FETCHCOMMAND ; then FETCHCOMMAND="/usr/bin/wget -t 5 --passive-ftp -P \${DISTDIR} \${URI}" ; done
  86. if test -z RESUMECOMMAND ; then RESUMECOMMAND="/usr/bin/wget -c -t 5 --passive-ftp -P \${DISTDIR} \${URI}" ; done
  87. if test -z FEATURES ; then FEATURES="sandbox distlocks" ; done
  88. if test -z PORTAGE_BINHOST_CHUNKSIZE ; then PORTAGE_BINHOST_CHUNKSIZE="3000" ; done
  89. if test -z CLEAN_DELAY ; then CLEAN_DELAY="5" ; done
  90. if test -z EMERGE_WARNING_DELAY ; then EMERGE_WARNING_DELAY="10" ; done
  91. if test -z AUTOCLEAN ; then AUTOCLEAN="yes" ; done
  92. if test -z RSYNC_RETRIES ; then RSYNC_RETRIES="3" ; done
  93. if test -z RSYNC_TIMEOUT ; then RSYNC_TIMEOUT="180" ; done
  94. $
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-4 11:25:12 | 显示全部楼层
Post by yongjian
意思和export VAR=1 && command 一个意思。


今天才反应过来,两种形式
  1. $ VAR=1 command
  2. $ export VAR=1 && command
复制代码

还是不一样的。看第 18 贴。
回复 支持 反对

使用道具 举报

发表于 2005-5-6 09:16:49 | 显示全部楼层
Post by herberteuler
今天才反应过来,两种形式
  1. $ VAR=1 command
  2. $ export VAR=1 && command
复制代码

还是不一样的。看第 18 贴。


是不一样,不过对于执行程序来说灭有区别。export 这后会改变环境,想想还是VAR=1 command来的好。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表