psql command is not found in only certain lines in shell scipt - bash

I have a shell script with a psql command to update a Redshift table. It's worked several times in the original spot in the shell script, but when I copied the command into another line in the script, I got an error saying psql: command not found.
Here's some pseudocode of where the psql command has and has not worked:
#start
if [ -f filename.txt ];
then echo 'first area';
psql #has not worked here
else psql ; #has successfully worked here
fi;
psql # has not worked here
#end
I'd just like pointers or suggestions to figure out the behavior of this. Is there any plausible explanation for this to work only in that else statement?

Related

PSQL /copy :variable substitution not working | Postgresql 11

I'm trying to read CSV file and writing the same into the table, CSV file was located in my local machine(client). I used /copy command and achieved the same. Here I have hardcoded my filepath in sql script. I want to parameterised my csv file path.
Based on my analysis /copy not supported :variable substitution, but not sure
I believe we can achieve this using shell variables, but I tried the same, It's not working as expected.
Following are my sample scripts
command:
psql -U postgres -h localhost testdb -a -f '/tmp/psql.sql' -v path='"/tmp/userData.csv"'
psql script:
\copy test_user_table('username','dob') from :path DELIMITER ',' CSV HEADER;
I executing this commands from shell and I'm getting no such a file not found exception. But same script is working with hardcoded path.
Anyone able to advise me on this.
Reference :
Variable substitution in psql \copy
https://www.postgresql.org/docs/devel/app-psql.html
I am new to Bash. So far your problem is way hard for me.
I can do it in one shell script. Maybe later I can make it to two scripts.
The follow is a simple one script file.
#!bin/bash
p=\'"/mnt/c/Users/JIAN HE/Desktop/test.csv"\'
c="copy emp from ${p}"
a=${c}
echo $a
psql -U postgres -d postgres -c "${a}"

grep command works in command line, but not in bash script: get no such file or directory erro

I know that there are some related questions already about this, but can't make it work for me!
So I can run grep in the command line and it works fine, but if I do that on a bash script, I have the following error:
grep: secondword:No such file or directory
I am connecting via ssh to the server, then I run some commands. The path to grep in the server is /bin/grep, but still it does not work. Here is the sample code:
#!/bin/bash
$host="user#host";
ssh $host "
myinfo=\$(grep "word secondword" path/to/file);
"
I also verified that it does not have the CR that is created in Windows with Notepad++. Any ideas on how to fix this?
EDIT:
As suggested, I made the following change with the quotes:
#!/bin/bash
$host="user#host";
ssh $host "
myinfo=\$(grep \"word secondword\" path/to/file);
"
but now I have a very weird behavior: it looks like is listing all the files that are on the home server path.Doing echo to the variable:
file1 file2 file 3
file4 file5 etc.
Why it as this behavior? Did I miss something?
Please Put Script working Dir. While run crontab it will take as user home as default path.
#!/bin/bash
cd Your_Path
$host="user#host"
ssh $host
myinfo=\$(grep "word secondword" path/to/file)

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

Bash script : Login to postgres home from shell script

I have to take a data dump from my postgres database.
How do I log in to the postgres home ? . I have the following script but it doesn't work :
#!/bin/sh$
export PASSWORD= something
echo $PASSWORD | sudo -S su postgres
pg_dump somedb > dump.txt+`date +%Y-%m-%d`
However when I run this script I do not get logged to postgres#gauss:$ at the same time script doesn't throw an error. Is there something I am doing wrong here ?
The reason your script fails is that the line
echo $PASSWORD | sudo -S su postgres
causes su to fork a subordinate shell. That shell tries to read from the standard input which has already been exhausted by sudo -S in reading the password. When the shell finds no more input (EOF) it exits. The next line of your script then executes as if that quoted line never happened, and therefore runs under your UID.
See j.hloetzek's answer for a much better way to do what you want.
Also the script as pasted has a two syntax errors in it, but you shouldn't use that approach anyway.
You cannot pipe the password to the sudo command, but can allow in /etc/sudoers certain commands to be run without password (check exact syntax!)
username YOURUSERNAME = NOPASSWD: /sbin/su postgres

bash command doesnt seem to work, but its echo does?

Well, I'm new to linux so this may be a very newbie kinda of thing, here it goes:
I have a script in which I'm trying to send some different jobs to remote computers (in fact Amazon's EC2 instances), these jobs are in fact the same function which I run with different parameters.
eventually in the script code I have this line:
nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
if I do:
echo nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
I get:
nohup ssh -fqi key.pem ubuntu#ec2-72-44-41-228.compute-1.amazonaws.com '(nohup ./Script.sh 11 1&)'
Now the weird thing. If I run the code with no echo in the script it doesnt work! it says in the nohup.out (in my laptop, no nohup.out is created in the remote instance) bash: (nohup ./Script.sh 10 1&): No such file or directory
The file does exist locally and remotely and is chmod +x.
If I simply run the very same script with an echo in front of the problematic line and copy its output and paste in the terminal, it works!.
Any clues welcome, thanks!
Try removing the single quotes from $tmp. It looks like bash is treating (nohup ./Script.sh 10 1&) as the command with no parameters, but technically nohup is the command with the parameters ./Script.sh 10 1.
The problem is the single quotes around the nohup command in your $tmp variable. These don't get used on the shell locally, so SSH passes them verbatim. This means remotely the ssh server tries to interpret (nohup ./Script.sh 10 1&) as a command (looks for a file named that) which there clearly isn't. Make sure you remove the single quotes in $tmp.

Resources