2016-06-16 11 views
7

systemdで新しい環境変数を設定するときに、他の環境変数を参照することはできますか?Systemd内の他の環境変数の参照

[Service] 
EnvironmentFile=/etc/environment 
Environment=HOSTNAME=$COREOS_PRIVATE_IPV4 
Environment=IP=$COREOS_PRIVATE_IPV4 
Environment=FELIX_FELIXHOSTNAME=$COREOS_PRIVATE_IPV4 

上記のコードは機能していないようです。

+1

私はUnix&Linuxサイトに適しているので、この質問を議論の対象外とすることにしました。プログラミングには直接関係しません。 http://unix.stackexchange.com/ –

答えて

8

これは実際にはunix & linuxの質問です。しかし、それにもかかわらず:いいえsystemdは、Environment=の中で環境変数拡張を行いません。 man systemd.execから:あなたはドキュメント$wordの例から見

Environment= 
     Sets environment variables for executed processes. Takes a space-separated list of variable assignments. This 
     option may be specified more than once, in which case all listed variables will be set. If the same variable is 
     set twice, the later setting will override the earlier setting. If the empty string is assigned to this option, 
     the list of environment variables is reset, all prior assignments have no effect. Variable expansion is not 
     performed inside the strings, however, specifier expansion is possible. The $ character has no special meaning. 
     If you need to assign a value containing spaces to a variable, use double quotes (") for the assignment. 

     Example: 

      Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6" 

     gives three variables "VAR1", "VAR2", "VAR3" with the values "word1 word2", "word3", "$word 5 6". 

としてだけ$word何の拡張が行われていないことを意味します。 指定子についてそのman交渉があること%i%n%u、など彼らは(自分のmanセクションの下)man systemd.unitにしています。一方ExecStart=およびその誘導体


環境変数拡張を実行します。 ExecStart=で環境変数を使用することは、systemdの余分な環境変数の一般的な回避策です。私はと信じています。それはまたのうちの1つで、多くの最近のプログラムが環境およびコマンドラインパラメータから同じパラメータを受け入れる理由です。 man systemd.serviceから

ExecStart=

拡張の例、:

Example: 

     Environment="ONE=one" 'TWO=two two' 
     ExecStart=/bin/echo $ONE $TWO ${TWO} 

    This will execute /bin/echo with four arguments: "one", "two", "two", and "two two". 

    Example: 

     Environment=ONE='one' "TWO='two two' too" THREE= 
     ExecStart=/bin/echo ${ONE} ${TWO} ${THREE} 
     ExecStart=/bin/echo $ONE $TWO $THREE 

    This results in echo being called twice, the first time with arguments "'one'", "'two two' too", "", and the second 
    time with arguments "one", "two two", "too". 

systemdは、そのドキュメントはmanいくつかの年代全体に広がっているが、1は、しばらく後に、それらに使用されます。