0

I'm trying to write a couple of shortcut aliases to quickly navigate to the httpdocs folders of the various domains and subdomains on my server. basically so I can type something like:

$ pwd
/
$ # Go to the 'site.com' domain:
$ goto site.com
$ pwd
/var/www/vhosts/site.com/httpdocs
$ # Go to the 'subsite' subdomain:
$ gotosub site.com subsite
$ pwd
/var/www/vhosts/site.com/subdomains/subsite/httpdocs

I'm pretty green when it comes to Linux commands and I've tried to research this but failed at putting together search terms that have helped.

I've tried using printf and backticks like this:

$ alias goto='`printf "cd /var/www/vhosts/%s/httpdocs"`'
$ alias gotosub='`printf "cd /var/www/vhosts/%s/subdomains/%s/httpdocs"`'

but that doesn't replace in the arguments. If I remove the backticks:

$ alias goto='printf "cd /var/www/vhosts/%s/httpdocs"'
$ alias gotosub='printf "cd /var/www/vhosts/%s/subdomains/%s/httpdocs"'

Then it works but I need to run them like so:

$ `goto site.com`
$ `gotosub site.com subsite`

...Which seems ugly and inefficient. What's the best approach to achieve this?

P-Q
  • 1
  • I see from the answers on that question that 'For all purposes where the arguments should be used somewhere in the middle, one needs to use a function instead of alias.', so I'm wondering if it's possible have a printf command at the end which is somehow sent backwards into either a cd or an eval command.

    I've tried using the '<' operator to do this but haven't been able to make it work.

    – P-Q Jan 23 '14 at 11:17

1 Answers1

1

Try using a function instead of or as well as an alias.

Define the function add to you .bashrc

goto()
{
if [[ $# = 2 ]]
then
  cd /var/www/vhosts/$1/subdomains/$2
else
  cd /var/www/vhosts/$1
fi
}

Use the function

goto example.com
goto example.com blog
X Tian
  • 10,463
  • Fantastic thanks, that works perfectly, better even since it's a single combined command. I wan't aware that I could put functions in .bashrc but I am now. Unfortunately I'm apparently not reputable enough to upvote, but I'll leave myself a note to come back and upvote as and when. – P-Q Jan 23 '14 at 14:49