busybox里环境变量设置的问题

本文介绍了BusyBox中环境变量设置的两种有效方法:通过在init脚本中设置环境变量,确保其对init进程及其子进程生效;以及在/etc/profile中进行设置的方法。这两种方法能够解决在BusyBox shell脚本中设置的环境变量不起作用的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一[来自网上文章]:
 
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设置环境变量
[验证] 有时候设置成功,有时候就不行。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值