检查根权限时未调用Bash函数

问题描述

我想制作一个bash脚本来检查是否安装了指定的软件包。这样做通常需要root权限。

我希望整个代码都能正常工作,但是在调用check_requirements时无法正常工作。 它不会打印任何消息。

代码如下:

#!/bin/bash

# This function installs the specified dependencies when called
install_requirements () {
    echo "$BCyan Downloading required packages... $Color_Off"
    apt-get install -y ${pkgs[@]}
    
    if [ "$?" -eq 0 ]; then
        echo "$Green Requirements are installed."
    else
        echo "$BRed Error encountered while downloading requirements."
    fi
    echo "$Blue Cleaning apt leftovers... $Color_Off" && apt-get autoclean && apt-get autoremove
}

# This function check if specified dependencies (aka packages) are installed. If not,install
check_requirements () {
    pkgs=( "zip" "unzip" "wget" "curl" "default-jdk" "jq" )
    ## Run the run_install function if sany of the libraries are missing
    dpkg -s "${pkgs[@]}" >/dev/null 2>&1 || install_requirements
}

root_check () {
    if [ "$EUID" -ne 0 ];then 
        sudo "$0" "$@"
        if [ "$?" -eq 0 ]; then 
            check_requirements
        else 
            echo "$Red Couldn't check for updates $Color_Off"
        fi
    fi
}

root_check

我在这里呆了很长时间。你能帮我找到我的困境的答案吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...