Difficulty with incrementing Variables and using the results in a If/else statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difficulty with incrementing Variables and using the results in a If/else statement
# 1  
Old 12-18-2012
Oracle Difficulty with incrementing Variables and using the results in a If/else statement

Environment:

BASH version: GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.

OS: Oracle Solaris 10 9/10 s10s_u9wos_14a SPARC
Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
Assembled 11 August 2010

Script:

Code:
#!/usr/bin/bash

#-------------------------------------------------
# This sftp's the files to the remote server
#
#load global variables
#----------------------------------------------------------

Code:
DATE=`date +"%d%b%y,%T-"`
NEWER=/users/ACCOUNT/.newer
LOG=/users/ACCOUNT/aly_sftp.log
HOSTIS=`hostname`
EMAIL=account@email.com
SENT=0

#If hostname = production server then use one #account, if it equals anything else use the test account

Code:
if [ $HOSTIS = 'hostname' ]; then
  TOHOST='account@regular.host.com'
 else
 TOHOST='account@test.host.com'
fi

#grab the account used for the transfer and load just the domain name for logging later
Code:
TODOMAIN=`echo $TOHOST|cut -d @ -f2`


#check to see if there are any files named *.txt in the /transfer directory
#see if they are newer that the ~/.newer file and if they are send them via SFTP to $HOSTIS


Code:
for i in `find /transfer -name '*.txt' -newer $NEWER -print`
        do

#sftp any files found, if none found or all sent goto RESULT

Code:
sftp $TOHOST <<RESULT
                cd /inbound/
                lcd /transfer
                put $i
                bye

#make a log entry with a datetime stamp that each file was transmitted

Code:
echo $DATE$i "transmitted to "$TODOMAIN >>$LOG

#for each file sent increment $SENT by 1
Code:
 SENT=$((SENT+1))
RESULT

#If any files are sent email the user that the files were successfully sent else die.

Code:
if [ $SENT != 0 ]
then
        mailx -s "SFTP completed successfully" $EMAIL<$LOG
        echo $DATE"email for SFTP completed successfully sent">>$LOG
        touch -a -m $NEWER
        SENT=0
else
        touch -a -m $NEWER
        echo $DATE$NEWER "touched">>$LOG
fi
done

So this has been giving me fits and I just can't figure it out. I am sure it's a rookie mistake but I have been staring at it too long and can't find it. The bit I am having trouble with is the SENT variable increment and using to determine which part of the if statement to execute.

Doesn't matter how I change it SENT=0 when it gets to the second if statement and it always executes the first condition rather than the second.

It has something to do with global vs local variables I think but I can't figure out how to correctly configure it.

Any teaching would be appreciated.

Last edited by os2mac; 12-18-2012 at 10:56 AM.. Reason: to correct conditional syntax...
# 2  
Old 12-18-2012
Hi,
Code:
echo $DATE$i "transmitted to "$TODOMAIN >>$LOG
SENT=$((SENT+1))

is inside the here-document. It should be after the closing RESULT label


--
It also helps if the indenting is done correctly. BTW..
# 3  
Old 12-18-2012
Oracle I edited for comments on code clarity

Given my edit, does your comment still hold true?

if so, wouldn't $SENT always increment by 1 even if there were no files sent?

If that's the case, how would I code for that condition, basically what I am trying to do is keep track of whether files were sent and then send an email to the user notifying them of such else die.

and I'd welcome an example of "proper indention" Smilie

Thanks for the input.
# 4  
Old 12-18-2012
Yes my comment still holds true. Right now, $SENT will never be incremented, It needs to be placed outside the here document, and you would need to test if it can be incremented..

Last edited by Scrutinizer; 12-18-2012 at 07:37 AM..
# 5  
Old 12-18-2012
Thanks

so after YOU got $SENT incrementing, it still wouldn't send. Turns out the problem was the conditional was incorrectly stated for some reason rather than -eq or == I had to use !=.

also in this iteration it will send an email for EACH file. How could I set it up for sending only one file for all files at the end of the loop?

thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

2. Shell Programming and Scripting

How to Assign SQL Query Results to Variables in Linux?

Hi, I am new to linux... How to Assign SQL Query Results to Variables in Linux,i want ti generate it in param files, Can anyone please explain me. Ex: SQL> Select * from EMP; O/P: Emp_No Emp_Name 1 AAA 2 BBB 3 CCC and I want expected... (5 Replies)
Discussion started by: Sravana Kumar
5 Replies

3. Shell Programming and Scripting

Multiple Query Results to Variables

Hello, I am very new to shell scripting and I am not sure of how best to handle the following scenario. I need to query a list of values from a table. I need to store those results and use them to selectively delete values in yet another table in a separate database. I do know how to store the... (3 Replies)
Discussion started by: flowervz
3 Replies

4. Shell Programming and Scripting

SQL/Plus in a coprocess example. Also saves query results into shell variables

While assisting a forum member, I recommended running SQL/Plus in a coprocess (to make database connections and run a test script) for the duration of his script rather than starting/stopping it once for every row in a file he was processing. I recalled I made a coprocess example for folks at... (2 Replies)
Discussion started by: gary_w
2 Replies

5. Shell Programming and Scripting

Oracle Query results to be stored in variables using unix

I want to store the sql query output into a variable #!/bin/ksh ORACLE_SID=DB01; export ORACLE_SID; export FILE_PATH=/home/asg/Tmp # Order Checking echo " removing old files " rm $FILE_PATH/Malformed_Order.txt echo " Enter the Malformed Order ....!" read orders echo "Regrade... (5 Replies)
Discussion started by: Nareshp
5 Replies

6. Programming

incrementing variables in C++

Hello, what is the result of the below, and how does it work? int i = 5; cout << i++ * ++i << endl; cout << i << endl; (12 Replies)
Discussion started by: milhan
12 Replies

7. Shell Programming and Scripting

Oracle Query results to be stored in variables

Hi I would like to know if there is a way to just have one ORACLE connection established, using which we can execute different queries and store the results under different variables. For e.g the following uses to two silent ORACLE connection to store the result under two different... (4 Replies)
Discussion started by: ashokjaysiv
4 Replies

8. UNIX for Advanced & Expert Users

Set shell variables from SQLPLUS query results

Hi All, I needed to get the result of two sqlplus queris into shell variables. After days of looking for the ultimate solution to this problem.. i found this... sqlplus -s USER/PASS@DB <<EOF | awk '{if(NR==1) printf("%s ", $1); if(NR==2) printf("%s ", $1);}' | read VAR1 VAR2 set head off... (2 Replies)
Discussion started by: pranavagarwal
2 Replies

9. Shell Programming and Scripting

shell script help: sorting, incrementing environment variables and stuff

First: me == noob. Whats a good resource for shell script info cause I'm having trouble finding good info. I'm writing a shell script to automate the setup of a flash 'page flip'. My current code is below. the page flip takes an xml file of format <content> <pages... (1 Reply)
Discussion started by: secoif
1 Replies

10. UNIX for Dummies Questions & Answers

Pipe SQL select statement results to script

Hello I would like to perform a select from a oracle table and return those values to my shell script For example: site=head -1 $infile | cut -c1-15 | awk '{printf "s%", $0} sqlplus -s /nolog |& #Open pipe to sql select col1, col2, col3, col4 from oracle_table where col5 =... (6 Replies)
Discussion started by: houtakker
6 Replies
Login or Register to Ask a Question