I have a script file which sends query to sqlplus and sends the output to some other output file.
However, this output file contain some info from sqlplus - some kind of greeting.
SQL*Plus: Release 11.2.0.3.0 Production on Tue Jan 15 12:30:40 2013
Copyright (c) 1982 2011 Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning Real Application Clusters Automatic Storage Management OLAP
Data Mining and Real Application Testing options
is there some way to execute the script so that this greeting will not be as part of a file?
You can use the sqlplus -s option when you connect to the database.
Execsqlstatement1="select
'ZZZ|'
||count(*) total
from table1"
$ORACLE_HOME/bin/sqlplus user/tneal01 << Eossql
set lines 80
set pages 50000
set timing on
spool /user/tneal01/SPOOL/output.`date +%Y-%m-%d`.tmp
$Execsqlstatement1
/
spool off
quit;
Eossql
Now if you don't want anything besides the result in the output file you can do something like this in your script:
grep 'ZZZ' $OutPutDir/$OutPutFile.$today.tmp|cut -d"|" -f2- >$OutPutDir/$OutPutFile.$today.txt
Here is a full example on why this works:
#!/bin/ksh
today=`date "+%Y-%m-%d"`
OutPutDir="/users/ttead01/SPOOL"
OutPutFile="output"
#add sql statements here
Execsqlstatement1="select
'ZZZ|'
||count(*) total
from users"
#adding connection details here
$ORACLE_HOME/bin/sqlplus tead01/rangers $SQLPLUS -s / << Eossql
set lines 80
set pages 50000
set timing on
spool /users/ttead01/SPOOL/output.`date +%Y-%m-%d`.tmp
$Execsqlstatement1;
spool off
quit;
Eossql
#check to make sure no ORA errors in the query
grep "ORA-" $OutPutDir/$OutPutFile.$today.tmp
if [ $? -eq 0 ]
then
echo "LOG MESSAGE sql select failed"
exit 1
fi
#Seach for ZZZ and cut out junk and send only result to a .txt file
grep 'ZZZ' $OutPutDir/$OutPutFile.$today.tmp|cut -d"|" -f2- >$OutPutDir/$OutPutFile.$today.txt
#Remove the .tmp file if .txt was created
if [ $? -eq 0 ]
then
/usr/bin/rm -f $OutPutDir/$OutPutFile.$today.tmp
else
exit 1
fi
Now for the test:
bash-3.2$ test.sh
Copyright (c) 1982, 2010, Oracle. All rights reserved.
SQL> set autocommit on;
SQL> rem set linesize 132;
SQL> define _editor = vi
SQL> alter session set nls_date_format = 'YYYYMMDD HH24MISS';
Session altered.
SQL> SQL> SQL> SQL> SQL> 2 3 4 5 6 7 8
TOTAL
--------------------------------------------
ZZZ|32
Elapsed: 00:00:00.17
Now let's look at the output .txt file:
bash-3.2$ cat output.2016-05-28.txt
32
bash-3.2$
Related
I am using vsql.exe on an external Vertica database for which I don't have any administrative access. I use some views with simple SELECT+FROM+WHERE queries.
These queries 90% of the time work just fine, but some times, randomly, I get this error:
ERROR 3326: Execution time exceeded run time cap of 00:00:45
The strange thing is that this error can happen way after those 45 seconds, even after 3 minutes. I've been told this is related to having different resource pools, but anyway I don't want to dig into that.
The problem is that when this occurs, vsql.exe returns errorlevel 0 and there is (apparently almost) no way to know this failed.
The output of the query is stored in a csv file. When it succeeds, it ends with (#### rows). But when it fails with this error, it just stops at any point of the csv, and its resulting size is around half of what's expected. This is of course not what you would expect when an error occurs, like no output or an empty one.
If there is a connection error or if the query has syntax errors, the errorlevel is not 0, so in those cases it behaves as expected.
I've tried many things, like increasing the timeout or adding -v ON_ERROR_STOP=ON to the vsql.exe parameters, but none of that helped.
I've googled a lot and found many people having this error, but the solutions are mostly related to increasing the timeouts, not related to the errorlevel returned.
Any help will be greatly appreciated.
TL;DR: how can I detect an error 3326 in a batch file like this?
#echo off
vsql.exe -h <hostname> -U <user> -w <pwd> -o output.cs -Ac "SELECT ....;"
echo %errorlevel% is always 0
if errorlevel 1 echo Error!! But this is never displayed.
Now that's really unexpected to me. I don't have Windows available just now, but trying on my Mac - at first just triggering a deliberate error:
$ vsql -h zbook -d sbx -U dbadmin -w $VSQL_PASSWORD -v ON_ERROR_STOP=ON -Ac "select * from foobarfoo"
ERROR 4566: Relation "foobarfoo" does not exist
$ echo $?
1
With ON_ERROR_STOP set to ON, this should be the behaviour everywhere.
Could you try what I did above through Windows, just with echo %ERRORLEVEL% instead of echo $?, just from the Windows command prompt and not in a batch file?
Next test: I run on resource pool general in my little test database, so I temporarily modify it to a runtime cap of 30 sec, run a silly query that will take over 30 seconds with ON_ERROR_STOP set to ON, collect the value returned by vsql and set the runtime cap of general back to NONE. I also have the %VSQL_* % env variables set so I don't have to repeat them all the time:
rem Windows way to set environment variables for vsql:
set VSQL_HOST=zbook
set VSQL_DATABASE=sbx
set VSQL_USER=dbadmin
set VSQL_PASSWORD=***masked***
Now for the test (backslashes, in Linux/MacOs escape a new line, which enables you to "word wrap" a shell command. Use the caret (^) in Windows for that):
marco ~/1/Vertica/supp $ # set a runtime cap
marco ~/1/Vertica/supp $ vsql -i -c \
"alter resource pool general runtimecap '00:00:30'"
ALTER RESOURCE POOL
Time: First fetch (0 rows): 116.326 ms. All rows formatted: 116.730 ms
marco ~/1/Vertica/supp $ vsql -v ON_ERROR_STOP=ON -iAc \
"select count(*) from one_million_rows a cross join one_million_rows b"
ERROR 3326: Execution time exceeded run time cap of 00:00:30
marco ~/1/Vertica/supp $ # test the return code
marco ~/1/Vertica/supp $ echo $?
1
marco ~/1/Vertica/supp $ # clear the runtime cap
marco ~/1/Vertica/supp $ vsql -i -c \
"alter resource pool general runtimecap NONE "
ALTER RESOURCE POOL
Time: First fetch (0 rows): 11.148 ms. All rows formatted: 11.383 ms
So it works in my case. Your line:
if errorlevel 1 echo Error!! But this is never displayed.
... never echoes anything because the previous line, with echo will return 0 to the shell, overriding the previous errorlevel.
Try it command by command on your Windows command prompt, and see what happens. Just echo %errorlevel%, without evaluating it.
And I notice that you are trying to export to CSV format. Then, try this:
Format the output unaligned (-A)
set the field separator to comma (-F ',')
remove the footer '(n rows)' (-P footer)
limit the output to 5 rows in the query for test
(I show the output before redirecting to file):
marco ~/1/Vertica/supp $ vsql -A -F ',' -P footer -c "select * from one_million_rows limit 5"
id,id_desc,dob,category,busid,revenue
0,0,1950-01-01,1,====== boss ========,0.000
1,-1,1950-01-02,2,kbv-000001kbv-000001,0.010
2,-2,1950-01-03,3,kbv-000002kbv-000002,0.020
3,-3,1950-01-04,4,kbv-000003kbv-000003,0.030
4,-4,1950-01-05,5,kbv-000004kbv-000004,0.040
Not aligning is much faster than aligning.
Then, as you spend most time in the fetching of the rows (that's because you get a timeout in the middle of an output file write process), try fetching more rows at a time than the default 1000. You will need to play with the value, depending on the network settings at your site until you get your best value:
-v ROWS_AT_A_TIME=10000
Once you're happy with the tested output, try this command (change the SELECT for your needs, of course ....):
marco ~/1/Vertica/supp $ vsql -A -F ',' -P footer \
-v ON_ERROR_STOP=ON -v ROWS_AT_A_TIME=10000 -o one_million_rows.csv \
-c "select * from one_million_rows"
marco ~/1/Vertica/supp $ wc -l one_million_rows.csv
1000001 one_million_rows.csv
The table actually contains one million rows. Note the line count in the file: 1,000,001. That's the title line included, but the footer (1000000 rows) removed.
I am new to Fabric 2.0 and recently installed all samples and I was able to run test-network without an issue with 2 orgs. Then I followed the directory on addOrg3 to add 3rd organization and join the channel I created earlier.
Now the fun part came in when I wanted to add 4th organization. What I did was, I copied the addOrg3 folder and renamed almost everything in each file to represent 4th organization. I even assigned new PORT for this organization. However I am seeing the following error.
I've also added the following in Scripts/envVar.sh
export PEER0_ORG4_CA=${PWD}/organizations/peerOrganizations/org4.example.com/peers/peer0.org4.example.com/tls/ca.crt
And added the following in envVarCLI.sh
elif [ $ORG -eq 4 ]; then
CORE_PEER_LOCALMSPID="Org4MSP"
CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG4_CA
CORE_PEER_ADDRESS=peer0.org4.example.com:12051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/organizations/peerOrganizations/org4.example.com/users/Admin#.../msp
I have also added step1Org4.sh and step2Org4.sh basically following by following addOrg3's structure.
What steps do you follow to add additional organizations ? Please help.
"No such container: Org4cli"
Sorry for the formatting since I wasn't able to put in to coding style but here is the output from running the command "./addOrg4.sh up"
**Add Org4 to channel 'mychannel' with '10' seconds and CLI delay of '3' seconds and using database 'leveldb'
Desktop/blockchain/BSI/fabric-samples/test-network/addOrg4/../../bin/cryptogen
##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
##########################################################
############ Create Org4 Identities ######################
##########################################################
+ cryptogen generate --config=org4-crypto.yaml --output=../organizations
org4.example.com
+ res=0
+ set +x
Generate CCP files for Org4
Desktop/blockchain/BSI/fabric-samples/test-network/addOrg4/../../bin/configtxgen
##########################################################
####### Generating Org4 organization definition #########
##########################################################
+ configtxgen -printOrg Org4MSP
2020-05-29 13:33:04.609 EDT [common.tools.configtxgen] main -> INFO 001 Loading configuration
2020-05-29 13:33:04.617 EDT [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 002 Loaded configuration: /Desktop/blockchain/BSI/fabric-samples/test-network/addOrg4/configtx.yaml
+ res=0
+ set +x
###############################################################
####### Generate and submit config tx to add Org4 #############
###############################################################
Error: No such container: Org4cli
ERROR !!!! Unable to create config tx **
In your addOrg4.sh have condition check like this:
CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /fabric-tools/) {print $1}')
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "Bringing up network"
Org4Up
fi
If you already run addOrg3.sh up, CONTAINER_IDS alway have value (Example: 51b4ad60d812). It is ContainerID of Org3cli. So function Org4Up will never call. Simple way is just comment code like this:
# CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /fabric-tools/) {print $1}')
# if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "Bringing up network"
Org4Up
# fi
It will bring up Org4cli you missing.
First check the container is up or not and if it is up then I think the CLI where the command is executed is not bootstrapped with the Org4 details.
I have added the 4th Organization from the three Org Hyperledger Fabric Network .Firstly, you have to create the Org4-artifacts (Crypto.yaml and Org4 docker file including the Org4Cli) and then try to follow the manual (step by step) process to add the new Organization from the official documentation.
https://hyperledger-fabric.readthedocs.io/en/release-2.0/channel_update_tutorial.html
Omit the process of editing scripts (step1 Org3.sh ...) because the workflow for adding the 4th or a new Org is slightly changed.So,you will spend a lot of time in just modifying the scripts.
I will write an article to add a new Org (4th) on medium,will paste the link here too.
How can I use the netcat program for bidirectional communication with a networked postscript printer? Postscript programs frequently send information to the STDOUT and that is difficult to capture over the network. I need bidirectional communication with the printer 9100 port. My answer is below. Does anybody know of a better way?
The netcat program can be used for bidirectional communication with a postscript printer.
I have been using some simple programs to send print jobs directly to a network postscript printer and haven't gotten any return info from the printer until now. I like to communicate directly with the printer instead of sending jobs to a spooler.
There is a little information a few places about printing with netcat just very sparse so I am making this report.
My printer is a Xerox 6500n connected to a router by ethernet cable. I can send postscript programs or pdf files directly to the printer in a variety of ways. Sometimes I want to get answers from the printer and so have a test program to use the printer to convert the input using 'pathforall' and send back the answer. The netcat does this nicely:
nc -n 192.168.1.111 9100 < pathforall.ps
I then get the desired response. This has been checked and is similar to ghostscript output only slightly off for some reason:
$ nc -v -w 5 -n 192.168.1.111 9100 < pathforall.ps
192.168.1.111 9100 (hp-pdl-datastr) open
28.6998 12.2999 moveto
28.6998 9.29996 28.3998 8.09995 25.2998 6.59998 curveto
24.3998 6.09998 21.2999 4.79999 19.0999 4.79999 curveto
15.3999 4.79999 12.4999 7.89996 12.4999 12.5999 curveto
12.4999 12.7999 lineto
12.4999 16.1999 13.5999 21.7999 28.6998 26.7999 curveto
closepath
$ gs pathforall.ps
GPL Ghostscript 9.50 (2019-10-15)
Copyright (C) 2019 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Loading Times-Roman font from /usr/share/ghostscript/fonts/Times-Roman.pfa... 4824712 3114962 3833824 2546359 1 done.
28.6184 12.2615 moveto
28.6184 9.27632 28.3224 8.07977 25.2262 6.57484 curveto
24.3257 6.08141 21.2418 4.78618 19.0461 4.78618 curveto
15.3577 4.78618 12.4589 7.8824 12.4589 12.5576 curveto
12.4589 12.7673 lineto
12.4589 16.1472 13.5567 21.7352 28.6184 26.7188 curveto
28.6184 12.2615 lineto
closepath
The netcat allows me to communicate with my printer similar to ghostscript except I don't have the executive mode working yet. Also, be sure to include a 'flush' command in the postscript to get the entire answer sent back.
This can be used for many purposes to get information from the printer.
EDIT: Here is a simple test to see if the printer STDOUT is getting back to the computer:
%!
(Hello World\n) print flush
EDIT3: Postscript executive mode works using telnet for the Xerox 6500n as suggested by luser droog. See comments below.
Here is a bash /dev/tcp alternative for printing without netcat.
This sends to a network PostScript printer using bash if built with --enable-net-redirections. This works with my Void bash and with my Xerox Phaser 6500n and switches between PJL and PostScript. Testing results with other printers would be helpful.
#!/bin/bash
#
# ++++ driverless postscript printing ++++
# bash needs --enable-net-redirections build option
#
# for postscript programs with postscript printer only
#
# bash escape is \033 or \e or \E or \x1B
exec 5<>/dev/tcp/192.168.1.111/9100 || exit 1 # change ip as needed
{
echo -e "\E%-12345X#PJL" # PJL entrance
echo #PJL ECHO "$(date)"
echo #PJL ECHO "setting up printer ..."
echo #PJL COMMENT Change Printer Settings
echo #PJL SET COPIES = 1 # modify environment settings
echo #PJL SET MANUALFEED = off
echo #PJL ECHO "Starting PostScript Program ..."
echo #PJL ENTER LANGUAGE = POSTSCRIPT
echo "%!" # optional
cat "$1" || echo -E "(Hello World\n) print flush" # if input.ps missing
echo -e "\004" # ctrl-D for end of file
echo -e "\e%-12345X#PJL" # back to PJL
echo #PJL ECHO "Finished PostScript Program."
echo #PJL RESET # unset modified environment settings
echo #PJL ECHO BYE.
echo -e "\033%-12345X" # PJL universal exit
} >&5 # send to printer
while read -t 122 -r LINE # timeout after 122 seconds of silence
do
echo "$LINE"
if [[ "$LINE" =~ #PJL\ ECHO\ BYE. ]]; then break; fi
done <&5
echo "Finished reading printer"
exec 5>&- # close
exec 5<&- # close both
exit 0
Here is the result in my terminal. Be sure to send a file like "print.sh input.ps" or this will happen:
$ ./print.sh
cat: '': No such file or directory
#PJL ECHO Mon 20 Apr 2020 07:10:03 AM PDT
#PJL ECHO setting up printer ...
#PJL ECHO Starting PostScript Program ...
Hello World
#PJL ECHO Finished PostScript Program.
#PJL ECHO BYE.
Finished reading printer
UPDATE: I have added this here: github
I am using a shell script to 'spool' a query. Here is a toy version:
#!/bin/sh
sqlplus -s userid/pass#SID << EOF
set echo off
set term off
set trims on
set pages 0
set feedback off
set linesize 1000
set colsep "|"
SPOOL $2
SELECT 'HEADER1|HEADER2|HEADER3' FROM DUAL
UNION ALL
SELECT
COLUMN1||'|'||
COLUMN2||'|'||
COLUMN3
FROM $1;
SPOOL OFF
EXIT 0;
EOF
And submitting using
nohup sh sqlquery.sh intable outtable > log &
The query runs fine and is formatted exactly how I would like, but the rows returned by the query are written to both the spool file and the log... I thought 'set echo off' would take care of this, but I am obviously missing something.
Any ideas?
I am offered (by some framework) to run commands in sqlplus, but am not launching it myself.
I'd like to know the version of that sqlplus running.
Within SQL*Plus, there are some preDEFINEd substitution variable:
SQL> define
DEFINE _DATE = "23-NOV-13" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "" (CHAR)
DEFINE _USER = "" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000100" (CHAR)
DEFINE _EDITOR = "Notepad" (CHAR)
Notice the _SQLPLUS_RELEASE. You reference this in SQLPLUS.
For example, you can do something like:
sqlplus -S /nolog<<EOF
prompt &_SQLPLUS_RELEASE
quit
EOF
You can just use command :
sqlplus -V
And you should get :
SQL*Plus: Release 18.0.0.0.0 - Production
Version 18.3.0.0.0
Or :
SQL*Plus: Release 19.0.0.0.0 - Production
Version 19.12.0.0.0
I don't think you can with an actual query. You may be able to get it with this:
SELECT
PROGRAM, MODULE
from v$session s
order by s.sid;
The Module column may contain the version number, it might not. It depends on the program. If memory serves correctly, sqlplus does not give this. For example, TOAD gives "TOAD Freeware 11.0.0.116"
> select &_sqlplus_release from dual;
old 1: select &_sqlplus_release from dual
new 1: select 1803000000 from dual
1803000000
----------
1803000000
You can also just connect to sqlplus through commande line. In LINUX you can do the following:
[orafresh#ljsrv1123 ~]$ sqlplus / as sysdba
Which will return:
SQL*Plus: Release 11.1.0.7.0 - Production on Fri Jul 14 12:47:36 2017
Copyright (c) 1982, 2008, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release
11.1.0.7.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options