Contents
How to capture a MySQL result set in a shell?
(Any newlines will be embedded there, but it’ll still be a single string.) $ {#linesIN [@]} still works since in Bash/ksh single-element arrays and scalar variables act the same. Another way to do it would be to pipe the output of the command to a while loop. Note you want to include the -N or the results include the column name.
How to execute a mysql command from a shell script?
MySQL uses them as the delimiters for identifiers but the shell, which gets the string first, uses them as executable command delimiters. Miss the escape on a single backtick of the MySQL commands, and the whole thing explodes with errors. The whole issue can be solved by using a quoted LimitString for the HEREDOC:
How to capture the output of a script in Bash?
A method I found to capture all output from any session is to start a new bash session and tee to a log file. its really useful for tracking more then just a script. ./myscript.sh | tee ./myscript.log #this will log only the output of the script. You can always invoke script inside a script to log everything.
How to send mysql query result to file?
Then simply output the result of that variable in a file. Solution 2: use tee as system command to send the result of the commands into a file, but this needs to be done from the crontab, like this: The correct way to handle error-messages is to through stderr. Use 2>&1 to catch the error.
How to print MySQL results in shell script?
Another way to do it would be to pipe the output of the command to a while loop. Note you want to include the -N or the results include the column name. And if you just want a count, you’d do a select count (columnName) and print out the results.
What does MyVariable do for MySQL Query output?
Note that myvariable will contain everything that MySQL outputs on standard out (usually everything but error messages), including any and all column headers, ASCII-art frames and so on, which may or may not be what you want. As has been noted, there appears to be a -e parameter to MySQL, I’d go for that one, definitely. A more direct way would be:
Can you read mysql query output into a shell array?
It’s certainly doable, but if you do not understand pipes and echo (very basic shell stuff), it will not be an easy task for you to do, so if at all possible I’d use a better suited language. To read the data line-by-line into a Bash array you can do this: