1748271 Members
4085 Online
108760 Solutions
New Discussion юеВ

Re: script trick

 
SOLVED
Go to solution
Tore_1
Regular Advisor

script trick

Hi, I would like to make an alias or script called subdir, which works like :

cd $(find . -type d -name $1 | head -1)

where $1 is the argument representing the directory.

How am I able to communicate this result from the script, or is there another way to get the same effect?
4 REPLIES 4
Steve Steel
Honored Contributor

Re: script trick

Hi

A script will not work because it will fork. change directory and then exit back to your local environment without the fork.

Why the script anyway

cd dir is easier.

cd name where name doesnt exist easy error message

A subdir of a directory with the name you want can also comeout . the following is easier in format.

if [ -d $1 ]
then
cd $1
fi


Then use the script as . script dir

to avoid the fork

An alias will not work because you cannot setup a parameter to pass easily.


steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Tore_1
Regular Advisor

Re: script trick

I know this.

subdir should be more than one level down.
Sridhar Bhaskarla
Honored Contributor
Solution

Re: script trick

Hi,

A workaround is to define it as a function in your .profile.

subdir()
{
if [ $# != 1 ]
then
echo "usage $0: subdirectory"
break
else
cd $(find . -type d -name $1 | head -1)
fi
}

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
gary phipps
Frequent Advisor

Re: script trick

Hi Tore,

one way would be to put the following in a script...

alias changedir="cd `find . -type d -name $1|head -1`"

...run the script by typing...

. ./scriptname

...then enter "changedir" to change directory.

Gary.