syntax error near unexpected token 'fi'" - syntax

if [ $pass = "123456" ] ; then mysql -u root -p$pass "create database newdb"; use newdb; CREAT TABLE user ( name char (30) , lname char (40) );
echo "creat table succesful" fi
When I run it it outputs start and then says Syntax error:
"(" unexpected (expecting "fi")
How could I fix this?

Change it to
if [ $pass = "123456" ] ; then mysql -u root -p$pass "create database newdb"; use newdb; CREAT TABLE user ( name char (30) , lname char (40) );
echo "creat table succesful"; fi
that is a semicolon before the fi.
Also the entire command sequence to mysql probably has to be in double quotes.

Related

If loop not working properly

In my script i execute correctly when first time in the if loop execute after the third time of execution its not working properly .what can i do is any mistake ah...? please fix my issue...?in the script i gave the value for customer name as ABC then again i gave the ABC that time it ececute the if condition correctly,but i gave the third time as ABC its not working properly..!
#!/bin/bash
echo " --- Enter the Database name ---"
read databasename
echo " --- enter the table name --- "
read table_name
sqlite3 $databasename.db "DROP TABLE IF EXISTS $table_name;"
sqlite3 $databasename.db "CREATE TABLE IF NOT EXISTS $table_name(cus_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,cus_name TEXT NOT NULL UNIQUE ,cus_domain TEXT UNIQUE, cus_status TEXT NOT NULL,Port INTEGER NOT NULL);"
echo " --- Enter the total number of rows do you want ---"
read cus_count
echo "--- Enter the following details one by one---"
port_num=8080
for((i=1;i<=cus_count;i++))
do
echo "enter the $i customer details"
echo "---Enter the customer name---"
read c_name
customer=$(sqlite3 $databasename.db "select cus_name from $table_name")
if [[ "$port_num" == "$port_num" ]]; then
port_num=$(($port_num + 1))
fi
if [[ $c_name != $customer ]]
then
echo "---Enter the Status(Active/Inactive)---"
read c_status
c_domain="$c_name"
else
echo "---OOPS you entered customer name already available---"
echo "---Please enter new customer name---"
i=$(($i - 1))
fi
sqlite3 $databasename.db "INSERT OR IGNORE INTO $table_name (cus_name,cus_domain,cus_status, Port) VALUES(\"$c_name\",\"${c_domain,,}.com\",\"$c_status\",\"$port_num\") ;"
done
echo " --- Records from the $table_name ---"
sqlite3 $databasename.db "select * from $table_name;"
please refer to below on "bash script arithmetic operations"
http://tldp.org/LDP/abs/html/arithexp.html
or
http://tldp.org/LDP/abs/html/ops.html

Issue with echo in shell script

getting issue with echo output.
line=table_name
echo "SELECT CASE WHEN FORMAT_TYPE LIKE '%character%' THEN 'replace(replace(replace('||ATTNAME||',''\'',''\\''),''"'',''\"''),''|'',''\|'') as '||ATTNAME||',' ELSE ATTNAME||',' END FROM _V_RELATION_COLUMN WHERE NAME = '$line' ORDER BY ATTNUM;"
Output I am looking for something like below:
SELECT CASE WHEN FORMAT_TYPE LIKE '%character%'
THEN 'REPLACE(REPLACE(REPLACE('||ATTNAME||',''\'',''\\''),''"'',''\"''),''|'',''\|'') AS '||ATTNAME||','
ELSE ATTNAME||','
END
FROM _V_RELATION_COLUMN WHERE NAME IN ('table_name')
ORDER BY ATTNUM;
But getting there error as
-bash: syntax error near unexpected token `)'
You need to escape the double quotes in your string
Try
echo "SELECT CASE WHEN FORMAT_TYPE LIKE '%character%' THEN 'replace(replace(replace('||ATTNAME||',''\'',''\\''),''\"'',''\\\"''),''|'',''\|'') as '||ATTNAME||',' ELSE ATTNAME||',' END FROM _V_RELATION_COLUMN WHERE NAME = '$line' ORDER BY ATTNUM;"
Note that to print an escape you have to escape the escape.
e.g
If you want \"
You need to type \\\"

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

unix shell script to check result of sql query

I have the code like this
v_use_xref_result=`sqlplus -s $UP <
Select Code_Desc
From Code_Detail
Where Code='UXLOC'
and CODE_TYPE='UXRF'
EOF`
if [ "$v_use_xref_result" == "Y" ]; then
echo "s" else
echo "n"
fi
when I query the same in sql developer I am getting the result as Y for this query Select Code_Desc From Code_Detail Where Code='UXLOC' and CODE_TYPE='UXRF' the datatype of Code_Desc is varchar
but I am unable to check in if condition. i am getting "n" as result for this script.
can anyone help me how to check it in if condition.
As written, the heredoc syntax in your script doesn't seem to be correct. Try saying:
v_use_xref_result=$(sqlplus -s $UP <<EOF
Select Code_Desc
From Code_Detail
Where Code='UXLOC'
and CODE_TYPE='UXRF'
EOF
)

Resources