Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your example, <code>filename</code> is set to the empty string (mind the spaces after the <code>=</code> sign!). You need</p> <pre><code>filename=$(inotifywait --format "%f" -e create /var/www/media2net/torrent) </code></pre> <p>Similarly,</p> <pre><code>date_field=$(date +"%F:%T") </code></pre> <p>and be careful, you have a typo in your <code>mysql</code> command (<code>date_field</code> and note <code>date_feild</code>):</p> <pre><code>mysql --host=localhost --user=root --password=admin Media2net &lt;&lt;EOF insert into video (title, description, url_video, upload_date) values('testing','default_description','$filename', '$date_field'); EOF </code></pre> <p>Now I hope that you're controlling the filenames. Imagine a filename that contains a single quote e.g., <code>hello'howdy</code>. You'll have a problem in your query. Worse, an evil user who puts a file named <code>whatever','something'); evil_mysql_command; whatever</code>, you'll have the evil command performed! One possibility is to sanitize the filename using <code>printf</code> thus:</p> <pre><code>printf -v filename '%q' "$(inotifywait --format "%f" -e create /var/www/media2net/torrent)" </code></pre> <p>This will at least escape the single quotes that could appear in a filename. See Gordon Davisson's comment: the <code>printf</code> trick will not prevent from all the possible attacks, so I really hope you absolutely control the name of the files!</p> <p>All these suggestions yield the following script:</p> <pre><code>#!/bin/bash while true; do printf -v filename '%q' "$(inotifywait --format "%f" -e create /var/www/media2net/torrent)" date_field=$(date +"%F:%T") mysql --host=localhost --user=root --password=admin Media2net &lt;&lt;EOF insert into video (title, description, url_video, upload_date) values('testing','default_description','$filename', '$date_field'); EOF echo "$filename" done </code></pre> <h1>Edit.</h1> <p>To answer your question in the comment:</p> <blockquote> <p>why did the script properly echo <code>$filename</code> to my terminal but not send it properly to MySQL, does that have to do with string starting with a space? or something else completely?</p> </blockquote> <p>That's because when you do something like:</p> <pre><code>whatever= command </code></pre> <p>then the variable <code>whatever</code> is set to the empty string, and the command <code>command</code> is executed (with the <code>whatever</code> variable set as environment variable). E.g.,</p> <pre><code>$ IFS='c' read a b c &lt;&lt;&lt; "AcBcC" $ echo "$a $b $c" A B C $ echo $IFS $ </code></pre> <p>In your script, the variable filename was in fact never globally set. You can check it by doing this:</p> <pre><code>$ filename= "false" $ echo "$filename" $ </code></pre> <p>What happens is that the environment variable <code>filename</code> is set to empty string then the command <code>false</code> (which happens to exist) is launched using that environment variable and we're done.</p> <p>When you do this:</p> <pre><code>filename= inotifywait --format "%f" -e create /var/www/media2net/torrent </code></pre> <p>the variable <code>filename</code> is set to the empty string, and then the command <code>inotifywait ...</code> is executed with <code>filename</code> as an environment variable (but <code>inotifywait</code> doesn't really care about it). <em>And that's what you saw on your terminal!</em> it was the output of this command. Then you probably saw an empty line, that was the output of</p> <pre><code>echo $filename </code></pre> <p>which was equivalent to</p> <pre><code>echo </code></pre> <p>since the variable <code>filename</code> expanded to an empty string.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload