Unexpected $'\r': command not found after mysql query in shell - mysql

This scipt gives proper result, but also gives mistake:$'\r': command not found in line with query.
#!/bin/bash
keyOrPass=$1
intercom=$2
flat=$3
number=$4
mysql -ulogin -ppass dbname -e "select cli.codeGuestEmail, cli.codePrivateEmail, cliKey.rf_id, cliKey.emailNotification from mbus_clients as cli join mbusClientKeys as cliKey on cliKey.id_client=cli.id WHERE cli.flat=${flat} and cli.domophone=${intercom};";
This is how I run the script:
sh sendEmailNotification.sh key 10001014 11 1

Create a version of your script without Windows line delimiter \r:
tr -d "\r" < sendEmailNotification.sh > sendEmailNotification_fixed.sh

Check and fix line-endings in your script, probably, it was saved with \r\n as line delimiters.
This link may be useful: How to convert DOS/Windows newline (CRLF) to Unix newline (\n) in a Bash script?

Related

export mysql table data to csv using bash script

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
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.
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)))"

shell script issue may be because of quotes

I am working on a shell script and I have to execute a MySQL query from one server to a remote DB Server. I have written this script. Script was working fine until I added
where socialr_host = "http://$NEW/"
I think this is an issue of "" inside ''. Please help me with this, I don't have much knowledge to shell scripting
SERVER_USER="root"
SERVER_HOST="192.168.0.13"
MYSQL_HOST="localhost"
MYSQL_PASS="pass"
MYSQL_USER="root"
CORES_DATABASE="/root/Desktop/cores.db"
CORES_FILESYSTEM="/root/Desktop/cores.disk"
DIFF_FILE="/root/Desktop/diff.txt"
CORES_PATH="/raid/solr/cores"
NEW="db6055.da2"
ssh $SERVER_USER#$SERVER_HOST " mysql -u $MYSQL_USER -p$MYSQL_PASS -e 'select coloumn_name from table_name where socialr_host = "http://$NEW/";' database" > $CORES_DATABASE
You need to escape double quotes within double quotes:
ssh $SERVER_USER#$SERVER_HOST " mysql -u $MYSQL_USER -p$MYSQL_PASS -e 'select coloumn_name from table_name where socialr_host = \"http://$NEW/\";' database" > $CORES_DATABASE
However, there are several other issues with your code, and I would avoid using Bash for something as complicated as this. If you insist on using Bash, please read up on at least quoting and SSH quoting.

Escape $ from sql script running from Linux Shell

I am trying to execute an insert statement from linux shell where one of the columns has '$2a$10$zKjqmgld1gDYB/qkDuAS' in the value. When I see the inserted data the value is truncated and I get only 'aKjqmgld1gDYB/qkDuAS' as any digit followed by dollar is treated by linux as a parameter passed to the script.
This is how I am executing the script
mysql -u user --password=password -e "insert into users(id,name,password) values(1,'Some Name','\$2a\$10\$zKjqmgld1gDYB/qkDuAS')"
I have even tried escaping the $ like \$2a\$10\$zKjqmgld1gDYB/qkDuAS, but still it yields the same truncated data , however when I do echo '\$2a\$10\$zKjqmgld1gDYB/qkDuAS', I get the entire thing back.
Sameer
Single quotes don't nest in shell (do they anywhere else, anyway?). So, your string is effectively outside the quotes. Combine with double quotes and backslashes:
mysql -u user --password=password -e \
"insert into users(id,name,password) values(1,'Some Name','\$2a$10\$zKjqmgld1gDYB/qkDuAS')"
it was already in double quotes (sorry, for writing the wrong query). That didn't work either, however I found a workaround
echo "insert into users(id,name,password) values(1,'Some Name','\$2a\$10\$zKjqmgld1gDYB/qkDuAS')" >> temp.sql
mysql -u user --password=password < temp.sql
This finally worked.

bash script automysql sees greater-than character as a table name

CMD="mysqldump -usomeuser -psomepass db_name \> /../SQL_$(date +'%m-%d-%Y').sql"
The above throws the following exception:
mysqldump: Couldn't find table: ">"
"&1>" also is seen as a table name.
I tried the option -all-database(s) and that doesn't work either.
Thanks!
You must not escape greater-than in the command:
CMD="mysqldump -usomeuser -psomepass db_name > /../SQL_$(date +'%m-%d-%Y').sql"
even then, if you try to execute the command just by invoking it, bash treats the greater-than as a positional parameter. You can execute it with a call to sh or bash:
bash -c "$CMD"

Why does script to run MySQLdump on each line of file gives error "command not found"?

Performing automated database backups for starters and testing commands. I've found for performing an action on each line of a text file via BASH CLI is something to the effect of:
# while read line; do
COMMAND $line
done
I've created a list of the database names file:
# mysql -uroot -e "show databases" > databases
Then tried the following against the file to see if it would work correctly.
# while read line; do
"mysqldump -uroot $line > /dbbackups/$line.sql"
done
Seemingly, this would be working correctly but am met with the following error(s):
[04:58:46] [root#theia database-backup-testing]# cat databases | while read line ; do "mysqldump -uroot $line > $line.sql" ; done
-bash: mysqldump -uroot Database > Database.sql: command not found
-bash: mysqldump -uroot information_schema > information_schema.sql: command not found
-bash: mysqldump -uroot cphulkd > cphulkd.sql: command not found
I am not sure why it is giving command not found, when obviously, the output of the commands seems to be correct. I have also tried using the absolute path of mysqldump (/usr/bin/mysqldump) but it gives the same error(s).
Can anyone fill me in on why this is happening?
EDIT: I found a fix:
The script works if the quotes are removed:
# cat databases |
while read line; do
mysqldump -uroot $line > $line.sql
done
Apparently, the quotes causes it to execute as a string and not a command.
why it is giving command not found
Your quotes are not correct, Try something like this:
while read line ; do mysqldump -uroot "$line" > "$line".sql ; done
Haven't used mysqldump so I cant help with the syntax of the specific command.
Here's a bash script that solves this:
https://github.com/jeevandongre/backup-restore