Question

I'm a newbie to Linux operating system I need to do the following:-

I have multiple projects under "~/myprojects"

Think of like >ls ~/myprojects project1 project2i newproject project_possible....

All my projects have a fixed structure see as below:-

ls ~/myprojects/

src lib inc common test_scripts

(all these are directories having some files in them

For navigating the current() I want to do something like this in my bashrc file.

assign curr_project = "$1" alias psrc='cd ~/myprojects/curr_project/src/' alias plib='cd ~/myprojects/curr_project/lib/'

Thanks in advance

Was it helpful?

Solution

You can use an environment variable to specify the current project and use the variable in your aliases:

current() {
    export CURR_PROJECT=$1
}

alias psrc='cd ~/myprojects/$CURR_PROJECT/src/'
alias plib='cd ~/myprojects/$CURR_PROJECT/lib/'

First you set the CURR_PROJECT by using

$ current project1

Then you call your alias to change directories:

$ psrc

Hope that helps.

OTHER TIPS

I use something similar for my work environment - many projects with a common directory structures. I also use a selector to allow me choose projects quickly without typing their name. You may find it useful.

E.g.

current()
{
    export PROJECT_ROOT=~/myprojects

    # If you pass a project name, use it, otherwise print a list
    # for the user to select
    if [ -n "$1" ]; then
        export CURRENT_PROJECT=$1
    else
        # Find subdirectories in PROJECT_ROOT
        SUBDIRS=`find $PROJECT_ROOT -mindepth 1 -maxdepth 1 -type d -printf "%f "`
        if [ -n "$SUBDIRS" ]; then
           PS3="Select project: "
           select d in $SUBDIRS; do
               if [[ -n $d ]]; then
                   export CURRENT_PROJECT=$d
                   break
               else
                   echo "Bad choice"
                   return
               fi
           done
        else
          echo "No projects found"
          return
        fi
    fi

    # Now we have the CURRENT_PROJECT name, set up the aliases
    alias psrc='cd $PROJECT_ROOT/$CURRENT_PROJECT/src/'
    alias plib='cd $PROJECT_ROOT/$CURRENT_PROJECT/lib/'
}

Then if you type "current", you will get a choice:

~$ current
1) proj1
2) proj2
3) proj3
Select project: 

This is a real time-saver for me - maybe it will be for you too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top