Displaying command return in one line

Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Displaying command return in one line

Hello all

I have a query (SQL) that returns a rather long field from an Oracle database. The field in question is defined on 400 characters but all these 400 cannot be displayed by the echo command. Thus when I launch the following command:
Code:
echo "SELECT FIELD01 FROM TABLE_NAME;" | sqlplus -s <login>/<password>@<CONNECTION_NAME>

I get the folloiwng:
Code:
https://dec.parent_groupe.com/C10/cg...=cognosViewer&
ui.action=run&ui.object=storeID(%22i18DCC8B926BB446BA88844C3DFDED8A6%22)&ui.name
=Contr%c3%b4les%20ICARE&run.outputFormat=&run.prompt=true&cv.header=false&ui.bac
kURL=%2fC10%2fcps4%2fportlets%2fcommon%2fclose.html

Where as what I need is all of the above on one line.

Can anyone please help me out?

Thanks a lot

S. BASU

Last edited by Franklin52; 01-14-2015 at 10:56 AM.. Reason: Please use code tags
# 2  
echo is not producing any output - sqlplus is.

Try:
Code:
sqloutput=$(echo "SELECT FIELD01 FROM TABLE_NAME;" | sqlplus -s <login>/<password>@<CONNECTION_NAME>)
echo $sqloutput

# 3  
Thanks for that Carlo. I'm however stuck because echo $sqloutput doesn't return anything except a '>'

Furthermore, I can't seem to get out of that prompt
# 4  
Then you might have lost a quote (or so) somewhere. I guess it's the prompt...

For your problem: can't you set the line length somewhere in sqlplus? Or column width? To 400?
# 5  
Quote:
Originally Posted by S. BASU
Hello all

I have a query (SQL) that returns a rather long field from an Oracle database. The field in question is defined on 400 characters but all these 400 cannot be displayed by the echo command. Thus when I launch the following command:

echo "SELECT FIELD01 FROM TABLE_NAME;" | sqlplus -s <login>/<password>@<CONNECTION_NAME> I get the folloiwng:

https://dec.parent_groupe.com/C10/cg...=cognosViewer&
ui.action=run&ui.object=storeID(%22i18DCC8B926BB446BA88844C3DFDED8A6%22)&ui.name
=Contr%c3%b4les%20ICARE&run.outputFormat=&run.prompt=true&cv.header=false&ui.bac
kURL=%2fC10%2fcps4%2fportlets%2fcommon%2fclose.html

Where as what I need is all of the above on one line.

Can anyone please help me out?

Thanks a lot

S. BASU
As an Oracle DBA, I'd strongly recommend some changes to the way you're doing things.

don't use:
Code:
sqlplus -s <login>/<password>@<CONNECTION_NAME>

That makes your password visible to anyone logged onto the system via
Code:
ps -ef

command

Instead ... consider doing something like:

Code:
sqlplus -s /nolog << EOF         
   connect <login>/<password>@<CONNECTION_NAME>
   @your_sql_script.sql
   exit
EOF

The advantages of using this structure over what you have are many:

1) keeps your password slightly more protected from prying eyes. it's much harder to sniff out (probably not impossible, but I'm not aware of a simple way to do it at this time).

2) you can build sql scripts in stand alone files, that actually "LOOK" like sql scripts. So they can have "set" commands at the start, var commands set if needed, parameters passed into them (using "&1", "&2", etc to catch them), and many queries if need be. Or as you need, even a simple, single SQL.

3) you can wrap unix braces around that block to capture all output in log file, variable, or whatever you need:

ie:
Code:
{
sqlplus -s /nolog << EOF         
   connect <login>/<password>@<CONNECTION_NAME>
   @your_sql_script.sql
   exit
EOF
}   > $my_log_file.log  2>&1

or you can assign it to a variable:

Code:
my_var = 
$(sqlplus -s /nolog << EOF         
   connect <login>/<password>@<CONNECTION_NAME>
   @your_sql_script.sql
   exit
EOF)

(that one's untested, sorry, I don't use that format alot myself)

but yeah, two main points are:

a) don't expose your password - use the connect keyword to delay the login and keep the password off the command line, or use External Ids, or some other login mechanism to help protect your password.
b) keep your sql in standalone sql scripts so you can easily test/modify it. Then all you need is "pass it through" to sql, via some wrapper, or such as above.
# 6  
Tried setting the line size using myriad commands. However I keep getting the error message 'linesize option not a valid number' which is really weird since I am giving 400 which is definitely a valid number.

