将变量从一个 shell 脚本导出到另一个 shell 脚本
我有几个脚本,它们的第一部分看起来是一样的。这部分的功能是识别脚本在哪台机器上运行并相应地设置几个变量。它看起来像这样:
ENV=`echo $LOGNAME | cut -c1-8`
if [ $ENV = "vrt3400b" ]
then
echo "Using TEST specific settings."
NAME_PREFIX="tst"
GROUP_NUMBER=`echo $USER | cut -c4-5`
GROUP_NUMBER_SUFFIX=00`echo $USER | cut -c8-9`
...
elif [ $ENV = "vrp3400a" ]
then
echo "Using PROD specific settings."
NAME_PREFIX="prd"
...
问题是,随着脚本数量的增加,维护小更改的开销变得非常耗时。
我提取了上面的部分并将其放入一个单独的脚本中,然后由所有其他脚本调用。但变量当然不会转发到其他脚本。也尝试了 export NAME_PREFIX="tst"
但没有成功。
谁能告诉我应该使用哪种方法来解决问题?
我正在考虑让该部分识别环境,将属性写入文件,然后可以将其传递给其他脚本。但似乎必须有一个更直接的方法。
// 迈克
I have a couple of scripts for which the first part of them looks the same. Function of this part is to identify at which machine the script is being runned and set a couple of variables accordingly. It looks something like this:
ENV=`echo $LOGNAME | cut -c1-8`
if [ $ENV = "vrt3400b" ]
then
echo "Using TEST specific settings."
NAME_PREFIX="tst"
GROUP_NUMBER=`echo $USER | cut -c4-5`
GROUP_NUMBER_SUFFIX=00`echo $USER | cut -c8-9`
...
elif [ $ENV = "vrp3400a" ]
then
echo "Using PROD specific settings."
NAME_PREFIX="prd"
...
The problem is that as the number of scripts grow the overhead of maintaining small changes gets very time consuming.
I extracted the above part and put it into a separate script, that is then called by all the other scripts. But the variables are fo course not forwarded to the other scripts. Tried export NAME_PREFIX="tst"
aswell but it didn't work.
Could anyone give me a hint on which approach I should use to solve the problem?
I was thinking about letting the part identifiying the environment, write properties to file which can then be passed to other scripts. But it seems that there must be a more straightforward approach.
// Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
初始化脚本(1.sh)
应用程序脚本
Initializing script (1.sh)
Application script