0

How can i connect to my db and execute the query in bash

this is my code so far:

echo "estacion: "$st;
fcha=$year2"-"$month"-"$day;
echo "fecha: "$fcha;

echo $archivoF " ==> " $rutabase"datos/obs/"$st"/"$year2"/"$archivoF;
if [ ! -d $rutabase"datos/obs/"$st"/"$year2 ]; then
  mkdir -p $rutabase"datos/obs/"$st"/"$year2;
fi
mv $archivoF $rutabase"datos/obs/"$st"/"$year2
IFS='.' read -ra tipoO <<< "$archivoF"
tipoOb=`echo "."${tipoO[1]}"."${tipoO[2]}`
query="insert into FILES (name,type,date,station) VALUES($archivoF,$tipoOb,$fcha,$st)"

echo $archivoG " ==> "$rutabase"rinex/nav/"$st"/"$year2"/"$archivoG;
if [ ! -d $rutabase"datos/nav/"$st"/"$year2 ]; then
mkdir -p $rutabase"datos/nav/"$st"/"$year2;
fi
mv $archivoG $rutabase"datos/nav/"$st"/"$year2
IFS='.' read -ra tipoN <<< "$archivoG"
tipoNa=`echo "."${tipoN[1]}`
query="insert into FILES (name,type,date_f,station) VALUES($archivoG,$tipoOb,$fcha,$st)"

any suggestions

5
  • mysql <options> -e "query string" Commented Sep 19, 2016 at 15:49
  • Make sure you quote the values that are strings. Commented Sep 19, 2016 at 15:49
  • Why do you use echo in your assignments? Just tipoOb=.${tipoO[1]} Commented Sep 19, 2016 at 15:51
  • It's also not necessary to keep starting and ending quotes. mv $archiveOF "${rutabase}datos/obs/$st/$year" Commented Sep 19, 2016 at 15:52
  • i'll keep in mind your suggestions, thanks Commented Sep 20, 2016 at 15:06

1 Answer 1

2

To execute a query from a script, use the mysql command with the -e option.

query="insert into FILES (name,type,date,station) VALUES('$archivoF','$tipoOb','$fcha','$st')"
mysql -h dbhost -u dbuser -ppassword dbname -e "$query"

Make sure you put quotes around the values that are strings in the query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.