Monday, February 25, 2013

echoing bash and mysql commands when running in "batch mode"

I was working on doing some migrations on a Linux machine.

Those migrations required me to execute some Unix commands (using a bash script file) and running some SQL statements to make some changes to my MySql DB.

The entire set of commands was to be called from the console after executing a script -t 2>~/mylog.time -a ~/mylog.script command so that the execution output would be saved to a file.

The problem was that the log file did not show the actual commands that where executing, only their output and on the MySql script not even the output was displayed.

So I was looking fro something like the old dos "Echo On" command.

After doing some reading I found the following:
As the first command within my bash script (after the #!/bin/bash) I added the command set -vx.

  •  -v Prints shell input lines as they are read. 
  •  -x Print command traces before executing command. (so that variables names are replaced with their values). 
To disable this echoing set +vx at the end of the bash script.

As previously stated, in my shell script I also wanted to execute some SQL statements on my DB.

So I created a do_sql.sh file that had the following single line:
mysql -u rewardy --password=mypass --database=mydb --default-character-set=utf8.

Redirecting of stdin to this file like ./do_sql.sh < sql_statements.sql got the statements in sql_statements.sql to execute on the DB but I didn't even see the output of the SQL commands (and obviously not the commands themselves either).

To make this work I modified the file do_sql.sh by adding two parameters at the end of the original line: mysql -u rewardy --password=mypass --database=mydb --default-character-set=utf8 -vvv -t


  • -vvv Causes the echoing I wanted
  • -t      forces printing to use the same formatting as used on interactive console sessions
I hope this helps.

No comments:

 
Clicky Web Analytics