0

Please consider the org-babel block below, where I dump a MySQL table:

#+begin_src sh :results value file :file dump.sql
mysqldump -u root -h 0.0.0.0 -P 49321 -proot db_name tbl_name
#+end_src

Running this command in my terminal gives me the output I expect: the raw SQL that I can redirect into a file. However, when I run it in that babel block, the file "dump.sql" contains only the character "2".

Changing the collection option to "output" makes emacs complain that it "Couldn't execute 'SELECT COLUMN_NAME,...". I'm sure there is some combination of results options that would give me what I want, but they elude me at the moment.

daraul
  • 13
  • 3

1 Answers1

1

It seems I should have done a few more minutes of research. I found my solution with the help of an answer to a similar question on these forums: I have to take advantage of the ":prologue", and ":epilogue" parameters to properly handle the command's output, and change the collection option to "output":

#+begin_src sh :results output file raw :file dump.sql :prologue "2>/dev/null" :epilogue ":"
mysqldump -u root -h 0.0.0.0 -P 49321 -proot cf_dev sforum_contacts
#+end_src

I had no idea these options existed, and don't understand what they do exactly -- the prologue option seems akin to appending it's value to each command in your block, and I have no idea what the epilogue option does.

daraul
  • 13
  • 3
  • You should [read the manual](https://orgmode.org/manual/Working-with-Source-Code.html) Instead of just cargo–culting from someone else’s question. In particular those :prologue and :epilogue options are probably not doing anything useful for you! They allow you to put stuff (in this case shell commands, since this is an “sh” source block) before and after the content of the source block before actually sending it to a shell. So yours is like running the script `2>/dev/null; mysqldump …; :`. What really fixed it for you is changing “:results value” to “:results output”. – db48x Jan 21 '23 at 04:05
  • I said in my question that only changing the collection option to output caused emacs to complain that it couldn't execute 'SELECT COLUMN_NAME,...". The prologue option caused the block to do what I wanted, but I do confess that I probably don't need the epilogue option. – daraul Jan 21 '23 at 11:33
  • `:prologue "2>/dev/null"` couldn’t have fixed it because all that does is redirect the stderr _of an empty command_ to /dev/null. Maybe it was really `:prologue "exec 2>/dev/null"`? – db48x Jan 21 '23 at 17:56