Using yaml in bash script - bash

I have following yaml file and I need to take inputs from this yaml file in my bash script
Database: backup
Table: mytable
Partitions: P10,P11,P12
I tried this like below but getting error
#!/bin/bash
Database=yq e '.Database' t_partitions.yaml
Table=yq e '.Table' t_partitions.yaml
Partitions=yq e '.Partitions' t_partitions.yaml
mysql -u root -p -e "
use $Database;
alter table $Table truncate partition $Partitions;
"
The error is
bash m.sh run
m.sh: line 2: e: command not found
m.sh: line 3: e: command not found
m.sh: line 4: e: command not found

Your assignment statement is wrong with Bash's grammar.
You need command substitution, like:
#!/bin/bash
Database="$(yq e '.Database' t_partitions.yaml)"
Table="$(yq e '.Table' t_partitions.yaml)"
Partitions="$(yq e '.Partitions' t_partitions.yaml)"
mysql -u root -p -e "
use $Database;
alter table $Table truncate partition $Partitions;
"
Using $() to get output of a command. Use "" to prevent eventually sentence break inside the output by some special character.

Related

How can I disable * expansion in a script?

I have a strange problem - possibly I'm just going blind. I have this short script, which replaces the string #qry# in the here-document with a select statement in a file and then pipes it to mysql:
#!/bin/bash
if [[ "$1" == "-h" ]]
then
echo "sqljob [sqlfile] [procnm] [host] [database] [config file]"
echo " sqlfile: text file containing an SQL statement"
echo " procnm: name that will given to the new, stored procedure"
echo " host: hostname of IP address of the database server"
echo " database: the procedure will be created here"
echo " config file: default configuration file with username and password"
exit
fi
infile=$1
procnm=$2
hn=$3
pn=$4
db=$5
mycfg=$6
{
set -o noglob
sed -e "s/#qry#/$(echo $(cat $infile))/g" <<!
drop procedure if exists $procnm;
delete from jobs where jobname="$procnm";
insert into jobs
set
notes="SQL job $procnm",
jobname="$procnm",
parm_tmpl='int';
delimiter //
create procedure $procnm(vqid int)
begin
call joblogmsg(vqid,0,"$procnm","","Executing #qry#");
drop table if exists ${procnm}_res;
create table ${procnm}_res as
#qry#
end//
delimiter ;
!
} | mysql --defaults-file=$mycfg -h $hn -P $pn $db
However, when the select contains *, it expands to whatever is in the directory even though I use noglob. However, it works from the command line:
$ set -o noglob
$ ls *
What am I doing wrong?
Edit
Block Comments in a Shell Script has been suggested as a duplicate, but as you will notice, I need to expand ${procnm} in the here-doc; I just need to avoid the same happening to select *.
I suspect it is because the construct echo (cat). The echo command gets the * from the cat command and the shell in which it runs expands it. In that shell set noglob is not active.
Try leaving the echo away: /$(cat $infile)/, in the end that is the data you need; then there is no extra glob expansion by a shell.

MonetDB doesn't recognize function names given to mclient via command line

I am trying to export a few columns from a table as encoded integer.
Basically I want to use a bash script to pass the SQL command to mclient as command line argument. My bash script looks like:
#!/bin/bash
dir=`pwd`
for col in occupation native_country martial_status race sex
do
mclient -d adult -s \
"create function encode${col}(s varchar(200)) returns int begin return (select code from ${col}_dict where ${col}=s); end;"
mclient -d adult -s \
"COPY (select encode${col}($col) from adult) INTO '${dir}/${col}.txt' NULL AS '0'"
mclient -d adult -s \
"drop function encode${col}"
done
In each iteration, I want to create a SQL function on the fly. Then use the function to encode an attribute and export it to a text file. And lastly drop the function.
However, the output strangely contains some monster characters,
as if it can't recognize the function name.
If I remove the second mclient command, the other operations are successful.
operation successful
Function 'X�..X�' not defined
operation successful
operation successful
Function '��"X�.X�' not defined
operation successful
operation successful
Function ' X�.PX�' not defined

Execute psql query in bash

I have a problem with executing a psql-query in a bash script.
Below is my code.
run_sql(){
sql_sel="$1;";#sql select
table=$2;#table name
for i in "${!GP_SERVER_NAMES[#]}"
do
logmsg "Executing [$sql_sel] on "${GP_SERVER_NAMES[$i]}": " $loglvl;
result_host[$i]=`${PSQL_HOST[$i]}${sql_sel}`;
#result_host[$i]=cleanresult "`$(${PSQL_HOST[$i]} "$tx_fix $sql_sel" 2>&1`");
if [[ `checkresult "${result_host[$i]}"` != 'true' ]]; then
logmsg "Error occured during sql select: Result for "${GP_SERVER_NAMES[$i]}" '${table}': ${result_host[$i]};" '1' '1';
raise_alarm "${GP_SYNC_SQL_ERR}" "${i}" "${table}" "${result_host}";
fi
logmsg "Result for" ${GP_SERVER_NAMES[$i]} " '${table}': ${result_host[$i]}";
done
final_result='true';
for i in "${!result_host[#]}"
do
if [[ `checkresult "${result_host[$i]}"` = 'true' ]]; then
final_result='false';
I am trying to executing the query on many different servers, with the following command
result_host[$i]=${PSQL_HOST[$i]}${sql_sel};
The above variables have the following meaning:
1. result_host[$i] : is an array that holds the i-th result of the sql query.
2. PSQL_HOST[$i] : is the command line psql-statement, including the IP address which is of the form
psql -t -q -h -U password -d database -c
3. $sql_sel : is the sql_statement
When I run the script, I get the following output.
scriptname.sh: line 168: SELECT: command not found
ERROR: syntax error at end of input
LINE 1: SELECT
^
Why is that? Any help, comments or suggestions would be appreciated.

Delimiter Warning in shell Script with psql

I get a Delimter Error in a Shell Script:
#!/bin/sh
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
;`
EOF
echo $result
#EOF
The result of the Script is fine. But I get two warnings:
./filename.sh: line 13: warning: here-document at line 8 delimited by end-of-file (wanted `EOF')
./filename.sh: line 9: EOF: command not found
What is the problems here? Thank you!
You have the start of your here-doc inside of your command, but the EOF is outside of your command.
result=`psql -d databasename -t -A <<EOF
SELECT COUNT(*) FROM schema.table
WHERE "column_name_x" = 'specific_value_x'
AND "column_name_y" = 'specific_value_y'
AND ("column_name_z" LIKE 'specific_z%' OR "column_name_za" LIKE 'specific_za%')
EOF
`
The ; seems wrong here too (at least it threw an error for me).

mysqlimport with Error 13

I use the following script to import data from a csv file:
#!/bin/bash
# show commands being executed, per debug
set -x
# define database connectivity
_db="xxx"
_db_user="xxx"
_db_password="xxx"
_table="movie"
# define directory containing CSV files
_csv_directory="/tmp"
_csv_file='xxxxxx.csv'
_header_columns_string='link,description,duration,thumbnaillink,iframe,tags,category'
# import csv into mysql
mysqlimport --fields-terminated-by=';' --lines-terminated-by="\n" --columns=$_header_columns_string -u $_db_user -p$_db_password $_db $_table $_csv_directory/$_csv_file
exit
When I execute the script as root via bash import.sh I get the following error message:
+ _db=mydatabase
+ _db_user=xxx
+ _db_password=xxx
+ _table=movie
+ _csv_directory=/tmp
+ _csv_file=xxxxxx.csv
+ _header_columns_string=link,description,duration,thumbnaillink,iframe,tags,category
+ mysqlimport --local '--fields-terminated-by=;' '--lines-terminated-by=\n' --columns=link,description,duration,thumbnaillink,iframe,tags,category -u xxx -pxxx mydatabase movie /tmp/xxxxxx.csv
mysqlimport: Error: 13, Can't get stat of '/var/lib/mysql/mydatabase/movie' (Errcode: 2), when using table: movie
+ exit
but the database and the table exist.
The csv file exists and can be read, the table can be selected and I can manually insert data-rows into the db.
What am I doing wrong?
Try doing it from inside SQL:
load data local infile 'FILENAME.CSV' into table TABLENAME
fields terminated by ',' optionally enclosed by '"' ignore 1 lines;
You run this from the command line or from a shell script like this
db='mysql -hX -uX -pX --database=X'
cat MYSCRIPT.SQL | $db

Resources