質問

Assuming:

export TEST=/somewhere

I want to run the command /somewhere/program using:

with cd('$TEST'):
  run('program')

However, this doesn't work because the $ gets escaped.

Is there a way to use an environment variable in a Fabric cd() call?

役に立ちましたか?

解決

Following suggestion from @AndrewWalker, here is a more compact solution that worked for me (and to my knowledge, the result is the same):

with cd(run("echo $TEST")):
  run("program")

But I decided to go for a (very slightly) more concise yet as readable solution:

run('cd $TEST && program')

This second solution, if I am correct, produces the same result.

他のヒント

You can capture the value by using echo

testdir = str(run("echo $TEST"))
with cd(testdir):
    run("program")

Alternatively:

import os

def my_task():
    with lcd(os.environ['TEST_PATH']):
        local('pwd')

os.getenv('TEST_PATH') may also be used (with a default, optionally)

Hat tip: Send bash environment variable back to python fabric

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top