Jump to content

Bash script newlines and mysql command oddity


Recommended Posts

Hi all!

 

So I'm finally getting around to doing my very own workflow programming and I've run into some weirdness creating a Workflow that Runs a Bash Script in Alfred v2. I've tested this bash script on the command line and it works a treat. In fact, most of the workflow works in Alfred but a couple of things are funky:

 

  • When using \n inside my echo commands it literally prints \n instead of using a new line. I'd like to format my virtual hosts files as I have them now. It's even more critical for /etc/hosts where that needs to on a new line.
  • When running the mysql logic - It doesn't create the database. It will, however, start MySQL which is something. Note I have tried to both make the commands sequential and used the && distinction. Neither works.

The long and short of this is that basically I am creating a new local site on my stack. I create a directory, a virtual host entry, an /etc/hosts entry, and a MySQL database. Simple enough.

 

I am sure this is due to my being very new to Alfred workflows. Probably something silly. Any help you can offer would be appreciated greatly.

 

Interpreter is /bin/bash - All of the escapes are checked in the workflow window (Backslashes, Brackets, etc.) and the whole script looks like this...
 
site="{query}"

# Create a new directory
mkdir ~/Sites/$site

# Create a new entry in the Apache virtual hosts file.
echo "\n<VirtualHost *:80>" >> ~/Sites/httpd-vhosts.conf
echo "  ServerName $site.localhost" >> ~/Sites/httpd-vhosts.conf
echo "  DocumentRoot \"/Users/ksilanskas/Sites/$site\"" >> ~/Sites/httpd-vhosts.conf
echo "  ErrorLog \"/Users/ksilanskas/Sites/Logs/$site-errors.log\"" >> ~/Sites/httpd-vhosts.conf
echo "</VirtualHost>" >> ~/Sites/httpd-vhosts.conf

# Ensure MySQL is running and then create the database.
mysql.server start >> /dev/null && echo "CREATE DATABASE $site" | mysql -u root -proot >> /dev/null

# Add an entry to the /etc/hosts file and restart Apache
osascript -e "do shell script \"sudo echo '127.0.0.1 $site.localhost' >> /etc/hosts && sudo apachectl restart >/dev/null\" with administrator privileges"

Thanks again! 

 

P.S. To prove I did a little searching... I did find this but it didn't look like a complete topic... http://www.alfredforum.com/topic/879-create-mysql-database-from-script-action/?hl=mysql - I'd prefer to just stick with using command line on this one if possible.

 

Link to comment

 

Hi all!

 

So I'm finally getting around to doing my very own workflow programming and I've run into some weirdness creating a Workflow that Runs a Bash Script in Alfred v2. I've tested this bash script on the command line and it works a treat. In fact, most of the workflow works in Alfred but a couple of things are funky:

 

  • When using \n inside my echo commands it literally prints \n instead of using a new line. I'd like to format my virtual hosts files as I have them now. It's even more critical for /etc/hosts where that needs to on a new line.
  • When running the mysql logic - It doesn't create the database. It will, however, start MySQL which is something. Note I have tried to both make the commands sequential and used the && distinction. Neither works.

The long and short of this is that basically I am creating a new local site on my stack. I create a directory, a virtual host entry, an /etc/hosts entry, and a MySQL database. Simple enough.

 

I am sure this is due to my being very new to Alfred workflows. Probably something silly. Any help you can offer would be appreciated greatly.

 

Interpreter is /bin/bash - All of the escapes are checked in the workflow window (Backslashes, Brackets, etc.) and the whole script looks like this...
 
site="{query}"

# Create a new directory
mkdir ~/Sites/$site

# Create a new entry in the Apache virtual hosts file.
echo "\n<VirtualHost *:80>" >> ~/Sites/httpd-vhosts.conf
echo "  ServerName $site.localhost" >> ~/Sites/httpd-vhosts.conf
echo "  DocumentRoot \"/Users/ksilanskas/Sites/$site\"" >> ~/Sites/httpd-vhosts.conf
echo "  ErrorLog \"/Users/ksilanskas/Sites/Logs/$site-errors.log\"" >> ~/Sites/httpd-vhosts.conf
echo "</VirtualHost>" >> ~/Sites/httpd-vhosts.conf

# Ensure MySQL is running and then create the database.
mysql.server start >> /dev/null && echo "CREATE DATABASE $site" | mysql -u root -proot >> /dev/null

# Add an entry to the /etc/hosts file and restart Apache
osascript -e "do shell script \"sudo echo '127.0.0.1 $site.localhost' >> /etc/hosts && sudo apachectl restart >/dev/null\" with administrator privileges"

Thanks again! 

 

P.S. To prove I did a little searching... I did find this but it didn't look like a complete topic... http://www.alfredforum.com/topic/879-create-mysql-database-from-script-action/?hl=mysql - I'd prefer to just stick with using command line on this one if possible.

 

 

Haven't messed with the mysql part yet but the newlines.... try replacing echo with printf and it should work but you'll need a \n at the end of each one to have all of those commands on a new line, otherwise, it prints them all on one line

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...