Delimiter Warning in shell Script with psql - shell

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

Related

Using yaml in bash script

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.

Bash treating variable expansion as command while passing to other script

a=HDH b=udud c=jsjsj bash secondscript
The command above works. I'd like to save the assignments in a variable, like so:
value="\
a=HDH \
b=udud \
c=ududj \
"
$value bash secondscript
But it gives an error:
test.sh: line 9: a=HDH: command not found
Why? What can I do instead?
bash's taking first item a=HDH as a command, what you need is :
value=(
"a=HDH"
"b=udud"
"c=ududj"
)
env "${value[#]}" bash secondscript

unix shell script not working gives me error

what is wrong with my code this is it -
export -P ttiUsername="Username: " || ^
i get an error about command ^ and the -p
here is my error
/Users/michaelgray/Desktop/ToontownWorld/ToontownInfiniteRetro/start_game.sh: line 3: export: -P: invalid option
export: usage: export [-nf] [name[=value] ...] or export -p
/Users/michaelgray/Desktop/ToontownWorld/ToontownInfiniteRetro/start_game.sh: line 3: ^: command not found
edit: i lowercased the p but it doesnt prompt u to enter username like it should
The problem with your code is this line:
export -P ttiUsername="Username: " || ^
It's wrong because export doesn't read data, -P is not a valid option, and ^ is not a valid command. None of it makes sense if the goal is to read data.
To read data from the user, use read:
read -p "Username: " ttiUsername
echo "You wrote: $ttiUsername"

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.

Bash script error. What is wrong with this script?

I'm new at bash script writing and I have this error. I have looked everywhere to find an answer with no success. What is wrong with this script?
#!/bin/bash
exec >> /Users/k_herriage/bin/post-gererate.out 2>&1
date
set -x
mynewfile="~/bin/convert_tst.txt"
myfile=fopen($mynewfile,'w+' );
#echo $myfile
fwrite($myfile, "testing");
fclose($myfile);
exit (0)
line 7: syntax error near unexpected token `('
line 7:`myfile = fopen ( '~/bin/convert_tst.txt','w' );'
Few points:
Calling a function in bash does not require parens, it is syntactically equivalent to a command:
do_something arg1 arg2 arg3
There is no need to do open-append-close sequence in bash, it is perfectly doable with a single command:
echo "testing" >> $mynewfile; ##
>> means "append", where if it was >, it would mean "overwrite" or "discard content". (Both will create the file if it didn't exist.)

Resources