0

I am working on a script to add and remove samba shares in linux. This script requires samba and dialog installed.

My complete code is at https://paste.ubuntu.com/p/Q45fjcFbwr/

Here's a snippet.

#!/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
...

Inside execute(), display_output "${execmd[@]}" correctly displays the command in string form in a msgbox.

But, ${execmd[@]} doesn't seem to do anything when I expect it to actually execute the command.

Thanks

Logan Lee
  • 807
  • 9
  • 21
  • Storing/passing commands as strings doesn't work; see [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) and [many](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia) [previous](https://stackoverflow.com/questions/13365553/setting-an-argument-with-bash) [questions](https://stackoverflow.com/questions/29527983/why-do-bash-parameter-expansions-cause-an-rsync-command-to-operate-differently). But ignore the answers that recommend `eval` -- that way lies madness and really weird bugs. – Gordon Davisson Jan 21 '21 at 02:30
  • 1
    Does this answer your question? [Bash script - variable content as a command to run](https://stackoverflow.com/questions/5998066/bash-script-variable-content-as-a-command-to-run) – Zois Tasoulas Jan 21 '21 at 02:31
  • The whole point of using an array is lost if you don't quote it properly; `"${execmd[@]}"` – tripleee Jan 21 '21 at 05:23
  • Could you please either accept the nominated duplicate, or [edit] the question to provide a [mre] which demonstrates the problem without `dialog` and Samba. – tripleee Jan 21 '21 at 05:24

0 Answers0