初学,在configuration文件夹中有个example.ini文件,我想读取里面的值变成环境变量,怎么才能实现?

example.ini

UNIVER_SEARCH_VIDEO=http://127.0.0.1/api/search/common
UNIVER_MONGO_URL=mongodb://127.0.0.1
UNIVER_SEARCH_LABEL=http://127.0.0.1api/search/common
UNIVER_USER_LOGIN=http://127.0.0.1/api/login
UNIVER_TASK_MANAGER=http://127.0.0.1/api/task/manager`请输入代码`

我希望从example.ini读取这五个值,然后赋给环境变量,怎么才能实现?

新人不太会写,有个大致的demo就行

继续求助下

我现在的environment-variable.ini文件内容是

UNIVER_USER_LOGIN=http://127.0.0.1/api/login
UNIVER_SEARCH_ORDINARY=http://127.0.0.1api/search/common
UNIVER_SEARCH_LABEL=http://127.0.0.1/api/search/common
UNIVER_SEARCH_VIDEO=http://127.0.0.1/api/search/common
UNIVER_TASK_MANAGER=http://127.0.0.1/api/task/manager
UNIVER_PROJECR_URL=567.110.220.11
UNIVER_PROJECR_PORT=667777760011

我执行命令

#!/bin/bash

grep 'UNIVER_USER_LOGIN' environment-variable.ini >> /etc/profile
grep 'UNIVER_SEARCH_LABEL' environment-variable.ini >> /etc/profile
grep 'UNIVER_SEARCH_VIDEO' environment-variable.ini >> /etc/profile
grep 'UNIVER_SEARCH_VIDEO' environment-variable.ini >> /etc/profile
grep 'UNIVER_TASK_MANAGER' environment-variable.ini >> /etc/profile
grep 'UNIVER_PROJECR_URL' environment-variable.ini >> /etc/profile
grep 'UNIVER_PROJECR_PORT' environment-variable.ini >> /etc/profile


source /etc/profile

:<<!
if [ $UNIVER_SEARCH_VIDEO ]; then
    echo ${UNIVER_SEARCH_VIDEO}
else
    echo "找不到"
fi

原来的没有的环境变量无法修改,比如这个环境变量UNIVER_PROJECR_PORT

怎么代码怎么优化?

如果电脑上原来有环境变量,可以将原来的环境变量修改

如果原来没有,就没有效果

新改的

#!/bin/bash

grep 'UNIVER_USER_LOGIN' environment-variable.ini >> /etc/profile
grep 'UNIVER_SEARCH_LABEL' environment-variable.ini >> /etc/profile
grep 'UNIVER_SEARCH_VIDEO' environment-variable.ini >> /etc/profile
grep 'UNIVER_SEARCH_VIDEO' environment-variable.ini >> /etc/profile
grep 'UNIVER_TASK_MANAGER' environment-variable.ini >> /etc/profile
grep 'UNIVER_PROJECR_URL' environment-variable.ini >> /etc/profile
echo "UNIVER_PROJECR_PORT='xxx'" >> /etc/profile
sed -i '/UNIVER_PROJECR_PORT/s/xxx/79000000/g' /etc/profile
grep 'UNIVER_PROJECR_PORT' environment-variable.ini >> /etc/profile

source /etc/profile

图片描述

阅读 2.3k
1 个回答
方法很多,随便列一个,也不一定可以
grep 'UNIVER_SEARCH_VIDEO' example.ini >> /etc/profile
...

source /etc/profile
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进