FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo User

 
 
LinkBack Thread Tools
 
Old 11-12-2010, 04:57 PM
Philip Webb
 
Default bash scripting tip

There are quick'n'easy commands to goto the previous dir
-- 'cd -' , which cb aliased as 'p' --
& goto the next-higher dir -- 'cd ..' , which cb aliased as 's' -- ,
but is there a way to set up a qne command to goto a parallel dir,
eg if you're in ~/tmp goto ~/hold ( 2 of my commonly-used dirs) ?

It needs to be a Bash function, so in ~/.bashrc
I tried 'function cd2() { cd .. ; cd $1 ; }',
so that 'cd2 hold' would take me where I wanted to go,
but it simply dropped me in ~ , the 2nd half being ignored.

It cb done with a shell var,
ie 'function cd2() { NEWDIR=$1 ; cd .. ; cd $NEWDIR ; NEWDIR= ; }',
which works but is a bit lengthy & could clash with an existing shell var.

The elegant way is 'function cd2() { cd .. ; cd $"$1" ; }' ;
the " ... " are essential: it fails without them or with ( ... ) instead.

HTH a few others.

--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
 
Old 11-12-2010, 05:08 PM
Bill Longman
 
Default bash scripting tip

On 11/12/2010 09:57 AM, Philip Webb wrote:
> There are quick'n'easy commands to goto the previous dir
> -- 'cd -' , which cb aliased as 'p' --
> & goto the next-higher dir -- 'cd ..' , which cb aliased as 's' -- ,
> but is there a way to set up a qne command to goto a parallel dir,
> eg if you're in ~/tmp goto ~/hold ( 2 of my commonly-used dirs) ?
>
> It needs to be a Bash function, so in ~/.bashrc
> I tried 'function cd2() { cd .. ; cd $1 ; }',
> so that 'cd2 hold' would take me where I wanted to go,
> but it simply dropped me in ~ , the 2nd half being ignored.
>
> It cb done with a shell var,
> ie 'function cd2() { NEWDIR=$1 ; cd .. ; cd $NEWDIR ; NEWDIR= ; }',
> which works but is a bit lengthy & could clash with an existing shell var.
>
> The elegant way is 'function cd2() { cd .. ; cd $"$1" ; }' ;
> the " ... " are essential: it fails without them or with ( ... ) instead.
>
> HTH a few others.
>

cd ${PWD/old/new}

works when you're in /some/old/tree/directory and you want to go to
/some/new/tree/directory
 
Old 11-12-2010, 05:31 PM
Philip Webb
 
Default bash scripting tip

101112 Bill Longman wrote:
> On 11/12/2010 09:57 AM, Philip Webb wrote:
>> but is there a way to set up a command to goto a parallel dir,
>> eg if you're in ~/tmp goto ~/hold ( 2 of my commonly-used dirs) ?
>> The elegant way is 'function cd2() { cd .. ; cd $"$1" ; }'.
> cd ${PWD/old/new}
> works when you're in /some/old/tree/directory
> and you want to go to /some/new/tree/directory

It works, but wouldn't be as useful for what I want to do,
as you'ld have to enter both 'old' & 'new' after the function command.
ie instead of 'cd2 hold', you'ld have to write 'cd2 tmp hold':
it doesn't work if you try 'cd ${PWD/./hold}', which leaves you in 'tmp'.

For generally jumping round the dir tree, I recommend Cdargs.

--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
 
Old 11-12-2010, 05:36 PM
Hilco Wijbenga
 
Default bash scripting tip

On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
> It needs to be a Bash function, so in *~/.bashrc
> I tried 'function cd2() { cd .. ; cd $1 ; }',

Doesn't

function cd2() { cd ../$1 }

work? (I haven't tried it.)
 
Old 11-12-2010, 06:17 PM
Hilco Wijbenga
 
Default bash scripting tip

On 12 November 2010 10:36, Hilco Wijbenga <hilco.wijbenga@gmail.com> wrote:
> On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
>> It needs to be a Bash function, so in *~/.bashrc
>> I tried 'function cd2() { cd .. ; cd $1 ; }',
>
> Doesn't
>
> function cd2() { cd ../$1 }
>
> work? (I haven't tried it.)

So yes, this:

function cd2() { cd ../$1; }

works.
 
Old 11-12-2010, 06:36 PM
BRM
 
Default bash scripting tip

----- Original Message ----

> From: Hilco Wijbenga <hilco.wijbenga@gmail.com>
> On 12 November 2010 10:36, Hilco Wijbenga <hilco.wijbenga@gmail.com> wrote:
> > On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
> >> It needs to be a Bash function, so in ~/.bashrc
> >> I tried 'function cd2() { cd .. ; cd $1 ; }',
> >
> > Doesn't
> >
> > function cd2() { cd ../$1 }
> >
> > work? (I haven't tried it.)
>
> So yes, this:
>
> function cd2() { cd ../$1; }
>
> works.

Something I have found useful is the pushd/popd functions in Bash.
Of course, to use them the way you want to you'd have to use two step procedure:

1. Init to the directory you want:

function cdInit()
{
pushd ${1} > /dev/null
pushd ${2} > /dev/null
}

2. cd away:

function cd2()
{
popd > /dev/null
pushd ${1} > /dev/null
}

3. close out when you're done:

function cdFini()
{
popd
}

You could probably modify the above do pull out the initial directory from a
single string by - e.g. turn /my/path/parent/child into /my/path/parent - as
well.
You could also process the DIRSTACK variable (or use the 'dirs' command) to see
if the parent directory is already on the stack too.

Note: I have the redirs in there because pushd/popd by default dumps the
DIRSTACK as its output.

$0.02

Ben
 
Old 11-12-2010, 07:36 PM
Philip Webb
 
Default bash scripting tip

101112 Hilco Wijbenga wrote:
> On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
>> I tried 'function cd2() { cd .. ; cd $1 ; }',
> Doesn't 'function cd2() { cd ../$1 ; }' work ? -- Yes

Yes, you're correct (slightly red face) !
I'm not sure why I didn't try that variation originally.

--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
 

Thread Tools




All times are GMT. The time now is 07:45 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org