You are not logged in.

#1 2024-07-18 13:00:11

meezoarch
Member
Registered: 2022-12-01
Posts: 42

Modify a script to allow multiple selections

The below is part of the bashmount script.
The script accepts input as below:
3e
This will eject (e) device number (3).

How can I modify the script to allow me to select more than one device and apply an action to them? For e.g. m3,5e => multi select mode, eject device 3 and 5
Or perhaps 3e 5e
Or any other approach that will add such functionality to the script.

select_action() {
    local devname
    local letter
    print_separator
    if ! read -r -e -p "${BOLD}Command:${ALL_OFF} " action; then
        # Exit on ctrl-d.
        printf '\n'
        exit 0
    fi
    if [[ "$action" =~ ^[1-9] ]]; then
        if [[ "$action" =~ ^[1-9][0-9]*$ ]]; then
            # Zero-based numbering for array elements, so subtract one.
            local number="$(( action - 1 ))"
            if (( number >= device_number )); then
                invalid_command
                return 1
            fi
            devname=${listed[number]}
            while true; do
                submenu || break
            done
        elif [[ "$action" =~ ^[1-9][0-9]*[eimou]$ ]]; then
            # Zero-based numbering for array elements, so subtract one.
            local number="$(( ${action%?} - 1 ))"

            if (( number >= device_number )); then
                invalid_command
                return 1
            fi
            devname="${listed[number]}"

            letter="${action: -1}"
            case "$letter" in
                "e") action_eject "$devname";;
                "i") action_info "$devname";;
                "m") action_mount "$devname";;
                "o") action_open "$devname";;
                "u") action_unmount "$devname";;
                 *)  return 1;;
            esac
            return 0
        else
            invalid_command
            return 1
        fi
    else
        case "$action" in
            "a")
                if (( ! ${#mounted[*]} )); then
                    error "No devices mounted."
                    return 1
                fi
                printf '\n'
                read -r -e -p "Unmount all devices [y/N]?: " unmount
                [[ "$unmount" != "y" ]] && [[ "$unmount" != "Y" ]] && return 0
                clear
                for devname in "${mounted[@]}"; do
                    action_unmount "$devname" || continue
                done
                enter_to_continue
                return 1;;
            "r"|"") return 0;;
            "q"|"b") exit 0;;
            "?") print_help; return 0;;
            *) invalid_command; return 1;;
        esac
    fi
}

Last edited by meezoarch (2024-07-18 14:05:03)

Offline

#2 2024-07-18 19:25:39

jaywk
Member
Registered: 2020-12-14
Posts: 14

Re: Modify a script to allow multiple selections

I think this should work for commands which are separated by the IFS (Internal Field Separator) variable, which is the space char by default.


select_action() {
    local devname
    local letter
    print_separator
    if ! read -r -e -p "${BOLD}Command:${ALL_OFF} " string_of_actions; then
        # Exit on ctrl-d.
        printf '\n'
        exit 0
    fi
    set -f
    array_of_actions=($string_of_actions)
    set +f

    for action in "${array_of_actions[@]}"
    do
	if [[ "$action" =~ ^[1-9] ]]; then
            ....
            ....
        fi
    done
}

Offline

#3 2024-07-18 22:45:17

meezoarch
Member
Registered: 2022-12-01
Posts: 42

Re: Modify a script to allow multiple selections

jaywk wrote:

I think this should work for commands which are separated by the IFS (Internal Field Separator) variable, which is the space char by default.

Okay thanks. I need to try this but I am away for few days.
I like your approach. So this essentially allows the user to execute any of the below possibilities as action commands:

3e #single command, the original behaviour of the script
3e 5e 6e #same command on more than one device 
3e 5u 6m #different commands on different devices 

Am I right?

Offline

#4 2024-07-19 14:45:29

jaywk
Member
Registered: 2020-12-14
Posts: 14

Re: Modify a script to allow multiple selections

Yes, I think so. These commands should be processed in that order, just like if you entered them one by one.
I tested the modified / new lines of the script, but not the whole modified script.

Offline

#5 2024-07-24 15:11:41

meezoarch
Member
Registered: 2022-12-01
Posts: 42

Re: Modify a script to allow multiple selections

jaywk wrote:

Yes, I think so. These commands should be processed in that order, just like if you entered them one by one.
I tested the modified / new lines of the script, but not the whole modified script.

I finally managed to test your solution.
Unfortunately it was not working.
Only the FIRST action gets executed.
After some digging, I had to comment a return command from the below part of the select_action function:

case "$letter" in
                "e") action_eject "$devname";;
                "i") action_info "$devname";;
                "m") action_mount "$devname";;
                "o") action_open "$devname";;
                "u") action_unmount "$devname";;
                 *)  return 1;;
            esac
            return 0

It was obviously preventing execution of subsequent commands.
It is now working like charm.
I guess this also means if any action in the array fails and exits with a non zero code, the subsequent actions will not be executed. This may not necessarily be a bad behavior and I am happy to live with it.
I think this can now be marked as resolved thank you.

Last edited by meezoarch (2024-07-24 15:20:11)

Offline

Board footer

Powered by FluxBB