何をするコード?
シェルスクリプトでインストールされているパッケージを確認して、もしパッケージがインストールされていなかったり古かったりしたら警告を出します。
コード
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
で別の処理にしているだけです。