Need to pass value to if condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to pass value to if condition
# 1  
Old 10-31-2012
Need to pass value to if condition

Hi Team,

I want to run multiple sql select command after loggin to sqlplus and need to pass the value to variable to check the condition. Please let me know how to do this.
----------------------------
When I am running the below script I am getting the below error
Code:
SP2-0734: unknown command beginning "return :va..." - rest of line ignored.
SP2-0734: unknown command beginning "return :va..." - rest of line ignored.

a2.sh: test: argument expected
-----------------------------
Example:
Code:
sqlplus -s system/xxxx << EOF
set pages 0
set head off
set feed off
variable var1 varchar2(10);
variable var2 varchar2(10);
begin
select num_rows into :var1 from DBA_TAB_PARTITIONS where TABLE_NAME='TABLE1' and table_OWNER='AAA'
and PARTITION_POSITION=(select max(PARTITION_POSITION)-3 from DBA_TAB_PARTITIONS where TABLE_NAME='TABLE1' and table_OWNER='AAA');
select num_rows into :var2 from DBA_TAB_PARTITIONS where TABLE_NAME='TABLE2' and table_OWNER='PPP'
and PARTITION_POSITION=(select max(PARTITION_POSITION)-3 from DBA_TAB_PARTITIONS where TABLE_NAME='TABLE2' and table_OWNER='PPP');
end;
/	

return :var1;
return :var2;
EOF
echo $var1
if [ $var1 -gt 0 ]; then
echo "condition 1......." 
fi

echo $var2
if [ $var2 -gt 0 ]; then
echo "condition 2...." 
fi


Last edited by Franklin52; 10-31-2012 at 05:47 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-31-2012
This is not Pro*C. You cannot have bound variables (:var).
This is one way of many to do this:
Code:
#!/bin/ksh
sqlplus -s system/xxxx << EOF >myfile.txt
set pages 0
set head off
set feed off
select num_rows 
      from DBA_TAB_PARTITIONS 
      where TABLE_NAME='TABLE1' 
             and table_OWNER='AAA'
            and PARTITION_POSITION=
            (select max(PARTITION_POSITION)-3 
              from DBA_TAB_PARTITIONS 
              where TABLE_NAME='TABLE1' and table_OWNER='AAA');
select num_rows
      from DBA_TAB_PARTITIONS 
      where TABLE_NAME='TABLE2' 
      and table_OWNER='PPP'
and PARTITION_POSITION=(select max(PARTITION_POSITION)-3 from DBA_TAB_PARTITIONS where TABLE_NAME='TABLE2' and table_OWNER='PPP');
EOF
cnt=0;
while read rec 
do
  arr[$cnt]="$rec"
  cnt=$(( $cnt + 1 ))
done
# you now have an array arr with 2 elements
echo "first element = ${arr[0]}"
echo "second element = ${arr[1]}"

You really should skip using shell and write the whole thing in PL/SQL, which supports if then else end if;
And you can use local variables (without the :var syntax);
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass IF condition via shell varibale in awk?

Hello, I have one query regarding passing IF condition shell variable inside awk. Here is the case- File content of keydefn.exp 201~2~LM Limit 02-current value~Limit 02 ~Limit02~Current~Value ~N~Y~S~0~9999999 201~3~LM Limit 03-current value~Limit... (2 Replies)
Discussion started by: anillambait
2 Replies

2. Shell Programming and Scripting

Pass the value from pipe

am trying to pass the date calculated to variable so that i can use the same for my further reporting and tracking of the changes as per the variable value. System = SunOS Release = 5.9 KernelID = Generic_122300-61 date '+%m %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case... (5 Replies)
Discussion started by: pradeep84in
5 Replies

3. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

Pass parameter

Hi, I have following for loop , please let me know how to get ${TXP_EXT_TABLE_${i}_SQL} parameter with 1DAY and 7DAY values. for i in 1DAY 7DAY do ${NZSQL_DIR}/nzsql -h ${HOST} -time -v ON_ERROR_STOP=1 -f ${SQL_DIR}/${TXP_EXT_TABLE_${i}_SQL} > ${TMP_LOG_FILE} 2>&1 done ... (4 Replies)
Discussion started by: sandy162
4 Replies

5. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. Emergency UNIX and Linux Support

Pass two parameters

Hi I have a batch file aaa.exe which needs two input parameters: Usually the command's format likes aaa 555 10000 But I want to use parameters to do it. aaa $1 $2 These two parameters come from a text file list.txt 41800497 41801375 41814783 41816135 41814930 41816135 41819987 41820843... (4 Replies)
Discussion started by: zhshqzyc
4 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. UNIX for Dummies Questions & Answers

How to pass the parameter value to a... ?

Hello I have a simple code like this one: #!/bin/ksh VER=$1 cat /usr/text | while read line do echo $line done Let's say $1=1.0.0 and the contents of text is: abcd.cfg asdf I would like the output to be like this abcd1.0.0.cfg asdf1.0.0 I am thinking of passing the... (5 Replies)
Discussion started by: khestoi
5 Replies

9. UNIX for Advanced & Expert Users

pf not working properly even with only "pass in all" and "pass out all" rules

i have two rules in my pf.conf file, "pass in all" and "pass out all" i was having issues with getting pf working to begin with, so i went with starting from nothing and working on up. i have an ultrasparc ultra1 200e, with an added 4-port fast ethernet sbus card, running "3.4 GENERIC#85... (4 Replies)
Discussion started by: xyyz
4 Replies

10. UNIX for Dummies Questions & Answers

Login and Pass

hi, I'm a dumbass and I created a llogin with no passowrd so when it asks me for a login and password I cannot do anything... and for somereason I cannot find the drive to format it... (1 Reply)
Discussion started by: Special K
1 Replies
Login or Register to Ask a Question