Prompt user to select a directory with a bash script and read result

前端 未结 3 1510
情深已故
情深已故 2020-12-28 18:41

I want to be read a dir with a bash script (actually I am using zsh).

I want to list the current folders in the same dir and display it to the user asking them to en

3条回答
  •  攒了一身酷
    2020-12-28 19:10

    Saw this snippet @ some stack link dunno exactly..though might be useful

     #! /bin/bash
    
    # customize with your own.
    options=(backup_*/)
    
    menu() {
        clear
        echo "Avaliable options:"
        for i in ${!options[@]}; do 
            printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
        done
        [[ "$msg" ]] && echo "$msg"; :
    }
    
    prompt="Check an option (again to uncheck, ENTER when done): "
    while menu && read -rp "$prompt" num && [[ "$num" ]]; do
        [[ "$num" != *[![:digit:]]* ]] && (( num > 0 && num <= ${#options[@]} )) || {
            msg="Invalid option: $num"; continue
        }
        ((num--)); msg="${options[num]} was ${choices[num]:+un}checked"
        [[ "${choices[num]}" ]] && choices[num]="" || choices[num]="[+]"
    done
    
    printf "You selected"; msg=" nothing"
    for i in ${!options[@]}; do 
        [[ "${choices[i]}" ]] && { printf " %s" "${options[i]}"; msg=""; }
    done
    echo "$msg"
    

提交回复
热议问题