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 ??)

 
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
if [ $actual_size -gt $FILE_SIZE ]

your actual_size=`du $file` is giving you more data than you expect ... see 2nd line for fix ...

root@solarisgeek # du /etc/shadow
4     /etc/shadow
root@solarisgeek # du /etc/shadow | awk '{print $1}'
4
root@solarisgeek #
1 Like

The -gt is giving me an unknown operator error

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

Hello WiseCraker

THis gives me unknown operator error

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

hello Iceguy
Thanks this helped me a lot :b::slight_smile:

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

hi Wise Craker

This worked with Quotes
"$var" -gt "$var1"

Which shell are you using?
I didn't read your code just the "if" statement...

This is using the "/bin/bash --posix" structure...

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]

-ksh is what I am using

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.:-

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:-

if [ "X$FILE_NAME" == "X" ]

to

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

... .and

if [ $actual_size > $FILE_SIZE ]

to

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:-

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

1 Like

Hi Rbatte1

Thanks for your suggestions, Chnaged my shell script to reflect as per your code, worked fine, Thank you once again

I am a newbie in SHELL, so learning a lot from unix gurus like you

We were all new to it once, and we're all still learning now.

Welcome aboard. :wink:

Robin

You can test also empty string without X extension - this example is cut&pasted worldwide, but sh shells don't need it. You can test simple

if [ "$FILE_SIZE" = "" ] ; then # value is empty
     do_some
fi
# or not empty testing
if [ "$FILE_SIZE" != "" ] ; then # value is not empty
     do_some
fi

# you can write previous line very shortly, but I like myself prev version = more readable, self documentation.
if [  !  "$FILE_SIZE"  ] ; then # variable is empty
     do_some
fi
# or not empty testing
if [  "$FILE_SIZE"  ] ; then # value is not empty
     do_some
fi

In shell script you can use old test command [ and also newer [[.
They are different test cmds. Almost same but not.

1 Like