Operating System - HP-UX
1849236 Members
2814 Online
104042 Solutions
New Discussion

Re: Shellscript help needed

 
Rajkumar_3
Regular Advisor

Shellscript help needed

Hai all,

I am having some problem with the script.....

I have attached the shell script with all the details.....

This script is prepared by robin.....

I need your help to solve this problem....


Raj
Oracle DBA
21 REPLIES 21
Deepak Extross
Honored Contributor

Re: Shellscript help needed

Well, we can try to help if you tell us what problem you are facing.

Is it the
|ARGS RM
that is causing the problem?
If so, you'll get an error:
"`|' is not expected."
Just delete that line and try again.

Apologies if I have not understood your question.
Rob Galloway_1
Frequent Advisor

Re: Shellscript help needed

Hi,

It looks to me like the following section of code is missing a 'do' keyword.

find /home/rajkumar/J??/* -name "X_*" -mtime +3 | while read FILE ;
rm $FILE && logger "$0 : $FILE deleted"
COUNT=$(expr $COUNT + 1)
done

Rob
Experience is a hard teacher. It tests first and teaches afterward.
Mary Ann Lipa
Valued Contributor

Re: Shellscript help needed

Hello,
...
exit 1
fi
|ARGS RM
COUNT=0
....

what's the line with the | ? that could be the erring line...
could please elaborate more on what the script basically does?
d_b
Which is worse, smoking or picking your nose in a public place?
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

hai all

Thank you for your replies...

similar to this script I have attached the other script with all the details..

Consider this is actual script...Thanking you....

Raj
Oracle DBA
Deepak Extross
Honored Contributor

Re: Shellscript help needed

First, your RETAIN_PEND is set to 5, not 3.

Second, find will give you all teh "X_*.DAT" files as well, you can eleimiate them by changing
find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | while read FILE ; do
to
find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "X_" | while read FILE ; do
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

Hai deepak,

Thank you for your reply....

We cannot hardcoded "X_" in the script..That "database.props" will updated by the user from some other application...Some times user many change the it to "Y_".If they change it to "Y_" ,internally this "database.props" will automatically update to "Y_".so at that time it will be a problem..So we have to avoid it by getting the value itself from the "database.props" file..Is it possible in that way????

Raj...
Oracle DBA
Deepak Extross
Honored Contributor

Re: Shellscript help needed

Sure.
You already have a BACK_FILE_EXT=X_ in your props file.
You can do this:
find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v ${BACK_FILE_EXT} | while read FILE ; do

Remember that once you change BACK_FILE_EXT from X_ to Y_, all files with X_ will be deleted when you run this script. Maybe you should consider:
find $DIR -name *${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "_" | while read FILE ; do
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

hi deepak,

When i placed | grep -v {BACK_FILE_EXT}
I am facing the error..

and When i replaced with
| grep -v "_" it is displaying the DAT files along with CDR files also....

any solution please..

Raj
Oracle DBA
Deepak Extross
Honored Contributor

Re: Shellscript help needed

I hope you are not forgetting the $ in
" grep -v ${BACK_FILE_EXT} "

find is working fine here in eliminating the cdr files.
I dont know whats going wrong on your end, but you can try this:
find $DIR -name *${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "_" | grep -v "CDR" | while read FILE ; do
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

Hai Deepak,

If i placed like this
1)|"grep -v ${BACK_FILE_EXT}"
2)|"grep -v $ "_""

If i placed like this |grep -v "_" i am getting CDR files as well as DAT files only....but "X_" are not displaying...

Any solution ???

Raj
Oracle DBA
Deepak Extross
Honored Contributor

Re: Shellscript help needed

OK, lets start again..
you've got file*.DAT, X_file*.DAT and file*.CDR.
Now, which of these do you want to delete if they are older than 3 days?
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

hi deepak,

The script is attached again...

In the "database.props" file i am having only "FILE_NAME=DAT" extension and "RETAIN_PEND=<3 or 4 days>
so i if execute the script it should collect only "DAT" extension files that were not starting with "X_<....>.DAT" and it should be deleted from the value specified in the "RETAIN_PEND" parameter........

Other than that we should not delete all the files like ".DAT" & ".CDR" and the files starting with "X_.DAT".

The files .DAT only should be deleted upon the RETAIN_PEND value....

Please revert back to me if my explanation is wrong......

RA
Oracle DBA
Deepak Extross
Honored Contributor

Re: Shellscript help needed

find . -name "*DAT" -mtime +3
will give you .DAT and X_.DAT

find . -name "*DAT" -mtime +3 | grep -v "_"
will give you .DAT

Please try this. I don't see how you can be getting .CDR files.
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

Hai Deepak,,

When i execute below one i am getting only "DAT" &"CDR" files...
#find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "_" | while read FILE ; do

But when i execute the below in the script ,It display nothing!!!!!!!!!
#find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "${BACK_FILE_EXT}" | while read FILE ; do

I dont want to hardcoded "_" or "X_" in the scripts..Every thing i should read from the parameter....

Is it possible.....

Raj
Oracle DBA
Deepak Extross
Honored Contributor

Re: Shellscript help needed

Looks like your variables $FILE_NAME and $BACK_FILE_EXT are not set correctly.
Can you echo them and check?
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

Hai

When i execute this below script,Its displaying null;

#################
export CONFIG_DIR=/home/rajkumar/house/input
export PROPERTY_FILE=database.props

gw=$CONFIG_DIR/$PROPERTY_FILE
echo $gw
set | grep BAC_ $gw | cut -d= -f2 | while read DIR ; do

if [ ! -d "$DIR" ] ; then
logger "$0: directory, $DIR not found."
fi

COUNT=0

find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "$BACK_FILE_EXT*" | while read FILE ; do
echo $FILE_NAME
echo $RETAIN_PEND
echo $BACK_FILE_EXT
#rm $FILE && logger "$0: file $FILE deleted."
COUNT=$(expr $COUNT + 1)
done

if [ "$COUNT" -gt 0 ] ; then
logger "$0: for directory $DIR, $COUNT file(s) removed."
else
logger "$0: for directory $DIR, no matching files found."
fi
done
exit 0
####################################
Oracle DBA
Ian Dennison_1
Honored Contributor

Re: Shellscript help needed

'set -x' at the start of the script, track the usage of variables and see where the break in logic is occuring.

Share and enjoy! Ian
Building a dumber user
Deepak Extross
Honored Contributor

Re: Shellscript help needed

OK, you need to do two thigs.
First, decide whether PROPERTY_FILE should be PROPERTY.PROPS or database.props.
Second, source the property file in your script like so:
-------------
gw=$CONFIG_DIR/$PROPERTY_FILE
. gw
-------------

If, after doing this, your echos are still null, you may need to export the values in your properties file.
That is, instead of
FILE_NAME=DAT
you'll have to do a
export FILE_NAME=DAT

Hope this helps.
Rajkumar_3
Regular Advisor

Re: Shellscript help needed

Hi deepak,

Still the same problem occuring....

Thank you all for your help..

Raj
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shellscript help needed

Hi Raj,

The only thing I can see wrong is that the $gw file is not being sourced. You don't need the "set" either, since you're grepping a file.

One minor point is that you need a "continue" statement when a $DIR is not found, to abort the current loop - this is how I see the script looking at the moment:

==========================================
export CONFIG_DIR=/home/rajkumar
export PROPERTY_FILE=database.props

gw=$CONFIG_DIR/$PROPERTY_FILE
echo $gw
. $gw
grep BAC_ $gw | cut -d= -f2 | while read DIR ; do

if [ ! -d "$DIR" ] ; then
logger "$0: directory, $DIR not found."
continue
fi

COUNT=0

find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "$BACK_FILE_EXT*" | while read FILE ; do
echo $FILE_NAME
echo $RETAIN_PEND
echo $BACK_FILE_EXT
#rm $FILE && logger "$0: file $FILE deleted."
COUNT=$(expr $COUNT + 1)
done

if [ "$COUNT" -gt 0 ] ; then
logger "$0: for directory $DIR, $COUNT file(s) removed."
else
logger "$0: for directory $DIR, no matching files found."
fi
done
exit 0
==========================================

Rgds, Robin.
Eugen Cocalea
Respected Contributor

Re: Shellscript help needed

Hi Raj,

try the script below. if it won't work, it's all your fault, it works fine for me :P~


export CONFIG_DIR=/root/test
export PROPERTY_FILE=database.props
gw=$CONFIG_DIR/$PROPERTY_FILE

. $gw

set | cat $gw | cut -d= -f1
cat $gw | grep "BAC_" | cut -d= -f2 | while read DIR ; do

if [ ! -d "$DIR" ] ; then
logger "$0: directory, $DIR not found."
fi

COUNT=0
find $DIR -name "*${FILE_NAME}" -mtime +${RETAIN_PEND} | grep -v "$BACK_FILE_EXT*" | while read FILE ; do


rm $FILE && logger "$0: file $FILE deleted."
COUNT=$(expr $COUNT + 1)
done

if [ "$COUNT" -gt 0 ] ; then
logger "$0: for directory $DIR, $COUNT file(s) removed."
else
logger "$0: for directory $DIR, no matching files found."
fi
done
exit 0
To Live Is To Learn