bash中过度设计的checkroot

时间:2018-11-18 20:46:16

标签: linux bash shell

我需要有关bash脚本(我正在掌握atm)的帮助。

如果要从非root用户进行调用,我想更好地使用checkroot来调用susudo等。

端子: kreyren@dreamon ~ $ scripter --something

代码块:

# ROOT CHECK
checkroot () {
    if [ $UID == 0 ]; then
    echo "Root is detected."
    break

    # Using `sudo`
    elif [ $UID != 0 ] && [[ -x sudo ]]; then 
    echo "Scripter needs root permission to proceed, trying using sudo."
    #TODO: Make the script invoke scripter with CLI argumments.
    sudo scripter #expected to be invoked with --something.

    # Using `su`
    elif [ $UID != 0 ]; then 
        echo "Scripter needs root permission to proceed, trying to log-in as root."
        #TODO: Make the script invoke scripter with CLI argumments.
        su -c "scripter"   #expected to be invoked with --something.
fi
}

我正在使用https://gitlab.com/argent/avasile作为此代码块的测试对象。<​​/ p>

代码块应该能够捕获调用的命令,并使用指定的变量AKA解析“ TODO”重新调用自身。

我当前所在的位置:https://github.com/Kreyren/random-scripts/blob/master/checkroot_WIP.sh

条件: 1)必须是功能的一部分才能具有通用性。 2)必须在没有其他用户输入(解析密码除外)的情况下进行。


我花了5天了,但我的主意是到目前为止,我最好的解决方案似乎是从history那里抢来的?

感谢任何人提供更多信息,包括理论和/或概念。 -格雷

1 个答案:

答案 0 :(得分:1)

您可以使用Bourne shell特殊变量$@使用所有原始参数再次调用脚本:

exec sudo scripter $@