Tuesday, March 21, 2017

Pipe inside a bash script


Today I got stuck trying to use pipe inside a command variable in a bash script:

$CMD="echo foo | cat"
EXEC=`$CMD`                 # expecting foo
echo $EXEC                  # got foo | cat

I could solve that if I run the command directly, without variables:

EXEC=`echo foo | cat`       # expecting foo
echo $EXEC                  # got foo

But that was not an option, because $CMD was complex and carefully mounted before that snippet.

Googling around I found that the solution is to use eval (although I used it slightly different from the source):

$CMD="echo foo | cat"
EXEC=`eval $CMD`            # expecting foo
echo $EXEC                  # got foo

The reason is that eval can expand the pipe command inside a variable, while backticks can't.


Wednesday, August 31, 2016

How to install mysql2 gem on Ubuntu

If you are having trouble to install the mysql2 gem, try the following:

# apt-get install build-essential ruby-dev libmysqlclient-dev

Sunday, July 24, 2016

Slides: Introduction to Ruby

I'm here today to share a presentation that I've made to a group of Software Development students.

Click here to see the presentation.

I'm sorry for a piece of code that was not translated (variable names in portuguese), but I think that you can get the idea (comparing how PHP is spelling and how Ruby is expressive).

Monday, April 21, 2014

Installing Ruby 2.1.1 in Ubuntu 14.04

The apt-get way don't install the latest stable Ruby (2.1.1), even on Ubuntu 14.04. Instead, I got 1.9.3 that way. RVM can be easy, but to work with Apache I'd need a system-wide installation independent of the RVM environment. So I've decided to install Ruby from source. I had problems around readline, but after seeing this hint, the solution that worked for me is:

./configure --with-readline-dir=/usr/lib/x86_64-linux-gnu/libreadline.so
make install

on the directory of the unpacked source of Ruby 2.1.1, as root. I suspect there is a more elegant solution out there, but this is enough for me at this time.

Friday, March 7, 2014


If you're having troubles with LOAD DATA INFILE ("Errcode 13"):
  • copy the CSV file to the DB folder (/var/lib/mysql/DATABASE), and
  • use the filename only, not the full path.

If you're having troubles with LOAD DATA LOCAL INFILE ("The used command is not allowed with this MySQL version"):
  • temporary solution: mysql -uUSER -p --local-infile DATABASE
  • permanent solution: sudo nano /etc/mysql/my.cnf and insert local-infile under [mysql]; save the file, close the editor and type sudo service mysql restart.

Note: LOAD DATA LOCAL INFILE uses IGNORE. If you want to know why some records were skipped, you must use LOAD DATA INFILE (without LOCAL). (Reference)

Sunday, September 29, 2013

Best way to install MongoDB in Ubuntu

Refer to http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/.

That's the best way.

If you try to install only thru apt-get, you can get a deprecated version.

If you try by downloading the binaries, it won't install useful things like user and service.

Tuesday, September 24, 2013

How to change the default mode for php files in Kate Editor

Whenever I open a PHP file within the excellent Kate Editor (which I prefer due to its multi-document interface), the default mode for editing the file is PHP (HTML) instead of PHP/PHP. I use much more the indentation of the last one. I always can change the mode thru Tools -> Mode -> Scripts -> PHP/PHP, but it's a pain to do it every time I open a PHP file.

But you can set the default mode to PHP/PHP by changing its priority compared to PHP (HTML)'s.

That's how I did it:

  • Go to Settings -> Configure Kate -> Open/Save -> Modes & Filetypes;
  • Select Scripts/PHP (HTML) Filetype;
  • Change Priority to 4;
  • Select the File Extensions field and copy it;
  • Select Scripts/PHP/PHP Filetype;
  • Go to the File Extensions and paste the content copied;
  • Hit Apply and Ok;
  • Close and reopen Kate.

I was hard to find that solution (see reference); I hope this can be useful.