Quantcast
Viewing all articles
Browse latest Browse all 2722

シェルスクリプトでArchLinuxにインストールされているパッケージを確認する

何をするコード?

シェルスクリプトでインストールされているパッケージを確認して、もしパッケージがインストールされていなかったり古かったりしたら警告を出します。

コード

dependence=("git""wget""make""hoge""archiso""search-in-dir-git")installed_pkg=($(pacman -Q | awk'{print $1}'))installed_ver=($(pacman -Q | awk'{print $2}'))

check_pkg(){local i
    local latest_ver
    for i in$(seq 0 $((${#installed_pkg[@]}-1)));do
        if[["${installed_pkg[${i}]}"=${1}]];then
            latest_ver=$(pacman -Sp--print-format'%v'${1} 2> /dev/null)if[["${installed_ver[${i}]}"="${latest_ver}"]];then
                echo-n"installed"return 0
            elif[[-z${latest_ver}]];then
                echo-n"norepo"return 0
            else
                echo-n"old"return 0
            fi
        fi
    done
    echo-n"not"return 0
}echo"Checking dependencies ..."for pkg in${dependence[@]};do
    echo-n"Checking ${pkg} ..."case$(check_pkg ${pkg})in"old")echo-en" $(pacman -Q${pkg} | awk'{print $2}')\n\n"echo"${pkg} is not the latest package."echo"Local: $(pacman -Q${pkg} 2> /dev/null | awk'{print $2}') Latest: $(pacman -Sp--print-format'%v'${pkg} 2> /dev/null)"echo;;"not")echo-e" ${pkg} is not installed.";check_failed=true;;"norepo")echo-en" $(pacman -Q${pkg} | awk'{print $2}')\n\n"echo"${pkg} is not a repository package.";;"installed")echo-ne" $(pacman -Q${pkg} | awk'{print $2}')\n";;esacdone

if[["${check_failed}"=true]];then
    exit 1
fi

解説もどき

check_pkg関数でパッケージの状態を判定しています。あとは依存関係の配列をforで変数pkgに順番にぶち込んで関数を実行させます。
この関数は最低限の値しか返しません。それぞれの値の意味は以下のとおりです。

not→インストールされていない
norepo→リポジトリのパッケージではない(インストールはされている)
installed→最新版が正常にインストールされている
old→古いバージョンがインストールされている

あとは関数の実行結果をcaseで別の処理にしているだけです。


Viewing all articles
Browse latest Browse all 2722

Trending Articles