1

Need to write a script, that choose one subdirectory in directory to use it.

I see it in this way.

  1. This is /SampleDir/ and it has /SubDir1/, /SubDir2/ and /SubDir3/

  2. User enter the directory location, program search it and give to user list of SubDirectories in this Dir, user choose with exactly subdir he want to work just click the number of list:

  3. Code

echo "Сhoose Directory:\n"
read SOMEDIR
//  #check nubmber of subdirs
echo "In directory you have multiple subdir's, which one do you want to use"
// #choose subdir
// #Any operation, like Remove SubDir
  1. And same in Terminal

Choose Directory:

input: /root/Documents/SampleDir/

In directory you have multiple subdir's, which one do you want to use:

1)SubDir1

2)SubDir2

3)SubDir3

Input: 1

Thanks a lot

Den2067
  • 11

2 Answers2

0

I wrote this script to select long sub-directory names that are time consuming to type.

Usage

When you call cdd and there is more than one sub-directory you need to pick one:

cdd.png

In this screen if you click OK button it is equivalent to:

cd Seven\ Mary\ Three

or:

cd "Seven Mary Three"

When you call cdd and there is only one sub-directory it automatically changes to it:

rick@alien:~/Music/Seven Mary Three$ cdd
rick@alien:~/Music/Seven Mary Three/American Standard$ 

When you call cdd and there are no sub-directories an error is displayed:

rick@alien:~/Music/Seven Mary Three/American Standard$ cdd
No subdirectories
rick@alien:~/Music/Seven Mary Three/American Standard$ 
0

You can place the directory list in an array

mapfile -d '' -t dirs < <(printf '%s\0' */)

and then use the bash select construct:

select d in "${dirs[@]}"; do echo "You chose: $d"; break; done
1) Desktop/           3) dir/               5) opt/               7) somedir/           9) subdir/
2) Documents/         4) openssh-8.2p1/     6) qgis_sample_data/  8) src/
#? 5
You chose: opt/

Replace the echo with whatever you actually want to do with the selected directory.

steeldriver
  • 136,215
  • 21
  • 243
  • 336