To be clear I am launching both queries together which is:

set lines 400;SELECT <column_name> FROM <table_name>;

This is because I ideally would not like to again have to create a script file (since that would mean an extra component.

Anywhere specific that I could be going wrong?
# 7  
Quote:
Originally Posted by S. BASU
Tried setting the line size using myriad commands. However I keep getting the error message 'linesize option not a valid number' which is really weird since I am giving 400 which is definitely a valid number.

To be clear I am launching both queries together which is:

set lines 400;SELECT <column_name> FROM <table_name>;

This is because I ideally would not like to again have to create a script file (since that would mean an extra component.

Anywhere specific that I could be going wrong?
Irregardless of your final destination, you have a problem, and are troubleshooting.

Create a script file for test purposes .. and see how it behaves ...
You just might find how much easier it is to troubleshoot Smilie

Once you have things figured out, you can always splice it back into your "one-liner" solution if you need it Smilie

[edit]
fully working sample ... try to setup this working sample first .. then change "my.sql" to use your query instead of the select * from dual .. and see what changes.

Code:
unix > more *
::::::::::::::
my.sql
::::::::::::::
set linesize 400
set pause off

select *
  from dual;

::::::::::::::
sql.ksh
::::::::::::::
{
sqlplus -s /nolog << EOF
   connect myid/password@dbname
   @./my.sql
   exit
EOF
}   >./my_log.log    2>&1

unix > ksh sql.ksh
unix > ls -ltr
total 6
-rw-r-----   1 ditto    group1         57 Jan 13 09:48 my.sql
-rw-r-----   1 ditto    group1        121 Jan 13 09:51 sql.ksh
-rw-r-----   1 ditto    group1         83 Jan 13 09:51 my_log.log
unix > more my_log.log

D
-
X

unix >

[/edit]

Last edited by Ditto; 01-13-2015 at 11:54 AM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #84
Difficulty: Easy
Windows 3.1, Windows 95, Windows 98, and Windows ME were all based on DOS.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Command to add Carriage Return and Line Feed

Hello, Can someone please share a Simple AWK command to append Carriage Return & Line Feed to the end of the file, If the Carriage Return & Line Feed does not exist ! Thanks (16 Replies)
Discussion started by: rosebud123
16 Replies

2. Shell Programming and Scripting

Why sed command deletes last line in a file if no carriage return?

Hi I am using sed command to make SCORE=somevalue to SCORE=blank in a file. Please see the attached lastline.txt file. After executing the below command on the file, it removes the last line. cat lastline.txt | sed 's/SCORE=.*$/SCORE=/g' > newfile.txt Why does sed command remove the... (3 Replies)
Discussion started by: ashok.k
3 Replies

3. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

4. Shell Programming and Scripting

How to get the return code of subroutines executed as standalone as command line in Perl ?

How to do I get the return code of a subroutine in a perl module if invoke the subroutine as standalone, I have an module say TestExit.pm and in that i have a subroutine say myTest() which is returns 12, if i were to call the subroutine from command line like CASE:1 ( Without an explict... (2 Replies)
Discussion started by: ennstate
2 Replies

5. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

6. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

7. UNIX for Dummies Questions & Answers

Displaying Return Codes

This is a high-level explanation, if more details are needed, please do not hesitate to ask. I have a set of .ctl files which I want to execute: AV1.ctl AV2.ctl AV3.ctl I have a script which has a for loop in it: for filename in AV1 AV2 AV3 do . execute_another_script.sh done ... (2 Replies)
Discussion started by: hern14
2 Replies

8. UNIX for Dummies Questions & Answers

displaying mutliple fields on command line

This is probably the dumbest question you guys can get, but I'm trying, as a complete noob, to display the unix calendar for all the months without Saturday and Sunday showing. How can I remove those fields without having to type all the fields in individually such as: cal -y | awk '{print $2,... (3 Replies)
Discussion started by: Trellot
3 Replies

9. Shell Programming and Scripting

displaying the path in the command line

Hi all, Does anyone know how to ammend the .cshrc file in $HOME for your session to display the path as part of the command line? So that I dont need to keep on typing pwd to see where I am? thanks Ocelot (2 Replies)
Discussion started by: ocelot
2 Replies

10. UNIX for Dummies Questions & Answers

displaying the first line?

how do i display just the first line of a file with the cat command or any command for that matter (4 Replies)
Discussion started by: imuuk
4 Replies

Featured Tech Videos