[gentoo-user] bash scripting tip

2010-11-12 Thread Philip Webb
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




Re: [gentoo-user] bash scripting tip

2010-11-12 Thread Bill Longman
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





Re: [gentoo-user] bash scripting tip

2010-11-12 Thread Philip Webb
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




Re: [gentoo-user] bash scripting tip

2010-11-12 Thread Hilco Wijbenga
On 12 November 2010 09:57, Philip Webb purs...@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.)



Re: [gentoo-user] bash scripting tip

2010-11-12 Thread Hilco Wijbenga
On 12 November 2010 10:36, Hilco Wijbenga hilco.wijbe...@gmail.com wrote:
 On 12 November 2010 09:57, Philip Webb purs...@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.



Re: [gentoo-user] bash scripting tip

2010-11-12 Thread BRM
- Original Message 

 From: Hilco Wijbenga hilco.wijbe...@gmail.com
 On 12 November 2010 10:36, Hilco Wijbenga hilco.wijbe...@gmail.com  wrote:
  On 12 November 2010 09:57, Philip Webb purs...@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




Re: [gentoo-user] bash scripting tip

2010-11-12 Thread Philip Webb
101112 Hilco Wijbenga wrote:
 On 12 November 2010 09:57, Philip Webb purs...@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