0

I am trying to export my sql table data to csv but have these errors. I am really confused with double quotes and single quotes.

#!/bin/bash
mysql -u root -pH0tjava1 -B -e 'SELECT CONCAT("sshpass -p ""Password"" rsync -avvtzh -e ""ssh -o StrictHostKeyChecking=no"" --log-file=""/home/toor/rsync2.log""", login,"@", ftp_addr, " :", camera_name,"/", "/",`\ 'home`',"/",login, "/", camera_name) INTO OUTFILE '/tmp/rsynctest3.csv' lines terminated by '\r\n' from inteliviz.cameras;"


Errors:
/usr/local/bin/rsync.sh: line 8: syntax error near unexpected token `)'
/usr/local/bin/rsync.sh: line 8: `  mysql -u root -pH0tjava1 -B -e "select  CONCAT ("sshpass -p "Pa55word"  rsync   -avvtzh -e  "ssh -o StrictHostKeyChecking=no"  --log-file= "/home/toor/rsync2.log", login,camera_name,ftp_addr)  INTO OUTFILE '/tmp/rsynctest3.csv' lines terminated by '\r\n'  from inteliviz.cameras;"'

/usr/local/bin/rsync.sh: line 8: unexpected EOF while looking for matching ``'
/usr/local/bin/rsync.sh: line 11: syntax error: unexpected end of file
Kiran V
  • 69
  • 5
  • 16

2 Answers2

0

I am really confused with double quotes and single quotes.

You have not only double and single quotes, but also backticks in your statement. The mismatches are:

  • You begin the SQL statement with 'SELECT… and end it with …inteliviz.cameras;". To switch the quotes from ' in the first part to " in the last part, you must insert a closing ' and an opening " betwixt, e. g. …camera_name)'" INTO OUTFILE…
  • The expression `\ 'home`' is messed - the first ` is inside the outer single quotes and thus has no special function, the ' before home closes the single quoted part, the second ` opens an unclosed command substitution, inside of which the final ' is. You have to reconsider what you want to have there and get the quote nesting straight.
Armali
  • 18,255
  • 14
  • 57
  • 171
0

Try with the below script, which works for me:

# Read query into a variable
sql="$(cat query.sql)"

# If sqlplus is not installed, then exit
if ! command -v sqlplus > /dev/null; then 
  echo "SQL*Plus is required..."
  exit 1 
fi 

# Connect to the database, run the query, then disconnect
echo -e "SET PAGESIZE 0\n SET FEEDBACK OFF\n $sql" | \
sqlplus -S -L "$USERNAME/$PASSWORD@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$HOST)(PORT=$PORT))(CONNECT_DATA=(SERVICE_NAME=$DATABASE)))"
Dom
  • 1,687
  • 6
  • 27
  • 37