If Condition Issue in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If Condition Issue in UNIX
# 1  
Old 08-07-2013
Linux If Condition Issue in UNIX

Hi

I am trying to do a "IF" Condition in UNIX where we compare EACH file size in a directory with a SIZE (Parameter passed)

If Each File size EXCEEDS parameter passed SIZE then we manipulate the file.
Somehow the IF condition do not work ?? (is this Variable decalration issue ??)

Code:
 
APPS_USER_PWD=$1
APPS_USER_ID=$2
APPS_USER_NAME=$3
REQUEST_ID=$4
DIR=$5
FILE_NAME=$6
FILE_SIZE=$7
############
echo "DIRECTORY passed to the Program "$DIR
echo " "
echo "File Name passed to the program "$FILE_NAME
echo " "
echo "File Size passed to the program "$FILE_SIZE
XML_PATH_FLAG=`sqlplus -s $APPS_USER_PWD<<EOF
set head off 
set feedback off 
set echo off 
set term off 
set pages 0
set linesize 9000 
set trims on
select directory_path from dba_directories where directory_name = '${DIR}';
EXIT
EOF`
echo " "
echo "The Path Obtained to pick up Order XML Files is $XML_PATH_FLAG"
echo " "
echo "Permissions changing on XML files "
echo " "
cd $XML_PATH_FLAG
chmod 777 *.xml
echo " "
echo "Permission Changes Completed "
 
# To Loop in the Direcorty path for each file one at a time & modify each file
 if [ "X$FILE_NAME" == "X" ] ; then # Only Directory is passed , File name echo "FILE_NAME parameter not passed ....."
echo $XML_PATH_FLAG*.xml
for file in $XML_PATH_FLAG*.xml
do 
actual_size=`du $file` # getting each file Size 
 
echo " OUTSIDE CONDITION file_size is " $actual_size " and parameter_size echo  " sent is " $FILE_SIZE " for " $file 
 
if [ $actual_size > $FILE_SIZE ] ; then ### we are comapring each file size with PARAMETER passed constant SIZE ###CULPRIT??
##THE ABOVE CONDITION do not work ###
sed -i $file -e 's/<\/ItemOut>/&\n/g;' 
 
fi
done
fi

# 2  
Old 08-07-2013
Code:
if [ $actual_size -gt $FILE_SIZE ]

# 3  
Old 08-07-2013
your actual_size=`du $file` is giving you more data than you expect ... see 2nd line for fix ...
Code:
root@solarisgeek # du /etc/shadow
4     /etc/shadow
root@solarisgeek # du /etc/shadow | awk '{print $1}'
4
root@solarisgeek #

This User Gave Thanks to Just Ice For This Post:
# 4  
Old 08-07-2013
The -gt is giving me an unknown operator error

---------- Post updated at 11:49 AM ---------- Previous update was at 11:47 AM ----------

Quote:
Originally Posted by wisecracker
Code:
if [ $actual_size -gt $FILE_SIZE ]

Hello WiseCraker

THis gives me unknown operator error

---------- Post updated at 11:59 AM ---------- Previous update was at 11:49 AM ----------

Quote:
Originally Posted by wisecracker
Code:
if [ $actual_size -gt $FILE_SIZE ]

Quote:
Originally Posted by Just Ice
your actual_size=`du $file` is giving you more data than you expect ... see 2nd line for fix ...
Code:
root@solarisgeek # du /etc/shadow
4     /etc/shadow
root@solarisgeek # du /etc/shadow | awk '{print $1}'
4
root@solarisgeek #

hello Iceguy
Thanks this helped me a lot SmilieSmilie

---------- Post updated at 12:01 PM ---------- Previous update was at 11:59 AM ----------

hi Wise Craker

This worked with Quotes
"$var" -gt "$var1"
# 5  
Old 08-07-2013
Which shell are you using?
I didn't read your code just the "if" statement...

This is using the "/bin/bash --posix" structure...
Code:
Last login: Wed Aug  7 18:09:33 on ttys000
AMIGA:barrywalker~> bash --posix
AMIGA:barrywalker~> x=10
AMIGA:barrywalker~> y=20
AMIGA:barrywalker~> if [ $x > $y ]; then printf "This is an error.\n"; fi
This is an error.
AMIGA:barrywalker~> # Above line as predicted.
AMIGA:barrywalker~> if [ $x -gt $y ]; then printf "No display.\n"; fi
AMIGA:barrywalker~> # Above line as predicted.
AMIGA:barrywalker~> if [ $x < $y ]; then printf "This is an error.\n"; fi
This is an error.
AMIGA:barrywalker~> # Notice this is the same as lines 5 and 6 but now "<"...
AMIGA:barrywalker~> if [ $x -lt $y ]; then printf "Display.\n"; fi
Display.
AMIGA:barrywalker~> # Above line as predicted.
AMIGA:barrywalker~> if [ "$x" -lt "$y" ]; then printf "Display.\n"; fi
Display.
AMIGA:barrywalker~> if [ "$x" -gt "$y" ]; then printf "No display.\n"; fi
AMIGA:barrywalker~> exit
exit
AMIGA:barrywalker~> exit
logout


