1830234 Members
2047 Online
109999 Solutions
New Discussion

doubt in script

 
laiju.c.babu
Regular Advisor

doubt in script

Hi Team,

 

I am quite new to the field of scripting .

 

In one of  our server we  have one script  which is using for removing the tapes from the library. I am attaching one of the script below , is it possible for you to tell me what will happen while this script is get executed.

 

#!/usr/bin/sh
#Input : scan thru all tape eject log files
#Output : list of dates files with tapes returning on that day.
#Usage : tapereturn.sh _today_YYYYMMDD_
>/tmp/tapereturn.txt
LOGDIR="/u11/library/log/"
if [[ $# -eq 0 ]] then
  echo "Usage tapereturn.sh _today_YYYYMMDD_"
  echo "No argument pass exit script"
  exit
fi
FILESET="*eject*$1*"
for a in `ls $LOGDIR$FILESET`
do
#  echo "$a"
   grep "expiration" $a |awk '{print $1 " " $NF}' >>/tmp/tapereturn.txt
done

while read tape return_date
do
   rd_file="return_on_`echo $return_date |tr \/ \_ `"   # Determine what is the return date file name
   touch /u11/library/log/returntape/$rd_file
   # Added these to ensure the return file always belongs to library id to avoid error
   chown library:osgadm /u11/library/log/returntape/$rd_file
   chmod 640 /u11/library/log/returntape/$rd_file
   #
   grep -q $tape /u11/library/log/returntape/$rd_file      # check if this tape already mention in return file
   if [[ $? -ne 0 ]] then                               # Cannot find tape in rd_file, means new instances
      echo "$tape backup on $1" >> /u11/library/log/returntape/$rd_file
   fi
                                                        # Else tape already found in rd_file due to rerun.
done </tmp/tapereturn.txt

rm /tmp/tapereturn.txt

 

 

Mainly i have one doubt on what this >/tmp/tapereturn.txt will do and what is the action of while statement here .

 

Regards

 

Laiju.C.Babu
7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: question on script

>I have one question on what this >/tmp/tapereturn.txt will do

 

This either creates an empty file or resets the EOF to 0.

 

>what is the action of while statement here .

 

At a high level the while loop finds tapes that are in the log directory with *expire* in their names.

Then it adds these tapes to the returntape/return_on_* file.

 

while read tape return_date; do

   ...
done </tmp/tapereturn.txt

 

Reads each record in tapereturn.txt and assigns the first field to tape and the rest to return_date.

Re: doubt in script

>/tmp/tapereturn.txt

 This just truncates the file /tmp/tapereturn.txt to length 0 (i.e. if the file still exists, its contents are deleted, if it doesn't exist, it is created empty) Effectively this is redirecting "nothing" to the file.

 

 

while read tape return_date
do
   rd_file="return_on_`echo $return_date |tr \/ \_ `"   # Determine what is the return date file name
   touch /u11/library/log/returntape/$rd_file
   # Added these to ensure the return file always belongs to library id to avoid error
   chown library:osgadm /u11/library/log/returntape/$rd_file
   chmod 640 /u11/library/log/returntape/$rd_file
   #
   grep -q $tape /u11/library/log/returntape/$rd_file      # check if this tape already mention in return file
   if [[ $? -ne 0 ]] then                               # Cannot find tape in rd_file, means new instances
      echo "$tape backup on $1" >> /u11/library/log/returntape/$rd_file
   fi
                                                        # Else tape already found in rd_file due to rerun.
done </tmp/tapereturn.txt

This reads each line out of /tmp/tapereturn.txt, which is presumably made up of 2 columns listing a tape and a return date. This information is used to create or update a file in /u11/library/log/returntape which is called return_on_<date> (although there's no indication in the script of what the <date> field will look like, except to say the fields in the date will be delimited by underscores "_" . What goes into this file? Well basically if it doesn't already contain a mention of the tape, then a line is appended mentioning the tape and return date.

 

Of course, most backup software these days has full media management functionality meaning you shouldn't have to "roll your own" scripts like this. I'm guessing at this site you have no proper backup software and are using tar/cpio/vxdump/fbackup or something like that?

 

 


I am an HPE Employee
Accept or Kudo
laiju.c.babu
Regular Advisor

Re: doubt in script

Hi Duncan,

 

Thanks for the reply.

 

In the script there is one variable having the below value

 

FILESET="*eject*$1*"

 

What this $1 implies .

 

 

Regards

Laiju.C.Babu
Dennis Handly
Acclaimed Contributor

Re: question on script

>What this $1 implies?

 

$? is the current first argument to the script/function, your date.

laiju.c.babu
Regular Advisor

Re: question on script

Hi DH,

 

Thanks for the update.

 

In that script i want to see the  output of tapereturn.txt, hence i added one more line for copying the tapereturn.txt to another directory, But this copying is not hapenning. Please see the below updated script, my aim is to copy the file tapereturn.txt to my home directory.  Could you please have a look on this and let me know whether i made any mistake

 

#!/usr/bin/sh
#Input : scan thru all tape eject log files
#Output : list of dates files with tapes returning on that day.
#Usage : tapereturn.sh _today_YYYYMMDD_
>/tmp/tapereturn.txt
LOGDIR="/u11/library/log/"
if [[ $# -eq 0 ]] then
  echo "Usage tapereturn.sh _today_YYYYMMDD_"
  echo "No argument pass exit script"
  exit
fi
FILESET="*eject*$1*"
for a in `ls $LOGDIR$FILESET`
do
#  echo "$a"
   grep "expiration" $a |awk '{print $1 " " $NF}' >>/tmp/tapereturn.txt
done
/usr/bin/cp -p -r /tmp/tapereturn.txt /u01/osg/adm/babula/  ####This one i added for copying the file to my home directory

while read tape return_date
do
   rd_file="return_on_`echo $return_date |tr \/ \_ `"   # Determine what is the return date file name
   touch /u11/library/log/returntape/$rd_file
   # Added these to ensure the return file always belongs to library id to avoid error
   chown library:osgadm /u11/library/log/returntape/$rd_file
   chmod 640 /u11/library/log/returntape/$rd_file
   #
   grep -q $tape /u11/library/log/returntape/$rd_file      # check if this tape already mention in return file
   if [[ $? -ne 0 ]] then                               # Cannot find tape in rd_file, means new instances
      echo "$tape backup on $1" >> /u11/library/log/returntape/$rd_file
   fi
                                                        # Else tape already found in rd_file due to rerun.
done </tmp/tapereturn.txt

rm /tmp/tapereturn.txt

 

Regards

Laiju.C.Babu
Dennis Handly
Acclaimed Contributor

Re: question on script

>But this copying is not happening.

 

It seems like it should but it happening multiple times.

 

Move it to the bottom:

done </tmp/tapereturn.txt

 

cp -p /tmp/tapereturn.txt /u01/osg/adm/babula/  ####This one I added

 

rm /tmp/tapereturn.txt

 

Do you have permission to write to your home directory?  (Are you yourself, when you run the script?)

(Also no need for recursion, -r).

laiju.c.babu
Regular Advisor

Re: question on script

Hi Dennis,

 

What  you told may be right ..  May be permission is making the issue .. Now i created a directory having full access in /tmp and copied the tapereturn.txt file to there.

 

Lets see is it working ?

 

Regards

 

Laiju.C.Babu