Gentoo Archives: gentoo-user

From: BRM <bm_witness@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] bash scripting tip
Date: Fri, 12 Nov 2010 20:04:17
Message-Id: 211204.74339.qm@web51903.mail.re2.yahoo.com
In Reply to: Re: [gentoo-user] bash scripting tip by Hilco Wijbenga
1 ----- Original Message ----
2
3 > From: Hilco Wijbenga <hilco.wijbenga@×××××.com>
4 > On 12 November 2010 10:36, Hilco Wijbenga <hilco.wijbenga@×××××.com> wrote:
5 > > On 12 November 2010 09:57, Philip Webb <purslow@××××××××.net> wrote:
6 > >> It needs to be a Bash function, so in ~/.bashrc
7 > >> I tried 'function cd2() { cd .. ; cd $1 ; }',
8 > >
9 > > Doesn't
10 > >
11 > > function cd2() { cd ../$1 }
12 > >
13 > > work? (I haven't tried it.)
14 >
15 > So yes, this:
16 >
17 > function cd2() { cd ../$1; }
18 >
19 > works.
20
21 Something I have found useful is the pushd/popd functions in Bash.
22 Of course, to use them the way you want to you'd have to use two step procedure:
23
24 1. Init to the directory you want:
25
26 function cdInit()
27 {
28 pushd ${1} > /dev/null
29 pushd ${2} > /dev/null
30 }
31
32 2. cd away:
33
34 function cd2()
35 {
36 popd > /dev/null
37 pushd ${1} > /dev/null
38 }
39
40 3. close out when you're done:
41
42 function cdFini()
43 {
44 popd
45 }
46
47 You could probably modify the above do pull out the initial directory from a
48 single string by - e.g. turn /my/path/parent/child into /my/path/parent - as
49 well.
50 You could also process the DIRSTACK variable (or use the 'dirs' command) to see
51 if the parent directory is already on the stack too.
52
53 Note: I have the redirs in there because pushd/popd by default dumps the
54 DIRSTACK as its output.
55
56 $0.02
57
58 Ben