フルbashシェルスクリプトの全体的な進捗状況

フルbashシェルスクリプトの全体的な進捗状況

明確にするために編集された:

次のスクリプトがあるとします(pvとカールがインストールされているとします)。

(現在、Ubuntuで実行していますが、より多くのLinuxディストリビューションで実行できるようにPOSIX互換にする予定です)

#!/bin/bash
sudo apt install vlc
mkdir -p ~/.steam/compatibilitytools.d
PROTONVERSIONNUMBER=$(curl -v --silent https://api.github.com/repos/popsUlfr/Proton/releases 2>&1 | grep "tag_name" | head -n 1 | cut -f4,4 -d"\"")
REPLACING=$(curl -v --silent https://api.github.com/repos/popsUlfr/Proton/releases 2>&1 | grep "target_commitish" | head -n 1 | cut -f4,4 -d"\"" | sed "s/[^_]\+/\L\u&/g")
PROTONVERSION=${REPLACING/_G/-6_G}
PROTONNAME=$PROTONVERSION"_"${PROTONVERSIONNUMBER##*-}
wget https://github.com/popsUlfr/Proton/releases/download/$PROTONVERSIONNUMBER/$PROTONNAME.tar.xz
pv $PROTONNAME.tar.xz | tar xp -J -C ~/.steam/compatibilitytools.d
rm $PROTONNAME.tar.xz

私の目には非常に見栄えの良い3つの進行状況バーが表示されます。

正確さとそのようなものはわからない私を変な奴と呼ぶ

質問

基本的な「実際の」進行状況バーの現在の進行状況バー「速度」に準拠した連続進行状況バーを形成するために、これら3つの独立した進行状況バーの機能をどのように活用できますか?

ベストアンサー1

以下は、複数の「作業」セクションがすべて同じスケジュールを更新する方法を示すサンプルコードです。 Whiptail機器はスクリプトのファイル記述子3に添付され、スクリプト中にいつでも更新できます。 (スクリプトが終了するかFD 3が明示的に閉じられると、機器は自動的に終了します。)

#!/bin/bash
#
pid=
tmpd=

tidyUp()
{
    # Clean up when we're done
    exec 3>&-
    [[ -n "$tmpd" ]] && rm -rf "$tmpd"
}
trap 'ss=$?; tidyUp; exit $ss' 1 2 15


updateGauge()
{
    local percent="$1" message="$2"
    printf "XXX\n%d\n%s\nXXX\n" $percent "$message" >&3
}


# Create the FIFO for communicating with the whiptail gauge
tmpd=$(mktemp --tmpdir --directory "wt.XXXXXXXXXX")
mkfifo "$tmpd/fifo"

# Start up the whiptail gauge and associate FD 3 with its status
whiptail --title 'Progress meter' --gauge 'Starting examples' 6 50 0 <"$tmpd/fifo" &
exec 3>"$tmpd/fifo"

# Real code starts here
percent=0

for example in 1 2 3
do
    updateGauge $percent "Getting example $example"
    sleep 3    # wget something

    percent=$((percent + 20))
done

for another in 4 5
do
    updateGauge $percent "Doing work for another example $another"
    sleep 2    # do some work

    percent=$((percent + 20))
done

# Done
tidyUp
exit 0

おすすめ記事