Variables of executed script available in executing script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variables of executed script available in executing script
# 1  
Old 05-10-2010
Variables of executed script available in executing script

Hi,

I have a script get_DB_var.ksh which do a data base call and get some variables as below:

Code:
 
sqlplus -silent $user/$pass@dbname <<END
select col1,
         col2,
         col3
from table_name where col4=$1;
exit;
END

Now I want to access all these variables i.e. col1,col2,col3... in the script that executes get_DB_var.ksh as below:

Code:
 
cat set_DB_var.ksh
 
ksh get_DB_var.ksh 
echo $col1
echo $col2
echo $col3

is this possible? or do I have to write values of col1,col2,col3... in a file and then read it in set_DB_var.ksh?
Code:
 
get_DB_var.ksh 
---------------
sqlplus -silent $user/$pass@dbname <<END > var_file.txt
select col1,
         col2,
         col3
from table_name where col4=$1;
exit;
END
 
set_DB_var.ksh
---------------
while read var_file
do
echo $col1
echo $col2
echo $col3
done

What I want to know is that do we have some command to see the variables of the executed script?? the similar way we have "export" command to have variables available in the called script!

-dips
# 2  
Old 05-10-2010
Do you have variable assignments in get_DB_var.ksh ?

Code:
from table_name where col4=$1;

this doesn't imply that col4 is a variable and contains some value.

you must have something like this to get the values as echo $col4
Code:
col4="some_value"

if so, you can source the get_DB_var.ksh within set_DB_var.ksh.

inside set_DB_var.ksh,
Code:
. get_DB_var.ksh
echo $col4

# 3  
Old 05-10-2010
Not sure if I fully understood you, but if you can make sure in your SQL-Query, that there is no header and footer information in the output, you can execute it inside a for loop for example and work through the elements each like:

Code:
RESULT=`sqlplus -silent $user/$pass@dbname <<END
select col1,
         col2,
         col3
from table_name where col4=$1;
exit;
END`

for COL in $RESULT; do
     echo $COL #do something with them here
done

If you have spaces in the output per col, you might want to use a while read construct instead.

Last edited by zaxxon; 05-11-2010 at 04:17 AM.. Reason: typo which was no typo
# 4  
Old 05-10-2010
Hi anchal_khare,

By
Code:
from table_name where col4=$1;

I mean that script get_DB_var.ksh script takes an argument

Code:
./get_DB_var.ksh some_file_info_var

so the sql query
Code:
select col1,
         col2,
         col3
from table_name where col4=some_file_info_var;

will give all the information from the table_name about this file; which I have to access it in another scripts. And I am not allowed (by the design team) to source the script get_DB_var.ksh ! Smilie just allowed to execute it within the scripts which need those DB variables.

