[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

How to pass variables from another file into shell script?

I like to pass variables from another file into shell script and those variables will be used in a sql block to update a database table whenever that sql block is run inside the shell script. One line of variable will be passed to the shell script at a time starting from the very top one. This process will continue in a loop until all the variable lines are passed.

Examples of variable file (var.txt) and shell script are below. All I need to do pass variables date parameter 1:date parameter 2 from var.txt file to var1 and var2 respectively into below shell script in a loop process. Please advise how I pass variables and modify below shell script with loop.

My variable file is following as it has date parameters:

var.txt file
-------------------
20091130:20091231
20091231:20100131
20100131:20100228

and so on .............................

Shell script is following
-----------------------------

############################################################################
# Oracle Environment Variables
############################################################################
. $HOME/.profile

ORA_USER=user
ORA_PWD=password
############################################################################
# Local Variables
############################################################################
run_date=`date '+%Y%m%d'`
run_time=`date '+%H%M%S'`

var1=20091130 --should come from var.txt file from top line first parameter
var2=20091231 --should come from var.txt file from top line second parameter

shell_dir=$HOME/analytics/bin
sql_dir=$HOME/analytics/sql
tmp_dir=$HOME/analytics/tmp
log_dir=$HOME/analytics/log

progName=${0##*/}
sql_file=${sql_dir}/${progName%%.*}.sql
log_file=${log_dir}/${progName%%.*}_${run_date}${run_time}.log

start_time=`date +%Y-%m-%d-%H:%M:%S`

############################################################################
# Begin Main
############################################################################

echo "NextGen CA Segmentation Snapshot process update config dates $start_time." >> ${log_file}
echo "*************************************************************************" >> ${log_file}


sqlplus -s ${ORA_USER}/${ORA_PWD}@ADWPRD <<EOF >> ${log_file}
set echo off;
set heading off;
set feedback off;
whenever sqlerror exit failure;

update birpt.ng_config
set value=$var1
where config ='NG_CA_SEGMNT_HIST_END_DATE';

commit;

update birpt.ng_config
set value=$var2
where config ='NG_CA_SEGMNT_HIST_EFF_DATE';

commit;

exec proc1;
exec proc2;
exec proc3;

exit;
EOF

end_time=`date +%Y-%m-%d-%H:%M:%S`

exit $?
0
IT_ETL
Asked:
IT_ETL
3 Solutions
 
Gerald26Commented:
Hello, lets consider these 2 little scripts and this variable file:

It should help you understand how variables work (double %%, and arguments  after calling a bat)

Regards


VARIABLES.TXT
------------------
1111:2222
3333:4444

Open in new window


Oracle.bat
-------------
@echo off
echo -----------------------
echo Var1 = %1 and Var2 = %2
echo -----------------------

Open in new window




Launch.bat (the launcher you need to run the script several times)
-----------------------------------------------------------------------------
for /F "eol=; tokens=1,2* delims=:" %%i in (variables.txt) do (@echo Reading a line & call oracle.bat %%i %%j)

Open in new window

0
 
Gerald26Commented:
Ohhh!! I'm sorry, I thought it was about DOS scripting !!!

I'm gonna try to redo it immediately for Sh !

sorry again !
0
 
johnsoneSenior Oracle DBACommented:
Try this.

You will notice the addition of the while to read from the file.  Also the 2 lines that will parse it out into the 2 variables.  There may be better or more efficient ways to do this, but this way works.

Also, not sure what you are trying to accomplish with the exit line, but it probably isn't doing what you want it to.  It is returning the status code from the date command, which probably doesn't change.
############################################################################
# Oracle Environment Variables
############################################################################
. $HOME/.profile

ORA_USER=user
ORA_PWD=password
############################################################################
# Local Variables
############################################################################
run_date=`date '+%Y%m%d'`
run_time=`date '+%H%M%S'`

shell_dir=$HOME/analytics/bin
sql_dir=$HOME/analytics/sql
tmp_dir=$HOME/analytics/tmp
log_dir=$HOME/analytics/log

progName=${0##*/}
sql_file=${sql_dir}/${progName%%.*}.sql
log_file=${log_dir}/${progName%%.*}_${run_date}${run_time}.log

start_time=`date +%Y-%m-%d-%H:%M:%S`

############################################################################
# Begin Main
############################################################################

echo "NextGen CA Segmentation Snapshot process update config dates $start_time." >> ${log_file}
echo "*************************************************************************" >> ${log_file}

while read line
do
var1=`echo $line | cut -f1 -d:`
var2=`echo $line | cut -f2 -d:`

sqlplus -s ${ORA_USER}/${ORA_PWD}@ADWPRD <<EOF >> ${log_file}
set echo off; 
set heading off;
set feedback off;
whenever sqlerror exit failure;

update birpt.ng_config
set value=$var1
where config ='NG_CA_SEGMNT_HIST_END_DATE';

commit;

update birpt.ng_config
set value=$var2
where config ='NG_CA_SEGMNT_HIST_EFF_DATE';

commit;

exec proc1;
exec proc2;
exec proc3;

exit;
EOF

done < var.txt

end_time=`date +%Y-%m-%d-%H:%M:%S`

exit $?

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Gerald26Commented:

perfect, I was looking for a way that wouldnt modify original source code, but it's pretty clean

I ended up with a standalone launcher script like
cat var.txt |awk -F':' '{system("./oracle.sh " $1 " "  $2);}'

Open in new window


and just changing this in the main script:
var1=$1
var2=$2

Open in new window

0
 
TintinCommented:
Just wrap in a while loop, ie:


while IFS=":"  read var1 var2
do
   [your sql block]
done <var.txt

Open in new window

0
 
IT_ETLAuthor Commented:
Hi Gerald26,

Just wanted to confirm, are you sayng below line should be part of the main shell script or write another shell script and below line in the script will execute main shell script which is oracle.sh.

cat var.txt |awk -F':' '{system("./oracle.sh " $1 " "  $2);}'

Also could you briefly explain below command. What do you mean by word "system" in the below command? Is that the name of the unix server? Is double quote missing in the below command for example " $1 " " $2".

awk -F':' '{system("./oracle.sh " $1 " "  $2);}'
0
 
Gerald26Commented:
Hello,

Indeed, the command line is another shell script that browse the var.txt file and calls the main script that i named 'oracle.sh'. My solution is based on Awk command. Tintin's solution is the same without using Awk, then I must admit it's even better.
Johnsone's solution is a modification of  the main script and is for me the best one so far.

But I will answer your question about Awk

Cat var.txt outputs the content of the file. the Pipe "| " will redirect the output to Awk command

Awk -F':'  

Open in new window

means : start Awk program to analyze the output file and consider semicolon as a separator
'{system("./oracle.sh "  

Open in new window

     that  will ask AWK to execute a shell script called oracle.sh. This is your main script. The space before the double quote is wanted because I am about to pass arguments to this command.
$1 " "  $2

Open in new window

gets the 2 variables gotten by Awk when parsing a line with a semicolon separator, and add them after the oracle.sh command. Add a space between them (" ")
)'}'

Open in new window

close the code block and the awk command

There is no missing double quote.

An easier example for Awk can be :

echo "Hello This is a test. Goodbye" | awk -F" " '{print $2 " " $3 " " $5}' 

Open in new window


This will browse the echo line and get items separated by a space.
It will then display item 2, 3 and 5 and will output "This is test."

Gerald

0
 
IT_ETLAuthor Commented:
Thanks Gerald! Your above comments were helpful.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now