现在的位置: 首页 > 综合 > 正文

busybox里环境变量设置的问题

2017年04月07日 ⁄ 综合 ⁄ 共 1156字 ⁄ 字号 评论关闭
方法一[来自网上文章]:
 
busybox里环境变量设置的问题
在busybox带的ash shell下。
如果你手动敲入export 来设置环境变量,是没问题的。但如果在shell 脚本里设置环境变量的话,一点效果都没有。不知道大家有没有遇到过此类问题?
我想要在系统启动的时候设置几个很重要的环境变量,但由于启动脚本里环境变量设置根本起不了作用,每次都得手动。所以没办法,我修改了 busybox源代码的libbb/setup_enviroment.c文件里的setup_environment函数,在里面添加了几个 xsetenv调用来设置我的环境变量,郁闷的是,还是没用。
 
问题解决了,将init=/sbin/init.sh加到传给内核的command string里(如boot=/etc/mtdblock2 mem=32 init=/sbin/init.sh)
 init.sh的内容如下:
 #!/bin/ash
 export TSLIB_TSDEVICE=/dev/touchscreen/ucb1x00
 exec /sbin/init
 原因?
 “Environment variables set in a shell script will not have any effect to
 any parent process.  init runs your rcS but init will never get the ENVs
 set in rcS.  Now if your init process was a shell and it sourced rcS, that
 would be different.
 You interactive shell most likely sourced /etc/profile on its own before
 issuing the prompt.  The kernel will make some of the boot args environment
 variables for init.  I believe all the ones with a '=' in them.
 If you really want init and all its children have a bunch of environment
 variables set try something like this
 boot with: linux init=/sbin/init.sh
 where /sbin/init.sh contains
 ------------------
 #!/bin/ash
 export foo=bar
 exec /sbin/init”
[验证]这是最好的办法。其实很多设置是init=/linuxrc,只要在linuxrc加入环境变量的设置就起作用。
 
方法二:
在/etc/profile设置环境变量
[验证] 有时候设置成功,有时候就不行。

抱歉!评论已关闭.