Hi zaxxon,
I some how want to have access to those variables in the script executing the get_DB_var.ksh script.
-dips
# 5  
Old 05-10-2010
Sorry to say that I am still confused about what you want. :-(

Another trial,

Code:
select a,
         b,
         c
from table_name where col4=$1;

from above text, which is inside get[...].ksh, and you are calling this from set[...].ksh:

Do you want something like that in the set[...].ksh ?

Code:
echo $col1 is a
echo $col2 is b
echo $col3 is c
echo $col4 is some_file_info_var # you are already having this.

if yes, you could do this with normal text processing and patter matching.
e.g grep,sed,awk,cut etc.

but please tell if this is your scenario so that we can help accordingly.
# 6  
Old 05-11-2010
If you want to use those variables in a subsequent script, you have to either export them and start the subsequent script from your main script or, you have to write those variables to a file, so that the 2nd script can read them.
# 7  
Old 05-11-2010
want sqlplus output with new line character

Hi,
now I am writing the sqlplus output to a file; as below:

Code:
sqlplus -s user/pass@DB <<END > result_file
set pagesize 0 linesize 10000 feedback off verify off heading off echo off
select
'EMP='||EMP||','||
'DEPT='||DEPT||','||
'JOB_NAME='||JOB_NAME
from employees where emp_id=$1;
exit;
END

The format of result_file will be as below:
Code:
EMP=1,DEPT=IT,JOB_NAME=SOFTWARE ENGINEER

but is there a way to get the format as below:
Code:
EMP=1
DEPT=IT
JOB_NAME=SOFTWARE ENGINEER

i.e. a new line char I tried something like this:
Code:
sqlplus -s user/pass@DB <<END > result_file
set pagesize 0 linesize 10000 feedback off verify off heading off echo off
select
'EMP='||EMP||\n||
'DEPT='||DEPT||\n||
'JOB_NAME='||JOB_NAME
from employees where emp_id=$1;
exit;
END

also <CR> instead of \n but to no avail. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script is not getting executed

Hi All, I have written a programme where i need to work in one session but when that session get executed it doesnt execute other programme till I write exit. Below is the script #!/bin/bash echo 'Going to start' exectrusteduser.com sh.com cd /UAT_Logs/CDCI_LOGS/SEEDADM pwd echo... (6 Replies)
Discussion started by: ripudaman.singh
6 Replies

2. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

3. Shell Programming and Scripting

INIT Script Getting Executed Twice?

Hello All, I copied and pasted the "/etc/init.d/skeleton" file to a new one so I could create my own init script for a program. Basically the ONLY edit I made to the skeleton "template" so far was to search and replace "FOO" with "snort". *NOTE: I know there are a bunch of snort init scripts... (6 Replies)
Discussion started by: mrm5102
6 Replies

4. Shell Programming and Scripting

Shell script not getting executed

Hi As per my requirement when I run . ./file.sh am getting the following error -bash:ELF: command not found when i execute as ./file.sh it is getting executed.How to resolve this. Thanks in advance. (3 Replies)
Discussion started by: pracheth
3 Replies

5. Shell Programming and Scripting

Shell script executed from Informatica ETL tool is spawning 2 processes for one script

Hi, I am having a shell script which has a while loop as shown below. while do sleep 60 done I am executing this script from Informatica ETL tool command task from where we can execute UNIX commands/scripts. When i do that, i am seeing 2 processes getting started for one script... (2 Replies)
Discussion started by: chekusi
2 Replies

6. UNIX for Dummies Questions & Answers

Script dosent exits after executing the script

Hi i wrote a script which dosent exists after executing any help #!/bin/bash netstat -ptlen | grep 10000 if ; then echo "Hive Thrift server is running" exit 0 else echo "Hive Thrift server is down Trying to Bring up the service" | mail -s "ALERT" team@domain.com `nohup hive... (7 Replies)
Discussion started by: vikatakavi
7 Replies

7. Shell Programming and Scripting

script has been executed successfully or not??

Guys, How can we know whether a script has been executed successfully or not ? We dont have any log directories, and we are not given a chance to modify the script. Could someone help me out with this Thanks (2 Replies)
Discussion started by: bobby1015
2 Replies

8. AIX

Script not getting executed via cron but executes when executed manually.

Hi Script not getting executed via cron but executes successfully when executed manually. Please assist cbspsap01(appuser) /app/scripts > cat restart.sh #!/bin/ksh cd /app/bin date >>logfile.out echo "Restart has been started....." >>logfile.out date >>logfile.out initfnsw -y restart... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

9. UNIX for Advanced & Expert Users

Executing a shell script from windows;script present in unix

I need to execute a shell script kept in unix machine from windows. User id, password area available. For eg. There's a shell script wich moves all the logs kept in my home directory to a directory named LOGS. Now i need to get this done through windows; either using a batch file, or java... (4 Replies)
Discussion started by: rajneesh_kapoor
4 Replies

10. Shell Programming and Scripting

How to know who executed a script?

Dear all I would like to capture who executed a script to a script's variable (i.e. testing.sh), so I can save it to a log file. testing.sh #! bin/ksh user=`<< code here >>` // get the info below in RED color <<main logic>> echo "$user execute testing.sh on `date`" >> testing.log ... (2 Replies)
Discussion started by: on9west
2 Replies
Login or Register to Ask a Question