I am trying to run a script with ./insert_data.sh; and I get an error saying there is a syntax error at or near the "."
Am working in PSQL, thanks
Edit #1: My script:
#!/bin/bash
# Script to insert data from courses.csv and students.csv into students database
PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
cat courses_test.csv | while IFS="," read MAJOR COURSE
do
# get major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
# if not found
if [[ -z $MAJOR_ID ]]
then
# insert major
INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
echo $INSERT_MAJOR_RESULT
# get new major_id
fi
# get course_id
# if not found
# insert course
# get new course_id
# insert into majors_courses
done
Edit #2: Command i'm using to run the script: ./insert_data.sh; Error I'm recieving:
students=> ./insert_data.sh;
ERROR: syntax error at or near "."
LINE 1: ./insert_data.sh;
^
Solved the problem- I had to get out of the PSQL terminal and enter the command within the "project" directory
We can't replicate your case 1:1 because lack of data. But you can always use bash debug with -x flag to see where it actually stuck:
test#test:/tmp$ bash -x ./test.sh
+ PSQL='psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c'
+ cat courses_test.csv
+ IFS=,
+ read MAJOR COURSE
++ psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c 'SELECT major_id FROM majors WHERE major='\''"Sex"'\'''
psql: error: could not connect to server: No such file or directory
Related
rebuild the udev rules in the /etc/udev/rules.d/99-oracle-asmdevices.rules file by running the following script:
i=1
cmd="/sbin/scsi_id -g -u -d"
for disk in sdb sdc sdd sde sdf; do
cat <<EOF >> /etc/udev/rules.d/99-oracle-asmdevices.rules
KERNEL=="sd?1",SUBSYSTEM=="block", PROGRAM=="$cmd /dev/\$parent", \
RESULT=="`$cmd /dev/$disk`", SYMLINK+="asm-disk$i", OWNER="grid", GROUP="dba", MODE="0660"
EOF
i=$(($i+1))
Error_Message:
script.sh: line 11: syntax error: unexpected end of file
[root#london1 sf_D_DRIVE]# warning: here-document at line 5 delimited by end-of-file (wanted EOF') -bash: syntax error near unexpected token ('
First, https://www.shellcheck.net/ - bookmark it, use it.
My corrections and tweaks -
#!/bin/bash
i=0
cmd="/sbin/scsi_id -g -u -d"
for disk in sdb sdc sdd sde sdf
do ((i++)); cat >> /etc/udev/rules.d/99-oracle-asmdevices.rules <<EOF
KERNEL=="sd?1",SUBSYSTEM=="block", PROGRAM=="$cmd /dev/\$parent", \
RESULT=="$($cmd /dev/$disk)", SYMLINK+="asm-disk$i", OWNER="grid", GROUP="dba", MODE="0660"
EOF
done
Im running the next postgres query using the next bash command.
sudo -u postgres bash -c "psql -d db -c \"SELECT ip FROM db_accounts;\"" \>/dev/null
The output is a table but before the table is printed, I get the following info prints
> psql: /usr/lib64/libssl.so.10: no version information available
> (required by psql) psql: /usr/lib64/libcrypto.so.10: no version
> information available (required by /usr/pgsql-9.4/lib/libpq.so.5)
> psql: /usr/lib64/libssl.so.10: no version information available
> (required by /usr/pgsql-9.4/lib/libpq.so.5)
I want to run my command without these prints appearing.
I tried to change the end of the command >/dev/null to 2>/dev/null and indeed the prints were disable but my table was not fully displayed (out of 800 rows only 40 were displayed),
Can someone help me please?
Use --quiet when you start psql
OR
It can be set in your postgresql.conf file by adding this
client_min_messages = warning
This blog is really helpful.
To fix want I wanted I added --pset pager=off to the psql to get the whole table and the disable the prints I change the end of the command to 2>/dev/null
final command:
sudo -u postgres bash -c "psql --pset pager=off --quiet -d db -c \"SELECT ip FROM db_accounts;\"" 2>/dev/null
I need to execute SQL from batch file.
I am executing following to connect to Postgres and select data from table
C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
select * from test;
I am able to connect to database, however I'm getting the error
'select' is not recognized as an internal or external command,
operable program or batch file.
Has anyone faced such issue?
This is one of the query i am trying, something similar works in shell script, (please ignore syntax error in the query if there are any)
copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;
You could pipe it into psql
(
echo select * from test;
) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
When closing parenthesis are part of the SQL query they have to be escaped with three carets.
(
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
Use the -f parameter to pass the batch file name
C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% -f 'sql_batch_file.sql'
http://www.postgresql.org/docs/current/static/app-psql.html
-f filename
--file=filename
Use the file filename as the source of commands instead of reading commands interactively. After the file is processed, psql terminates. This is in many ways equivalent to the meta-command \i.
If filename is - (hyphen), then standard input is read until an EOF indication or \q meta-command. Note however that Readline is not used in this case (much as if -n had been specified).
if running on Linux, this is what worked for me (need to update values below with your user, db name etc)
psql "host=YOUR_HOST port=YOUR_PORT dbname=YOUR_DB_NAME user=YOUR_USER_NAME password=YOUR_PASSWORD" -f "fully_qualified_path_to_your_script.sql"
You cannot put the query on separate line, batch interpreter will assume it's another command instead of a query for psql. I believe you will need to quote it as well.
I agree with Spidey:
1] if you are passing the file with the sql use -f or --file parameter
When you want to execute several commands the best way to do that is to add parameter -f, and after that just type path to your file without any " or ' marks (relative paths works also):
psql -h %host% -p 5432 -U %user% -d %dbname% -f ..\..\folder\Data.txt
It also works in .NET Core. I need it to add basic data to my database after migrations.
Kindly refer to the documentation
1] if you are passing the file with the sql use -f or --file parameter
2] if you are passing individual command use -c or --command parameter
If you are trying the shell script
psql postgresql://$username:$password#$host/$database < /app/sql_script/script.sql
I'm try to migrate a command from hive cli to beeline:
$ hive -e "USE my_db;ALTER TABLE apache_log ADD PARTITION(year = 2015, month = 3) LOCATION 'hdfs://DB//user/my_db/prod/apache_log/2015/03';"
this works.
But when doing it in beeline it doesn't like the "USE my_db;" part:
$ beeline -u jdbc:hive2://my_server.com -n my_user -p my_password -e "USE my_db;ALTER TABLE apache_log ADD PARTITION(year = 2015, month = 3) LOCATION 'hdfs://DB/user/my_user/prod/apache_log/2015/03';"
Error: Error while compiling statement: FAILED: ParseException line 1:9 missing EOF at ';' near 'my_db' (state=42000,code=40000)
Beeline version 0.12.0-cdh5.1.3 by Apache Hive
Based on the error message (expecting EOF) it seems to me that beeline would not accept multi-statements like hive cli used to do?
I have raised it as a Hive bug and was accepted as a bug and patch provided: https://issues.apache.org/jira/browse/HIVE-9877
It is possible to do this by doing it this way:
(i wrote it on more than one line to make it more readable)
beeline
-u jdbc:hive2://my_server.com
-n my_user -p my_password
-e "USE my_db;"
-e "ALTER TABLE apache_log ADD PARTITION(year = 2015, month = 3) LOCATION 'hdfs://DB/user/my_user/prod/apache_log/2015/03';"
-e "--other nice queries..."
I am a root user and in a shell script I would like to change user to oracle than run a sql script, I tried following;
#!/bin/sh
portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
then
sudo -u oracle << EOF
/oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 #/oracle/product/102/db/GW_EP_List.sql;
EOF
else
exit
fi
it gives me following error;
./deneme2.sh: syntax error at line 12: `end of file' unexpected
Can you please let me know what might be the problem?
Thanks,
Halit
When using here documents the closing string MUST be at the beginning of the line!
Try
#!/bin/sh
portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
then
sudo -u oracle << EOF
/oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 #/oracle/product/102/db/GW_EP_List.sql;
EOF
else
exit
fi
You can use su. Remember get environment with su -:
COMMAND="/oracle/product/102/db/bin/sqlplus -s a51... "
su - oracle -c $COMMAND
A nice sample oracle-base site, Automating Database Startup and Shutdown on Linux Post:
case "$1" in
'start')
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
touch /var/lock/subsys/dbora
;;
sudo -u oracle /oracle/product/102/db/bin/sqlplus -s a513s..........
You don't need EOF here. Execute your sqlplus command like above. In this case your oracle user must be a sudo user.
If oracle is a normal user
su - oracle -c "/oracle/product/102/db/bin/sqlplus -s a513s.........."
A little more about su command (From man page):
The su command is used to become another user during a login session.
Invoked without a username, su defaults to becoming the superuser.
The optional argument - may be used to provide an environment similar
to what the user would expect had the user logged in directly.
Additional arguments may be provided after the username, in which case
they are supplied to the user's login shell. In particular, an
argument of -c will cause the next argument to be treated as a command
by most command interpreters. The command will be executed by the
shell specified in /etc/passwd for the target user.