환경 파일에서 모든 변수 내보내기

환경 파일에서 모든 변수 내보내기

특정 시스템 서비스에서 사용하는 모든 변수를 내보내고 EnvironmentFile.

예를 들어 foo.service:

[Service]
...
EnvronmentFile=/foo.env
EnvronmentFile=/bar.env

foo.env:

a=1
#b=2
c=3

bashrc그래서 내 물건 에 추가하고 싶어요

set -a
grep EnvironmentFile /etc/systemd/system/foo.service | grep -v '^ *#' | cut -d'=' -f2 | xargs  -I {} bash -c 'source {};'
set +a

이 글에 명시된 대로답변.

더 우아한 솔루션이 있습니까?

답변1

"source"를 실행하기 위해 새로운 bash 쉘을 실행하고 있기 때문에 전혀 작동하지 않습니다.

노력하다:

load_env() 
{
  local files=( $(egrep '^[ ]*EnvironmentFile' "$1" ) )
  local f 
  set -a
  for f in "${files[@]}"
  do
     .   "${f##*=}"   # this expression delete the EnvironmentFile= part
  done
  set +a
}

load_env /etc/systemd/system/foo.service

관련 정보