tags:

views:

35

answers:

2

Hi,

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 enter a number to select the correct folder.

Please select a Folder eg, 1,2,3.
1. Folder Name 1 (this should the acutal name of the folder in the dir
2. Folder 2
3. Folder 3.

I would like to also be able to convert the input eg 1. Back to the actual folder name so I can

cd ./$foldername/

Thanks for you help. Cheers, John.

A: 
#!/bin/sh

dirs=(*/)

read -p "$(
        f=0
        for dirname in "${dirs[@]}" ; do
                echo "$((++f)): $dirname"
        done

        echo -ne 'Please select a directory > '
)" selection

selected_dir="${dirs[$((selection-1))]}"

echo "You selected '$selected_dir'"
amphetamachine
+4  A: 

Unless your formatting requirements are very strict, you can probably just use bash’s select construct.

The following code will present a menu of all the directories in the current directory and then chdir to the selected one:

printf "Please select folder:\n"
select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
cd "$d" && pwd
Chris Johnsen
WONDERFULL. TESTED AND WORKS LIKE A CHARM. THANKS!That worked just the way I wanted it to.Cheers, John.
John Ballinger