mirror of
https://github.com/bin456789/reinstall.git
synced 2025-01-19 04:49:13 +08:00
core: 用 type 判断命令是否存在
This commit is contained in:
parent
69821dae24
commit
9a75f2ec0e
56
reinstall.sh
56
reinstall.sh
@ -210,7 +210,7 @@ is_virt() {
|
|||||||
# aws t4g debian 11 systemd-detect-virt 为 none,即使装了dmidecode
|
# aws t4g debian 11 systemd-detect-virt 为 none,即使装了dmidecode
|
||||||
# virt-what: 未装 deidecode时结果为空,装了deidecode后结果为aws
|
# virt-what: 未装 deidecode时结果为空,装了deidecode后结果为aws
|
||||||
# 所以综合两个命令的结果来判断
|
# 所以综合两个命令的结果来判断
|
||||||
if command -v systemd-detect-virt && systemd-detect-virt; then
|
if is_have_cmd systemd-detect-virt && systemd-detect-virt; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
# debian 安装 virt-what 不会自动安装 dmidecode,因此结果有误
|
# debian 安装 virt-what 不会自动安装 dmidecode,因此结果有误
|
||||||
@ -572,33 +572,26 @@ verify_os_string() {
|
|||||||
usage_and_exit
|
usage_and_exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get_cmd_path() {
|
||||||
|
# arch 云镜像不带 which
|
||||||
|
# command -v 包括脚本里面的方法
|
||||||
|
type -f -p $1
|
||||||
|
}
|
||||||
|
|
||||||
|
is_have_cmd() {
|
||||||
|
get_cmd_path $1 >/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
install_pkg() {
|
install_pkg() {
|
||||||
is_in_windows && return
|
is_in_windows && return
|
||||||
|
|
||||||
for cmd in "$@"; do
|
|
||||||
# 用 which 而不是 command -v,因为 command -v 把脚本中的function也算在内
|
|
||||||
if ! which $cmd >/dev/null 2>&1 ||
|
|
||||||
# gentoo 默认编译的 unsquashfs 不支持 xz
|
|
||||||
{ [ "$cmd" = unsquashfs ] &&
|
|
||||||
which emerge >/dev/null 2>&1 &&
|
|
||||||
! unsquashfs |& grep -w xz &&
|
|
||||||
echo "unsquashfs not supported xz. need rebuild."; }; then
|
|
||||||
|
|
||||||
if ! find_pkg_mgr; then
|
|
||||||
error_and_exit "Can't find compatible package manager. Please manually install $cmd."
|
|
||||||
fi
|
|
||||||
cmd_to_pkg
|
|
||||||
install_pkg_real
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
find_pkg_mgr() {
|
find_pkg_mgr() {
|
||||||
if [ -z "$pkg_mgr" ]; then
|
if [ -z "$pkg_mgr" ]; then
|
||||||
# command -v 有先后顺序,dnf放yum前面
|
for c in dnf yum apt pacman zypper emerge apk; do
|
||||||
if ! pkg_mgr=$(command -v dnf yum apt pacman zypper emerge apk | head -1 | awk -F/ '{print $NF}' | grep .); then
|
is_have_cmd $c && pkg_mgr=$c && return
|
||||||
|
done
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd_to_pkg() {
|
cmd_to_pkg() {
|
||||||
@ -641,6 +634,23 @@ install_pkg() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for cmd in "$@"; do
|
||||||
|
if ! is_have_cmd $cmd ||
|
||||||
|
{
|
||||||
|
# gentoo 默认编译的 unsquashfs 不支持 xz
|
||||||
|
[ "$cmd" = unsquashfs ] &&
|
||||||
|
is_have_cmd emerge &&
|
||||||
|
! unsquashfs |& grep -w xz &&
|
||||||
|
echo "unsquashfs not supported xz. need rebuild."
|
||||||
|
}; then
|
||||||
|
if ! find_pkg_mgr; then
|
||||||
|
error_and_exit "Can't find compatible package manager. Please manually install $cmd."
|
||||||
|
fi
|
||||||
|
cmd_to_pkg
|
||||||
|
install_pkg_real
|
||||||
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
check_ram() {
|
check_ram() {
|
||||||
@ -1151,8 +1161,8 @@ build_cmdline
|
|||||||
info 'create grub config'
|
info 'create grub config'
|
||||||
# linux grub
|
# linux grub
|
||||||
if ! is_in_windows; then
|
if ! is_in_windows; then
|
||||||
if command -v update-grub; then
|
if is_have_cmd update-grub; then
|
||||||
grub_cfg=$(grep -o '[^ ]*grub.cfg' "$(which update-grub)")
|
grub_cfg=$(grep -o '[^ ]*grub.cfg' "$(get_cmd_path update-grub)")
|
||||||
else
|
else
|
||||||
# 找出主配置文件(含有menuentry|blscfg)
|
# 找出主配置文件(含有menuentry|blscfg)
|
||||||
# 如果是efi,先搜索efi目录
|
# 如果是efi,先搜索efi目录
|
||||||
|
Loading…
x
Reference in New Issue
Block a user