执行从参数接收到的命令到 Bash 脚本中的函数

问题描述

我正在编写一个脚本来在 linux 中添加删除 samba 共享。此脚本需要安装 samba 和对话框。

我的完整代码位于 https://paste.ubuntu.com/p/Q45fjcFbwr/

这是一个片段。

#!/bin/bash

function display_output() {
    dialog --backtitle "" --title "" --clear --msgBox "$1" 10 90
}

function execute() {
    execmd=("$1")
    ${execmd[@]}                   # doesn't do anything it seems...
    #display_output "${execmd[@]}"
}

function yesno() {
    # store command string
    cmd=$1
    
    dialog --title "Confirm" \
       --backtitle "Confirm command" \
       --yesno "$cmd" 7 90
    confirm=$?
    
    # actions
    case $confirm in
    0) execute "$cmd";;
    1) break;;
    255) break;;

    esac
}    

function create_share() {
    ...

    if [ $cancelled = "no" ]; then
    case $1 in
    PUBW) yesno "net usershare add \"${VALUES_ARRAY[0]}\" \"${VALUES_ARRAY[1]}\" \"${VALUES_ARRAY[2]}\" Everyone:F guest_ok=y && chmod 777 \"${VALUES_ARRAY[1]}\"";;
    PUBR) yesno "net usershare add \"${VALUES_ARRAY[0]}\" \"${VALUES_ARRAY[1]}\" \"${VALUES_ARRAY[2]}\" Everyone:R guest_ok=y";;
    PRVW) yesno "net usershare add \"${VALUES_ARRAY[0]}\" \"${VALUES_ARRAY[1]}\" \"${VALUES_ARRAY[2]}\" Everyone:F guest_ok=n && chmod 777 \"${VALUES_ARRAY[1]}\"";;
    PRVR) yesno "net usershare add \"${VALUES_ARRAY[0]}\" \"${VALUES_ARRAY[1]}\" \"${VALUES_ARRAY[2]}\" Everyone:R guest_ok=n";;
    esac
    fi
}

# try out main menu
# the main menu is in an infinite loop
...

在 execute() 中,display_output "${execmd[@]}" 在 msgBox 中以字符串形式正确显示命令。

但是,当我期望它实际执行命令时,${execmd[@]} 似乎没有做任何事情。

谢谢

解决方法

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

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

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