CWD およびファイルパス文字列は、launchctl 呼び出しで自動的に連結されます。

CWD およびファイルパス文字列は、launchctl 呼び出しで自動的に連結されます。

私は現在私の機能を書こうとしています。バッシュ構成ファイルこれにより、OSX LaunchAgentが引数として渡され、再起動されます。やや無意味かもしれませんが、私には長いパスを書きたくありません。launchctl un/load

私はlaunchctlの最後の呼び出しを除いて、ほとんどすべてのことをしました。以下は、関数スクリプトとそれに続くエラーメッセージです。

機能

launchctl_mgr() {
        failure() {
                local lineno=$1
                local msg=$2
                echo "Failed at $lineno: $msg"
        }
        trap 'failure ${lineno} "$BASH_COMMAND"' ERR
        trap 'trap - ERR' RETURN

        instruction=$1
        filepath=$2;
        #test whether the file argument is already a file or not and whether or not it is actually$
        if [[ ! -f $2 ]]
        then
                if [[ -f ~/Library/LaunchAgents/$2.plist ]]
                then
                        filepath="~/Library/Launchagents/$filepath.plist"
                        echo "found!: $filepath"
                elif [[ -f /Library/LaunchAgents/$2.plist ]]
                then
                        filepath="/Library/LaunchAgents/$filepath.plist"
                else
                        echo "debug: ~/Library/LaunchAgents/$filepath.plist"
                        echo "Filename supplied was not valid as a launchagent"
                        return 1
                fi
        fi
        if [[ $instruction=="restart" || instruction=="unload" ]]
        then
                echo "instruction is $instruction"
                echo "filepath is $filepath"
                launchctl stop $filepath
                launchctl unload $filepath
                if [[ instruction=="restart" ]]
                then
                        launchctl load $filepath
                        launchctl start $filepath
                fi
        fi
}

出力/エラー

____________________ => launchctl_mgr unload local.unminimise
found!: ~/Library/Launchagents/local.unminimise.plist
instruction is unload
filepath is ~/Library/Launchagents/local.unminimise.plist
Failed at launchctl stop $filepath:
/Users/[current_user]/~/Library/Launchagents/local.unminimise.plist: No such file or directory
/Users/[current_user]/~/Library/Launchagents/local.unminimise.plist: No such file or directory
Failed at launchctl start $filepath:

(「[current_user]」は実際のユーザーアカウント名を置き換えます)

launchctlが示すように、bashとlaunchctlの両方が現在のディレクトリパスとlaunchctlに入力したパス文字列を連結してエラーになります。ファイルパス文字列が印刷され、前の行では問題ありません。

ベストアンサー1

  1. [[...]]テストでは==前後にスペースが必要です。それ以外の場合は文字列として評価され、空でないことは常にtrueです。

    たとえば、

    $ instruction=whatever
    $ [[ $instruction=="restart" ]] && echo true || echo false
    true
    

    これは次のとおりです。

    $ [[ whatever=="restart" ]] && echo true || echo false
    true
    

    また、以下と同じです(他のいくつかのバリエーションも含みます)。

    $ [[ -n 'whatever=="restart"' ]] && echo true || echo false
    true
    

    $instructionこれらすべての場合、「再起動」が同じであることを確認しません。代わりに文字列がwhatever=="restart"空でないことをテストしています。文字列には文字の順序が正確に含まれていますが、==これは重要ではありません。これは文字列に含まれる文字にすぎず、特別な意味はありません。

    比較:

    $ instruction=whatever
    $ [[ $instruction == "restart" ]] && echo true || echo false
    false
    $ instruction=restart
    $ [[ $instruction == "restart" ]] && echo true || echo false
    true
    

  1. スクリプトには$before変数が欠落している場所もあります。例えば

    if [[ $instruction=="restart" || instruction=="unload" ]]
    

    先行および末尾のスペースが必要です。== そして$2番目の前にa instruction

    if [[ $instruction == "restart" || $instruction == "unload" ]]
    

    ここでも同様です。

    if [[ instruction=="restart" ]]
    

    これは次のようになります。

    if [[ $instruction == "restart" ]]
    

  1. 変数(およびシェル位置パラメータ)を使用するときは、二重引用符で囲む必要があります。例えば

    local lineno="$1"
    local msg="$2"
    

    そして:

    instruction="$1"
    filepath="$2"
    

    そして:

    launchctl stop "$filepath"
    launchctl unload "$filepath"
    

  1. スタンドアロンスクリプトの代わりに関数で書くのはなぜですか?現在のシェルの環境を変更するために必要ですか?

    関数を書くには問題はありませんが、考える現在のシェルの環境を変更したい場合は、誤って環境が変更されないように、別のスクリプトによって提供される分離機能を活用することをお勧めします。

    またはステートメントみんな関数変数($instructionおよび含む$filepathlocalを変更しましたが、これは変数の変更を防ぐためにのみ機能し、関数はまだシェルの現在のディレクトリなどの他の項目を変更できます。

おすすめ記事