run bash script containing postgresql script failed - bash

I have already create bash script containing sql script (postgresql), but failed.
owner the file : postgres:postgres with 777 (chmod), but when I execute with "./bash1.sh" error: psql: FATAL: role "postgres" does not exist
the bash script:
#!/bin/sh
THE_DATABASE=epolicy
MY_TABLE=hpx_sms
psql ${THE_DATABASE} <<THE_END
SELECT * FROM ${MY_TABLE};
THE_END
how to fix this problem? or any reference? thank you.

Related

How to read .sql file by dictionary on MacOS?

I got error 2 when exec sql file by dictionary
I tried to exec mysql file on Mac OS at this dictionary: "/Users/kato/Desktop/world_mysql_script.sql"
source /Users/kato/Desktop/world_mysql_script.sql;
Then I got this error:
Failed to open file '/Users/kato/Desktop/world_mysql_script.sql', error: 2
Do you have any solution?
1.
Please try to use mysql command.
mysql -u DB_USER -p -h DB_HOST DB_NAME < /Users/kato/Desktop/world_mysql_script.sql

Running into error with BASH script with permission denied, but when running the command directly in bash shell its getting executed

When running below command directly in bash shell, I am able to get the output. But when I am passing it via BASH script getting access denied. Any help would be appreciated
$ jq -r '.id' Repooutput.txt
dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf
Error:
$ sh test.sh
test.sh: line 3: /c/ProgramData/chocolatey/bin/jq: Permission denied
I think the reason is that when executing the script with sh test.sh we're asking the POSIX interpreter (shell) to execute the content on the script, while when executing it with ./test.sh we're asking the script to "execute itself".
For the latter, the file needs to have execution permissions, which you can add with
chmod +x test.sh
Issue was with the naming convention of JQ inside BASH folder path, because of which the script was unable to pick the command. Renaming the JQ within BASH folder resolved this

do shell script results in command not found

I've found sh-script for CPU Throttling and would like to schedule it on startup using AppleScript in the following way:
do shell script "sudo /Users/BohdanAir/Documents/Automation/cputhrottler.sh" "password mypassword" with administrator privileges
And get the following error:
error "sudo: /Users/BohdanAir/Documents/Automation/cputhrottler.sh: command not found" number 1
P.S: The pass provided to the sh file is being fully copied from the Get Info option (CDM + I)
Not even sure how you received that error message when the do shell script command shown in your question does not even compile.
The "password mypassword" portion actually has to be password "mypassword" for it to compile.
The reason you're getting:
error "sudo: /Users/BohdanAir/Documents/Automation/cputhrottler.sh: command not found" number 1
Is because cputhrottler.sh is not set as executable.
Make it executable using the following command:
chmod u+x /Users/BohdanAir/Documents/Automation/cputhrottler.sh
This will allow it to execute and not throw that particular error message.
Or use the following convention:
do shell script "sh /Users/BohdanAir/Documents/Automation/cputhrottler.sh" password "mypassword" with administrator privileges
Note that sudo was replaced with sh, or use other shell. In this manner the shell script does not need to be made executable.

:not found bash script, mysql dump

Help me please. When I execute .sh
mysqldump -ulogin-ppass --all-databases > dumps/dbs.sql;
I get error message
/home/b/script.sh: 1: /home/b/script.sh:
: not found
but command create file successfully!
As I understand when the sign ; is in the end, error message occurs. Without it error : Directory nonexistent
Try this:
#!/bin/bash
DIR=$(date +%F_%H-%M)
mkdir $DIR
mysqldump -uusername -ppassword --all-databases > $DIR/dbs.sql

PSQL run from shell script - command not found?

I try to execute an PSQL from shell, and the thing is - it returns the error "command not found". I have a shell script in which there're lines:
ID3=`more DATA/Id3.txt`
psql -h localhost test test -Atc "SELECT id, reference, timestamp FROM restricted WHERE id='`$ID3`'"
In the Id3.txt there's only the ID. When the psql command is written and executed direct through prompt - there's no problem at all and correct value is returned. When executed with a .sh file - error "command not found" is brought up. I have no clue why. Maybe anyone have a idea?
In your script try to add which psql to see whether you can find the executable
Run below command on your console: whereis psql
And then replace psql inyour script with the output of above command. This

Resources