Snowsql script not executed when run as here document from bash script - bash

I have a bash script that contains something like this:
snowsql -a <account> -u <user> --authenticator externalbrowser -d <dbname> -o quiet=false <<-EOF
!source foo.sql
EOF
When I run it I don't see any of the output from the commands in foo.sql on the screen. It also appears that none of the SQL in foo.sql is executed (not reflected in state of database). Terminal output is:
* SnowSQL * v1.2.13
Type SQL statements or !help
Goodbye!
If I run foo.sql from an interactive Snowsql session the output from foo.sql is shown and the database is updated accordingly.
Why is foo.sql not executed when called in batch mode from the bash script?

Can you try to run it using -f parameter?
https://docs.snowflake.com/en/user-guide/snowsql-use.html#running-while-connecting-f-connection-parameter

As #Gokhan says this looks to be down to the authentication method. Authentication via a non-browser method makes the problem go away.

Related

difference between execute shell script directly and through pipe

my shell script called mongoLogin.sh as below:
#!/bin/sh
mongo
use demo
show tables
the function of above is logining mongo and show tables of database called demo.
if I execute it directly by:
sh mongoLogin.sh
it works. however if I execute it by this way as below:
cat mongoLogin.sh | sh
compared with execute directly, it will exit automatically, as well as I ctrl+c after execute it directly. It seemed the sh command after pipe will create a new subprocess, and this subprocess finish due to some reason.
Do there exist some method that I can achieve same result by execute script through pipe?
update:
when execute directly it seemed only the first command make effect, because following commands are mongo operations rather than shell commands. And when I execute it by pipe, all commands make effect, but it exit automatically.
the output of executing it through pipe as below:
xxx#xxxMacBook-Pro:~/Downloads$cat mongoLogin.sh | sh
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("43c55950-f9e2-49ca-a458-
611f8c71eae4") }
MongoDB server version: 4.0.3
switched to db demo
test
bye
Both variants don't work, because use demo is not a valid shell command.
I have not worked with mongo, but if the command accepts is input from stdin, you can try
mongo <<OGNOM
use demo
show tables
OGNOM
Make sure that the final OGNOM starts in the first column of your line!

bash call is creating a new process.I want to the next command in same process

I am logging into a remote server using SSH client. I have written a script that will execute two commands on the server.But, as the first command executes a bash script that calls "bash" command at the end. This results in execution of only one command not the other.
I cannot edit the first script to comment or remove the bash call.
i have written following script:
abc.sh
#!/bin/bash
command1="sudo -u user_abc -H /abc/xyz/start_shell.sh"
command2=".try1.sh"
$command1 && $command2
Only command 1 is getting executed not the second as the "bash" call is creating a new process the second command is not executing.
Solution 1
Since you can execute start_shell.sh you must have read permissions. Therefore, you could copy the script, modify it such that it doesn't call bash anymore, and execute the modified version.
I think this would be the best solution. If you really really really have to use start_shell.sh as is, then you could try one of the following solutions.
Solution 2
Try closing stdin using <&-. An interactive bash session will exit immediately if there is no stdin.
sudo -u user_abc -H /abc/xyz/start_shell.sh <&-; ./try1.sh
Solution 3
Change the order if both commands are independent.
./try1.sh; sudo -u user_abc -H /abc/xyz/start_shell.sh

run multiple zkCli commands

I want to run two commands together for zkCli.
zkCli addauth digest username:password && zkCli setAcl /zknode-path world:anyone:crdwa
I have already set the ACL value for a zknode, and want to revert it back. But running this command give, authentication is not valid. How to run these two commands in one session?
I managed to run multiple commands in zkCli using heredoc format
(see How does ` cat << EOF` work in bash?)
Insert this snippet into a bash file
TMPVAR="addauth digest username:password\nsetAcl /zknode-path world:anyone:crdwa"
/zookeeper-3.4.10/bin/zkCli.sh <<EOF
$(echo -e ${TMPVAR})
quit
EOF
First, we set TMPVAR with both commands you wanted to execute in a single zkCli session, with a \n delimiter between them
Then we evaluate TMPVAR into STDIN line by line and this will make zkCli execute command after command, and then finally execute quit

PSQL: How can I prevent any output on the command line?

My problem: I'm trying to run a database generation script at the command line via a batch file as part of a TFS build process to enable nightly testing on a known dataset.
The scripts we run are outputting Notices, Warnings and some Errors on the command line. I would like to suppress at least the Notices and Warnings, and if possible the Errors as they don't seem to have an impact on the overall success of the scripts. This output seems to be affecting the success or failure of the process as far as the TFS build process is concerned. It's highlighting every line of output from the scripts as errors and failing the build.
As our systems are running on Windows, most of the potential solutions I've found online don't work as they seem to target Linux.
I've changed the client_min_messages to error in the postgresql.conf file, but when looking at the same configuration from pgAdmin (tools > server configuration) it shows the value as Error but the current value as Notice.
All of the lines in the batch file that call psql use the -q flag as well but that only seems to prevent the basics such as CREATE TABLE and ALTER TABLE etc.
An example line from the batch file is:
psql -d database -q < C:\Database\scripts\script.sql
Example output line from this command:
WARNING: column "identity" has type "unknown"
DETAIL: Proceeding with relation creation anyway.
Specifying the file with the -f flag makes no difference.
I can manually run the batch file on my development machine and it produces the expected database regardless of what errors or messages show on the command prompt.
So ultimately I need all psql commands in my batch files to run silently.
psql COMMAND &> output.txt
Or, using your example command:
psql -d database -q < C:\Database\scripts\script.sql &> output.txt
use psql -o flag to send the command output to the filename you wish or /dev/null if you don't care about it.
The -q option will not prevent the query output.
-q, --quiet run quietly (no messages, only query output)
to avoid the output you have to send the query result to a file
psql -U username -d db_name -pXXXX -c "SELECT * FROM table_name LIMIT 5;" > C:\test.csv
use 1 > : create new file each time
use 2 >> : will create and keep adding

Exit from the oracle user session when running a script

I have a problem with a bash script on an Ubuntu system. I need to program a script which connects to oracle an spool some queries to text files.
Almost all of this tasks are acomplished but the script doesn't run as it should be.
To generate the connection to oracle I use the following lines inside my script:
su oracle
export ORACLE_SID=DB_SID
export ORACLE_HOME=[ORACLE_PATH]
export TWO_TASK=[HOSTNAME:PORT]
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -s usr/pass << EOF
In the subsequent lines I make all the spooling of the data and finally for exiting from sqlplus I use the following lines:
quit;
To diconnect from sqlplus
exit
To exit from the su oracle session
EOF
And after the EOF tag I put some other commands to be executed.
Problem is when I run my script:
user# sh MyScript.sh
Instead of doing all the tasks, the script only executes the lines to the point of the << EOF and returns to me the control of the terminal still logged as the oracle user.
oracle#user#
If I type 'exit' and press enter then the rest of the script is executed.
I need the script to execute from start to finish without this middle step but I don't know how.
Thanks in advance.
In the end it's not neccesary to use the [su oracle] to make the export of the environment variables so I simply deleted the line:
su oracle
And the process worked like a charm.
Thanks for all who helped.

Resources