The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
put value of multiple sql statements into unix variables sw@pnil Shell Programming and Scripting 1 11-11-2008 11:49 AM
Unix passing environmental Variables belfastbelle UNIX for Dummies Questions & Answers 7 09-15-2008 08:40 PM
comparing 2 string variables in unix katdaniel16 UNIX for Dummies Questions & Answers 17 07-23-2008 03:25 AM
accessing unix variables in oracle ravi raj kumar High Level Programming 1 02-06-2008 11:05 AM
by using c++ how to set environment variables in unix sada@123 UNIX for Advanced & Expert Users 1 03-21-2006 01:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-06-2009
djehres djehres is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 4
unix variables from sql / pl/sql

How do I dynamically assign the below output to unix shell variables
so I can build a menu in a shell script?

Example:

var1 = 1 var2= SYSTEM
var3 = 2 var4= UNDOTBS1

and so on, then in the shell script I can use the variables to build a menu.




set serveroutput on

declare
v_counter number(2):=0;
v_tbs dba_tablespaces.tablespace_name%type;

cursor tbs_cur is
select tablespace_name
from dba_tablespaces
where tablespace_name not in ('TEMP');

begin
open tbs_cur;
dbms_output.put_line('Number'||' '||'Tablespace Name');
loop
exit when tbs_cur%notfound;
fetch tbs_cur into v_tbs;
v_counter := v_counter+1;
exit when tbs_cur%notfound ;
dbms_output.put_line(v_counter||' '||v_tbs);
end loop;
close tbs_cur;
end;
/


SQL> /
Number Tablespace Name
1 SYSTEM
2 UNDOTBS1
3 SYSAUX
4 USERS
5 MAINT
6 MAINT_IDX
7 OFP
8 OFP_IDX
9 TMX
10 TMX_IDX

PL/SQL procedure successfully completed.
  #2 (permalink)  
Old 05-06-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,813
one way:

Code:
#!/bin/ksh

sqlplus -s user/pswd@somedb <<EOF > outputfile.lis
set serveroutput on

declare
v_counter number(2):=0;
v_tbs dba_tablespaces.tablespace_name%type;

cursor tbs_cur is
select tablespace_name
from dba_tablespaces
where tablespace_name not in ('TEMP');

begin
open tbs_cur;
dbms_output.put_line('Number'||' '||'Tablespace Name');
loop
exit when tbs_cur%notfound;
fetch tbs_cur into v_tbs;
v_counter := v_counter+1;
exit when tbs_cur%notfound ;
dbms_output.put_line(v_counter||' '||v_tbs);
end loop;
close tbs_cur;
end;
/
exit
EOF

while read num val
do
   arr[$num]="$val"
done < outputfile.lis

You now have an array: arr
  #3 (permalink)  
Old 05-07-2009
rwuerth rwuerth is offline
Registered User
  
 

Join Date: Jan 2009
Location: Va. Beach
Posts: 72
Quote:
How do I dynamically assign the below output to unix shell variables
so I can build a menu in a shell script?
When I want to dynamically create menus in shell scripts from data in an Oracle db I do something like the following:


Code:
#!/bin/ksh

function getTabsp {
  sqlplus -S <logon/pass> <<_EOD
  set echo off
  column tablespace_name format a30
  set feedback off
  set heading off
  set pagesize 0
  select tablespace_name 
  from dba_tablespaces
  where tablespace_name not in ('TEMP');
  exit
_EOD
}

PS3="Which Tablespace? "
select tabsp in $(getTabsp)
do
  if [[ -z $tabsp ]]; then
    <error processing here>>
  else
     <$tabsp equal the user's choice of tablespace names>
     <$REPLY equals the number of that choice>
     <do whatever you need with those values>
  fi
done

If you need to know ahead of time what the number of the choice will be (it's assigned automatically by the 'select' statement, you will have to make sure you can use the sql statement to properly order your results with 'order by'. You can use a different column than "tablepsace_name" to 'order by' if you select it also, but use a

Code:
column <name> noprint

before the select statement, this way the function only returns the tablespace_name column.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0