Linux.com

aliases and pushd

Posted by: Anonymous Coward on September 20, 2005 12:32 AM
Why not use some simple BASH aliases to invoke "pushd PATH"? Then, create a simple script to concatenate new paths to and re-source your aliases file.
<tt>#!/bin/bash
#
# addnickpath -- Add a path nick to your aliases file
#
ALIASFILE=~/.bash_aliases

usage() {
    cat << EOUSAGE >&2
Usage: $0 NICK PATH

Add a nick alias for a pathname.  The nick alias will not be added if one
already exists.  The alias will only be ACTIVE if the directory exists, but the
rule will still be added to your aliases file.

Current Alias File: ${ALIASFILE}
EOUSAGE
}

if [ $# -lt 2 ] ; then
    usage
fi

# Check for the alias
if { alias|grep $1 >/dev/null; } ; then
    echo "Alias exists.  Aborted." >&2
    exit 1
fi

if { grep "alias $1=" $ALIASFILE >/dev/null; } ; then
    echo "Alias already added to $ALIASFILE.  Aborted." >&2
    exit 1
fi

# Add nick alias to ~/.bash_aliases
cat << EOADD >> ${ALIASFILE}
# Added with $0
if [ -d "$2" ] ; then
    alias $1="pushd $2"
fi
EOADD

echo "# Re-source your ${ALIASFILE} with:"
echo ". ${ALIASFILE}"

exit 0</tt>
Just type "NICK" at the commandline. Use a naming convention such as "goto-PLACE", so that your bash-completion will give you all the options...
<tt>shell$ goto-[tab] [tab]
goto-web  goto-logs  goto-wherever</tt>
Because you used "pushd", you have pushed the directory on to a stack. Use "popd" to remove the top directory and go to the previous directory on the stack, or use "pushd" to traverse to another directory on the stack. Type "dirs" to print the stack.

To delete path nicks, edit your $ALIASFILE. To see where an alias points, just type "alias ALIASNAME". Enjoy.

#

Return to CLI Magic: CDargs