[Process completed]

# 6  
Old 08-07-2013
-ksh is what I am using
# 7  
Old 08-08-2013
I have an observation that your user credentials for SQL will be displayed if anyone lists your process. Could you consider butting the credentials in the 'here' document, e.g.:-
Code:
XML_PATH_FLAG=`sqlplus -s <<-EOSQL
$APPS_USER_PWD
set head off 
set feedback off 
set echo off
:
:
:

Could you consider changing your test conditions from:-
Code:
if [ "X$FILE_NAME" == "X" ]

to
Code:
if [ -z "$FILE_NAME" ]                 # Is string null?

... .and
Code:
if [ $actual_size > $FILE_SIZE ]

to
Code:
if [ $actual_size -gt $FILE_SIZE ]     # Is 1st value greater than 2nd

To use this as an arithmetic comparison, you will have to be certain that both values are:-
  1. Numeric
  2. Not null
Perhaps this may help:-
Code:
typeset -i actual_size FILE_SIZE

You script will report an error if you are assigning non-numeric to the variables listed, which may highlight the real source of the problem.



i hope that these suggestions help,

Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Solaris 11 ksh93 if condition issue

Solaris 11 ksh93 if condition issue Has anyone run into issues with Solaris 11 with ksh93 if condition where it intermittently return wrong return code? We did not see this issue in Solaris 10 with ksh88 Any thoughts? Thanks! Solaris version: SunOS t52-ccc-28 5.11 11.2... (4 Replies)
Discussion started by: nugent
4 Replies

2. Shell Programming and Scripting

Issue with IF condition

Getting syntax error when the condition after -o returns more than one file if then echo "Found" fi output: =>ls -l total 5 -rw-rw-r-- 1 wes wes 0 Jun 25 04:51 a.done -rw-rw-r-- 1 wes wes 0 Jun 25 04:51 a.zip -rw-rw-r-- 1 wes wes ... (13 Replies)
Discussion started by: anijan
13 Replies

3. Shell Programming and Scripting

Condition checking issue while if

hi, i am using a simple condition end_ct=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID << EOF select description from bravo_statistics where trunc(time_stamp)=trunc(sysdate)-1 and description='END CAT'; EOF` echo $end_ct; echo... (30 Replies)
Discussion started by: lovelysethii
30 Replies

4. Shell Programming and Scripting

if condition issue

Hi, I have prepared the below shell script for one of the requirement which accesses Informatica application and performs certain functions. But for some reason, if condition fails and scripts stops abruptly with the below error. Please let me know the where the problem is #!/bin/ksh ... (2 Replies)
Discussion started by: svajhala
2 Replies

5. Shell Programming and Scripting

Unix and db2 where condition issue(new line)

Hi I am extracting a column value(DESCRIPTION) from one table and passing it to another db2 statement in a shell code to fetch some value(ID) but the value when passed in where condition is taking as newline+value. Please find the out put when executed: + echo description is ::::... (1 Reply)
Discussion started by: msp2244
1 Replies

6. Shell Programming and Scripting

if condition issue

Hi, Unix Gurus, I got some issue with my script. #!/usr/bin/ksh while read newstatus do if ; then echo "It is finished " else echo "Not Finished" fi done <checkresult.txt basically, I want to read one file (the file only contains one word, but it dynamically changed),... (2 Replies)
Discussion started by: ken002
2 Replies

7. UNIX for Dummies Questions & Answers

If condition issue

Hello, I am using the following code #!/bin/sh chris='rerun' chris2=$1 echo "chris=$chris" echo "chris2=$chris2" if then echo "RERUN" else echo "NOT RERUN" fi (6 Replies)
Discussion started by: chriss_58
6 Replies

8. Shell Programming and Scripting

An issue with condition statement in shell script

Hello forum members. please go through the below mentioned issue and let me know the right solution. I have to write a script which runs another script .the executable script take input parmeters.so iam writing the the script below . Sample Code:Begins #! /bin/ksh echo " enter... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

9. Shell Programming and Scripting

Unix Arithmatic operation issue , datatype issue

Hi, I have a shell scripting. This will take 7 digit number in each line and add 7 digit number with next subsequent lines ( normal addition ). Eg: 0000001 0000220 0001235 0000022 0000023 ........... ......... ........ Like this i am having around 1500000 records. After adding... (23 Replies)
Discussion started by: thambi
23 Replies

10. UNIX for Dummies Questions & Answers

if...elif...fi condition in Unix

Hey guyes! i have a little problem in if condition, can anybody please solve my problem? Here what i am doing. if then echo "int1 is equal to int2" elif then echo "int1 is greater than int2" else echo "int1 is smaller than int2" fiNo, matter int1 is smaller than... (9 Replies)
Discussion started by: abidmalik
9 Replies
Login or Register to Ask a Question