Liduo
Liduo

Reputation: 45

bash shell code confusion

The following code recursively find the sub directory of arg1 (pwd by default),labeling each directory or file with a number. Then prompt user to enter a number, and cd that directory label with that number(if it is a directory).

But I do not understand where that number come from.... and how I can control the depth of subdirectory it reaches...

usage source gd.sh gd

#!/bin/bash

function gd ()
{
local dirname dirs dir

if [ $# -gt 0 ]
then
dirname=$1
else
dirname=$(pwd)
fi

dirs=$(find $dirname -type d)

PS3=`echo -e "\nPlease Select Directory Number: "`

select dir in $dirs
do
if [ $dir ]
then
    cd $dir
    break
else
    echo 'Invalid Selection!'
fi
done

Thanks for help :)

Upvotes: 2

Views: 67

Answers (1)

Birei
Birei

Reputation: 36262

The number comes from the select ... in ... instruction. It adds a number for each element of the list. Look at the man page of bash.

For your second question, use the option -maxdepth of find:

dirs=$(find $dirname -maxdepth 2 -type d)

Upvotes: 1

Related Questions