Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching mysql output in shell script

Tags:

shell

I am new to shell scripting. I am trying to write a shell script in which i can run mysql commands after reading from the file. I have to capture the output of the sql commands in a separate file. Following is the code :

mysql -h ${DB_SERVER} -P ${DB_PORT} -u ${USER} -p${PASS} -f < createUsers.sql >> install.log

Shell script takes a few inputs from the user. When i run the shell script and if i specify a wrong password, the error shows up on the console but not in the log file.

Please suggest how can i redirect the error to the log file as well.

like image 562
Sachin Avatar asked Feb 24 '23 15:02

Sachin


1 Answers

Add 2>&1 on to the end to also capture stderr. Standard out is file descriptor 1 and standard error is 2, so this redirects stderr to wherever stdout 1 is going, i.e. install.log.

mysql [options] < createUsers.sql >> install.log 2>&1

Note: The order of redirections matters. Make sure you put it after the >>, not before it.

like image 118
John Kugelman Avatar answered Mar 05 '23 17:03

John Kugelman