fakerootで変数と関数を渡す

fakerootで変数と関数を渡す

プロジェクトでfakerootを使用したいのですが、プロジェクトにfakerootに渡す必要がある関数と変数がたくさんあります。

#!/bin/bash
myVar="foo"

function testFunction() {
    echo "$myVar"
}

fakeroot -- bash -c testFunction

しかし、実行されないか、testFunctionエコーされません。myVar

ベストアンサー1

bashエクスポート機能機能を使用することもできます。ただし、スクリプトfakerootなので、そのような環境でこれらの変数を削除しないshシステムで実装する必要があります。これが発生しないようにするには、インタプリタ自体として解釈する必要があります。shBASH_FUNC_fname%%dashfakerootbashbash -o posixsh

#!/bin/bash -
myVar="foo"

testFunction() {
    printf '%s\n' "$myVar"
}

export myVar
export -f testFunction

fakeroot=$(command -v fakeroot)
bash -o posix -- "${fakeroot:?fakeroot not found}" -- bash -c testFunction

また、それを実行しているすべての人が利用できるmyVarようにするには、エクスポートする必要があります。同時にとを呼び出すのではなく、宣言する前にエクスポートすることもできます。bashfakerootexportmyVartestFunctionset -o allexport

おすすめ記事