Home > Mobile >  Echoing input into a select prompt produces unexpected behaviour
Echoing input into a select prompt produces unexpected behaviour

Time:12-15

I am trying to automate selecting options in a list. When I have chosen Select All from a previous list, to go through each file I have to select 1 then 6, repeatedly. until I have gone through all the files. I would like to be able to automate this, so that after I have selected 1, I then select 4, then it automatically selects 1 and 6 for the rest of the files.

I have tried starting with echoing just 1 into the select prompt and it works but not as expected. It selects option 1 and outputs the information but rather than stopping there, it goes to the next file and selects option 1 until it has gone through all the files.

Why does it do this behaviour and how can I get it to work as expected?

listVideoActions() {
    local numVideos=$((${#VIDEOLIST[@]}))
    actions=("Show video information"
             "Search name on opensubtitles"
             "Search hash on opensubtitles"
             "Repeat last action for all files"
             "Go back to video selection"
    )
    #Add conditional actions. If the Select All option was picked a "Next Video"
    #action is needed to go though all the videos, otherwise just add a "Quit" action
    if [ $# -eq 3 ] && [ $3 -lt $numVideos ]; then actions =("Next video"); actions =("Quit")
    else actions =("Quit"); fi

    printf "${BBlue}$2${Color_Off}\n"
    PS3="Select an action: "

    echo 1 | select action in "${actions[@]}"; do
        printf "\n"
        if   [ $REPLY = 1 ]; then displayVideoInformation "$1"
        elif [ $REPLY = 2 ]; then searchNameOpensubtitles "$1"
        elif [ $REPLY = 3 ]; then searchHashOpensubtitles "$1"
        #elif [ $REPLY = 4 ]; then repeatLast
        elif [ $REPLY = 5 ]; then listVideos;
        elif [ $REPLY = 6 ] && [ $# -eq 3 ] && [ $3 -lt $numVideos ]; then break
        elif [ $REPLY = 6 ]; then exit 1
        elif [ $REPLY = 7 ] && [ $# -eq 3 ] && [ $3 -lt $numVideos ]; then exit 1; fi
    done
}

The call to listVideoActions()

i=1
for video in "${VIDEOLIST[@]}"; do
    listVideoActions "$video" "${video##*/}" $i
    ((i  ))
done

CodePudding user response:

I Have found a way to do this with yes instead of echo.

selectVideoAction() {
    [[ $REPLY -eq 2 || $REPLY -eq 3 || $REPLY -eq 4 ]] && lastAction=$REPLY
    PS3="Select an action($lastAction): "
    printf "\n"

    if   [ $REPLY -eq 1 ]; then listVideos
    elif [ $REPLY -eq 2 ]; then displayVideoInformation "$1"
    elif [ $REPLY -eq 3 ]; then searchNameOpensubtitles "$1"
    elif [ $REPLY -eq 4 ]; then searchHashOpensubtitles "$1"; 
    elif [ $REPLY -eq 5 ]  && [ $selectAll -eq 0 ]; then exit 1; fi
}

listVideoActions() {
    local selectAll=0
    [[ $# -eq 3 && $3 -lt $((${#VIDEOLIST[@]})) ]] && selectAll=1

    local actions=("Go back to video selection")
    actions =("Show video information")
    actions =("Search name on opensubtitles")
    actions =("Search hash on opensubtitles")
    [ $selectAll -ne 0 ] && actions =("Next video")\
    && actions =("Repeat last action on remaining files")
    actions =("Quit")

    printf "${BBlue}$2${Color_Off}\n"
    PS3="Select an action($lastAction): "

    if [ $repeatLast -eq 1 ]; then
        sleep 2 | yes "$lastAction" | select action in "${actions[@]}"; do
            selectVideoAction "$1" "$#" "$3"
            break
        done
    elif [ $repeatLast -eq 0 ]; then
        select action in "${actions[@]}"; do
            selectVideoAction "$1" "$#" "$3"

            if   [ $REPLY = 5 ] && [ "$selectAll" -eq 1 ]; then break
            elif [ $REPLY = 6 ] && [ "$selectAll" -eq 1 ]; then 
                if [ ! -z "${lastAction}" ]; then repeatLast=1; break; fi
                PS3="No last action. Select an action($lastAction): "
            elif [ $REPLY = 7 ] && [ "$selectAll" -eq 1 ]; then exit 1; fi
        done
    fi
}

The call to listVideoActions()

local lastAction=""
local repeatLast=0
local i=1
for video in "${VIDEOLIST[@]}"; do
    listVideoActions "$video" "${video##*/}" "$i"
    ((i  ))
done
  •  Tags:  
  • bash
  • Related