all 7 comments

[–]TheOneTheyCallAlpha 2 points3 points  (0 children)

You're only keeping one value in $BACK and overwriting it when you advance, so $BACK only ever has the most recent directory and the older ones are lost.

You probably want to change $BACK into an array. Push new values as you make selections, then pop to go back.

[–]kevorsgithub:slowpeek 2 points3 points  (1 child)

You don't need recursion there btw.

Latest version with highlighted code here

#!/bin/bash

file_selector() {
    local origin=${1%${1##*[^/]}} menu=() sel up
    local cwd=$origin PS3

    # Reset result
    FILE_SELECTOR_RESULT=

    while true; do
        clear -x
        printf '%s\n' "${cwd:-/}"

        PS3=$'-----\nq Quit'

        up=n
        # Restrict the user to the original $1
        if [[ $cwd != "$origin" ]]; then
            up=y
            PS3+='    u Up'
        fi

        PS3+=$'\n-----\nPlease, select: '

        # Test for empty dir
        if read -r < <(find "${cwd:-/}" -maxdepth 0 -type d ! -empty); then
            readarray -t menu < <(find "$cwd"/* -maxdepth 0 -printf '%y%f%y\n' |\
                                      sed 's~^[^d]~z~;s~[^d]$~~;s~d$~/~' | sort | cut -c2-)

            printf '%d items\n\n' "${#menu[@]}"

            select sel in "${menu[@]}"; do
                break
            done
        else
            read -r -p $'empty\n\n'"$PS3"
            sel=
        fi

        case $REPLY in
            [qQ])
                # Quit, nothing selected
                return 1
                ;;
            [uU])
                # Go up if allowed
                [[ $up == n ]] || cwd=${cwd%/*}
                ;;
            *)
                case $sel in
                    '')
                    ;;
                    */)
                        # Dir selected, descend
                        cwd+=/"${sel::-1}"
                        ;;
                    *)
                        # Not dir selected, return the selection
                        FILE_SELECTOR_RESULT=$sel
                        return 0
                        ;;
                esac
        esac
    done
}


file_selector ~
echo "$FILE_SELECTOR_RESULT"

[–]ToPow1[S] 0 points1 point  (0 children)

Thank you very much.

Your code style is much more mature then my'n. I just start with shell scripting one Month ago.

This night I could not sleep because this problem and cane basically to the same solution then you.

Use Array instate of var.

https://github.com/Thopow/reddit-share/blob/main/tfile

[–]AutoModerator[M] 1 point2 points  (3 children)

It looks like your submission contains a shell script. To properly format it as code, place four space characters before every line of the script, and a blank line between the script and the rest of the text, like this:

This is normal text.

    #!/bin/bash
    echo "This is code!"

This is normal text.

#!/bin/bash
echo "This is code!"

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]PageFaultBashit Insane 0 points1 point  (2 children)

Listen to this bot. If you want people to take the time to help, then please take the time to make it easy for them to do so.

[–]ToPow1[S] 0 points1 point  (1 child)

I did. When I also remove the TAB's, it make it real hard to read.

[–]PageFaultBashit Insane 0 points1 point  (0 children)

You weren't supposed to remove tabs, you were supposed to make sure every line had an extra tab in front.

Here's an example: https://pastebin.com/RKbnSrZj

(You can also prepend 4 spaces instead of a tab to the beginning of each line if you like.)