プロセスが完了するのを待つステートメントは無視されます。

プロセスが完了するのを待つステートメントは無視されます。

sudo権限を使用せずにgitインストールを自動化しようとしています。良い(しかし遅い)回避策はgitにバンドルされており、xcode-select --install標準ユーザーが呼び出すことができるXcodeをインストールすることです。

#!/bin/bash
# check if user has git installed
which git &> /dev/null
OUT=$?
sleep 3


# if output of git check is not 0(not foud) then run installer
if [ ! $OUT -eq 0 ]; then
    xcode_dialog () {
        XCODE_MESSAGE="$(osascript -e 'tell app "System Events" to display dialog "Please click install when Command Line Developer Tools appears"')"
        if [ "$XCODE_MESSAGE" = "button returned:OK" ]; then
            xcode-select --install
        else
            echo "You have cancelled the installation, please rerun the installer."
        fi
    }
    xcode_dialog
fi

# get xcode installer process ID by name and wait for it to finish
until  [ ! "$(pgrep -i 'Install Command Line Developer Tools')" ]; do
    sleep 1
done

echo 'Xcode has finished installing'

which git &> /dev/null
OUT=$?
if [ $OUT = 0 ]; then
    echo 'Xcode was installed incorrectly'
    exit 1
fi

しかし、私のuntilステートメントは完全に無視され、XCODE_MESSAGEOKが返されるとすぐにgitの2番目のチェックがほとんど実行されます。インストーラが完了するのを待つロジックをよりよく実装する方法を知っている人はいますか?

ベストアンサー1

スクリプトに従うアプローチを少し変更します。 「git」があることを確認する代わりに、インストールプロセスが実行されていないことを確認してください。

#!/bin/bash

# check if user has git installed and propose to install if not installed
if [ "$(which git)" ]; then
        echo "You already have git. Exiting.."
        exit
else
        XCODE_MESSAGE="$(osascript -e 'tell app "System Events" to display dialog "Please click install when Command Line Developer Tools appears"')"
        if [ "$XCODE_MESSAGE" = "button returned:OK" ]; then
            xcode-select --install
        else
            echo "You have cancelled the installation, please rerun the installer."
            # you have forgotten to exit here
            exit
        fi
fi

until [ "$(which git)" ]; do
        echo -n "."
        sleep 1
done
echo ""

echo 'Xcode has finished installing'

おすすめ記事