1

I'm trying to run this command:

mysql --host="$ipAddr" -u "$usr" -p -t < "$baseName.sql" >> log.txt

As you can see I'm trying to run this comand with parameters loaded from file and save the result to the other file. My command executes correct but file "log.txt" stays empty. How to correct that??

  • 2
    What are the contents of $baseName.sql? If log.txt is empty, then your command isn't producing any output. – terdon Mar 28 '19 at 10:39
  • same q as Terdon basically: mysql --host="$ipAddr" -u "$usr" -p -t < "$baseName.sql" provides output? If not ... nothing ends up in log.txt. Also make sure log.txt is at a location you can write to. – Rinzwind Mar 28 '19 at 10:48
  • $baseName.sql contains SQL code. It's variable because i use it for more projects. It makes output when I have some errors in code – Elhatron Mar 28 '19 at 11:02

1 Answers1

0

One possible explanation to what you are seeing is that the command generates output only to stderr and no output to stdout.

You have done something like this:

mysql [...] >> log.txt

which means that you are redirecting stdout to the log.txt file. However, any output to stderr will still appear directly in your terminal, it will not go to the log.txt file. Usually, error messages are output to stderr so if there are some error messages resulting from your mysql command then it is likely that those will be output to stderr rather than stdout.

You could try this instead:

mysql [...] >> log.txt 2>> err_log.txt

(just add "2>> err_log.txt" to the end of your command line)

that means that you are now requesting stdout to be redirected to the log.txt file, as before, but now you are also redirecting stderr to the err_log.txt file.

More about this here: https://stackoverflow.com/questions/7526971/how-to-redirect-both-stdout-and-stderr-to-a-file

Also here: https://stackoverflow.com/questions/876239/how-can-i-redirect-and-append-both-stdout-and-stderr-to-a-file-with-bash

Elias
  • 2,039
  • 1
    It works!! Thanks!! Meanwhile i found this article https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file and thanks to You it's easier to understand :D – Elhatron Mar 31 '19 at 14:31