0

I need a simple bash script function for .bashrc file to navigate to my home directory. We use hg for all our work purpose in the directory and contains many streams.

/home/<username>/RelStream1/
/home/<username>/RelStream2/
/home/<username>/RelStream3/
/home/<username>/RelStream4/

For namesake i call this /home//RelStream1/ path as repo home.

Now for work purpose, i need to navigate to sub folders from my repo home path. /home/<username>/RelStream1/pkgdir/appsdir/moduledir/submoduledir/.... After finishing work, i need to come back to home repo directory for compilation. I have added alias like this, but still its painful to remember the path back.

    alias ..="cd .."
    alias ..2="cd ../.."
    alias ..3="cd ../../.."
    alias ..4="cd ../../../.."
    alias ..5="cd ../../../../.."

Please note many repo home directories would be used at any point of time and hence setting aliases as alias rhome='cd /home/<username>/RelStreamX/' will have little use.

I just need a simple bash function to get back to repo home directory (current repo). It has to get the current path and navigate my directory back to home repo directory

    Ex1:
    From: /home/<username>/RelStream1/pkgdir/appsdir/moduledir/submoduledir/
    To: /home/<username>/RelStream1/ 

    Ex2:
    From: /home/<username>/RelStream3/pkgdir/appsdir/moduledir/submoduledir/micromodule/
    To: /home/<username>/RelStream3/ 

    Ex3:
    From: /home/<username>/RelStream4/pkgdir/appsdir/
    To: /home/<username>/RelStream4/ 

I have tried some methods in bash(beginner), but does not give expected results.

code:

    repohome_func()
    {
    cwd=$(pwd)
    echo "CurrPath:${cwd}"
    sep="/"

    //My plan is to search the cur path with "/" char up to 4 times, 
    //cut the front string and then execute that cmd. 

    }alias rhome=repohome_func
UserM
  • 190
  • 2
  • 19

4 Answers4

1

This should work for you:

function repohome() {
    local repodir=${PWD/$HOME\/}
    cd "$HOME/${repodir%%/*}"
}
pgl
  • 7,551
  • 2
  • 23
  • 31
0

This should make:

alias rhome="cd $(pwd | sed 's~/[^/]*~~4g')"

It gets the current path with pwd and replaces it to what is needed. As this is within $() then we cd there.

You need to remove all current path but the first three blocks. With this sed, we do that.

See an example with a file:

$ cat a
/home/myuser/myrepo1/asld/as/asdf/asdf/asdf
/home/myuser/myrepo2/asdf
/home/myuser/myrepo3/asld/as/asdf/f/asdf
/home/myuser/myrepo4/asld/as/asdf/
$ sed 's~/[^/]*~~4g' a
/home/myuser/myrepo1
/home/myuser/myrepo2
/home/myuser/myrepo3
/home/myuser/myrepo4

From what was commented below, it seems like that the solution was not working as an alias, but it does as a function. The hypothesis is that there must be some alias in either cd, pwd or sed that makes them work differently.

As aliases are bypassed by functions, using it that way mades it. Otherwise, it could also work by using \ before any command, so the command alias gets bypassed as well.

alias rhome="\cd $(\pwd | \sed 's~/[^/]*~~4g')"

More info:

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • I just want to try a cmd in bash prompt "rhome", then it should get me to current repo home directory. I don't want to be bothered with X. – UserM Sep 25 '14 at 15:33
  • For that, you just need an alias: `alias rhome="cd /your/needed/path"`. Apart from that, `X` is strictly needed, there is no way to indicate a variable behaviour without a variable. – fedorqui Sep 25 '14 at 15:34
  • I will be working on different repo directories at the same time. Hence that option will be applicable for one directory. – UserM Sep 25 '14 at 15:35
  • This solution does not work properly. It goes back to /home// instead of /home//RelStream1/ – UserM Sep 26 '14 at 06:01
  • @userm it can't be possible. This is keeping three blocks in the path. – fedorqui Sep 26 '14 at 07:45
  • I have provided the output obtained alias rhme="cd $(pwd | sed 's~/[^/]*~~4g')" [userm@TEGM001 ~]$ cd codebk/mur/mur2/ [userm@TEGM001 mur2]$ pwd /home/userm/codebk/mur/mur2 [userm@TEGM001 mur2]$ [userm@TEGM001 mur2]$ rhme [userm@TEGM001 ~]$ pwd /home/userm – UserM Sep 26 '14 at 09:19
  • @userm it is not very clear to see code in comments. Could you edit your question adding this? – fedorqui Sep 26 '14 at 09:22
  • I have updated the output at the end of the question for your use – UserM Sep 26 '14 at 09:25
  • I got output of the command as given below echo "$(pwd | sed 's~/[^/]*~~4g')" outputs as /home/mvenugop/codebk May be the last "/" is missed – UserM Sep 26 '14 at 09:36
  • @userm I still don't understand it. `echo "/home/userm/codebk/mur/mur/" | sed 's~/[^/]*~~4g'` to me returns `/home/userm/codebk`. Give a try with `alias rhme="cd $(pwd | awk 'BEGIN{FS=OFS="/"} NF=4'`. – fedorqui Sep 26 '14 at 09:40
  • Was this your full command?? alias rhme2="cd $(pwd | awk 'BEGIN{FS=OFS="/"} NF=4'" I get error as unexpected EOF while looking for matching `"' and syntax error: unexpected end of file – UserM Sep 26 '14 at 10:03
  • @userm ups, yes, it is like this --> `alias rhme="cd $(pwd | awk 'BEGIN{FS=OFS="/"} NF=4')"` – fedorqui Sep 26 '14 at 10:05
  • after you new cmd executing, i still the same result. – UserM Sep 26 '14 at 10:09
  • @userm I don't have an explanation for that and won't debug any more. Just try with `5` instead of `4` in both sed and awk commands. – fedorqui Sep 26 '14 at 10:10
  • Changed to 5 instead of 4, but still the same result. May be there is some problem in my system. Not sure, i will award for the effort spent for this question. – UserM Sep 26 '14 at 10:15
  • @userm I am thinking that your `cd` may have some alias. Type `alias cd` or diretly `alias` and see if it is hiding some problem there. – fedorqui Sep 26 '14 at 10:16
  • One good thing, if i executed your code within bash function, it works as expected. repohome_func() { echo "$(pwd | sed 's~/[^/]*~~4g')" cd $(pwd | sed 's~/[^/]*~~4g') } alias rhome=repohome_func – UserM Sep 26 '14 at 10:26
  • 1
    @userm Good one! Then there must be something in aliases bothering. You can bypass them with \ ([More info here on backslash](http://stackoverflow.com/q/15951772/1983854)): `alias rhme="\cd $(\pwd | \sed 's~/[^/]*~~4g')"`. Also, the name of the function can be used directly. You can call `repohome_func` once you defined this function. – fedorqui Sep 26 '14 at 10:29
  • Dear downvoter: mind to explain the reason of the downvote? I am missing something? – fedorqui Sep 27 '14 at 12:30
0

If you add your home directory to the front of CDPATH, for example,

CDPATH=/home/<username>:.

then any relative path used with cd will be looked up first in your home directory before the current working directory. Then

cd Relstream1

will take you to /home/<username>/Relstream1 before ./Relstream1.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks for your comments, but sill my intention is not to bother about current repo directory where i am working. It should automatically navigate back to current repo directory. – UserM Sep 26 '14 at 06:03
0

You might want to try the builtin commands pushd and popd. If you use pushd <dir> instead of cd <dir>, you can then use popd to return to the previous working directory.

Cole Tierney
  • 9,571
  • 1
  • 27
  • 35