I use a Mac but these ideas can be extrapolated for other systems.
Part One: Better drush uli
The problem:
Many of us use the command drush uli
several times a day to log in to various sites on our local machines.
In order for drush uli
to work, you need to cd
to the proper directory, and/or add the --uri
flag to specify which site you're trying to log in to. If you develop using different AMP stacks or are using SSL you might have to specify the port. These details may have been long forgotten if it has been a while since you worked on that site.
$ cd path/to/mysite $ drush uli --uri=http://mysite.local:8083
Thousands of milliseconds are wasted thinking and typing in this arduous process. A bash script can save you. Behold the elegance that awaits:
$ uli mysite
The script:
uli.sh
#!/usr/bin/env bash if [ -z "$1" ] # If no argument is given. then # "then" is a required part of the if/else statement. # Print some help text. echo "Which site?" echo "Script will use MAMP port by default. Use 'add' for Acquia dev desktop" echo "uli [site] [add (optional)]" else # There is an argument. if [[ "$2" == "add" ]] #if the second argument is 'add' then cd $HOME/Sites/$1/sites/default drush --uri=http://$1.local:8083/ uli else cd $HOME/Sites/$1/sites/default drush --uri=http://$1.local:8888/ uli fi # "fi" ends the if statement. fi
Script Notes:
#!/usr/bin/env bash
- the Sha-bangif else then fi
- These keywords form the structure of the if/then statement.$1 $2
- These are arguments submitted with the command. If I type uli mysite
, mysite
is $1
, if I type uli mysite add
, then add
is $2
. The arguments are incremental numbers unless you specifically give the variable a name. More on that in later posts.$HOME
- On a Mac this is usually the /Users/myname/ folder. Depending on your setup, you may need to change this path./sites/default
- In Drupal 7 if you add $base_path = whatever.local to your settings.php then you can run drush commands from the site root instead.Where does the script go?
$ chmod 700 path/to/file
in the Terminal. More on file permissions here.Secondly, you'll need to add an alias (shortcut)*. I keep my aliases in my .bash_profile file. Some use .bashrc. The difference between the two is explained here.
My script file is called uli.sh so I add the following line to my .bash_profile (Found in the home directory).
alias uli="/path/to/uli.sh"
*You could also add a file called uli
to the /usr/bin folder on a Mac and skip the part about writing an alias. Personally I like to keep all my custom scripts in a folder inside my home directory.
What if the site is a multisite?
Why don't you just create a local alias file?
You know you could set MAMP to use port 80, right?
Sure could. I like seeing the port number in the url. It reminds me that I'm on my local and gives a clue which stack I'm using.