The script below is loading in a bunch of csv files into a mysql db. I am trying to execute this function within the loop but the mysql table field called return is causing the script to think it should execute the function return.
The ` around return is to escape it for mysql, its a mysql keyword.
for f in *.txt;
do
mysql -uroot -ppassword -e "LOAD DATA INFILE '$f' INTO TABLE info FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (`return`,`id`,`field1`,`field2`);";
done
The -r tests if the file exists and if you have read permission on the file. Bash scripting tutorial - if statement. The meaning of -r depends on what program/command it is given as an argument for. In this case it is for [ , and means to check whether the file named by the next argument is readable.
The return keyword exits a function, script, or script block.
We use exit to exit from a Bash script. In that case, the script exits with the exit status set to the integer argument. If we don't pass an argument to exit, it exits with the exit status set to the exit status of the last command executed before exit.
exit exits the calling shell or shell script with the exit status specified by n . If you omit n , the exit status is that of the last command executed (an end-of-file exits the shell). return exits a function with the return value specified by n . If you omit n , the return status is that of the last command executed.
it's the stupid quoting convention in mysql of using backtick characters. Can you use Single-quotes instead, ie '
, ie 'return'
?
Backticks mean "perform command substitution in this current command in the shell", so it is trying to run the command return
.
If you can't use 'return'
then you can escape ALL of the backticks, like
\`return\`
IHTH
This happen because strings enclosed by ` ` are executed by bash.
Try this:
echo '` date `' # this output the string ` date `
echo "` date `" # this output current time
But you cannot change double quotes with single one, because you need $f
to be sostituded with variable. So escape backtick with \
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With