1833758 Members
2946 Online
110063 Solutions
New Discussion

A Powerful shell script!

 
Jayesh shah
Frequent Advisor

A Powerful shell script!


I have a file of the format below :



......

......
and so on



------------------------------------------------------
IBE augmentation level
IBE rule
1527753849 1163 Exports/CC
1527753849 1163 Exports/Global_wrapper
1527753849 1163 Exports/yacc
4294967295 0 FILELIST.IBE
3618088840 5846 README.FIRST
ABE.IC augmentation level
clear rule
1743432609 16384 usr/bin/clear
dev rule
68954040 218620 dev/core
dld rule
1105456478 102400 usr/lib/dld.sl

-----------------------------------------------------------


Each level corresponds to a source directory
eg.IBE corresponds to /ha/be/sa_ems/IBE/
and the locations correspond to files under them.
eg if there is Exports/CC, it means
there is a file /ha/be/sa_ems/IBE/Exports/CC
and it has corresponding checksum,size as indicated

What I need to do:
copy the files to a particular directory from all these various locations

eg.Let's say the target root dir is /BE/
the for the line
Exports/CC

I will have to
1. find the level and get the base source dir
In this case it is IBE which corresponds to /ha/be/sa_ems/IBE/

2.create any sub-dirs under /BE if required.
In this case it is Exports
So, I will have to create Exports under /BE/

3.check for the file and verify whether the checksum and sixe are correct

4.If step 3 succeeds,copy it to the target dir

In this case it is CC
so,I will have to copy CC to /BE/Exports/CC

5.Retain the permissions,owner,group of the file.


It is really big process,and there are hundreds of such lines and there are many such files.

Can anyone provide some script or any ideas/suggestions
/or any information on these.
It would be highly appreciated and highly Rewarded:)

Waiting for many responses:)

-Jayes
6 REPLIES 6
Sridhar Bhaskarla
Honored Contributor

Re: A Powerful shell script!

Hi Jayesh,

Good food. Try this.

//start

#!/usr/bin/ksh

#change these to the applicable

INFILE=/full_path_to_your_formatted_file
TMP=/tmp/input$$
PARENT=/ha/be/sa_ems
TARGET=/

#Change if you want

LOG=/tmp/output.log
ERRLOG=/tmp/err.log
TMPTAR=/tmp/tmp.tar

for SRC in `grep "augmentation level" $INFILE |awk '{print $1}'`
do
cd ${PARENT}/${SRC}
mkdir ${TARGET}/${SRC}
sed -n '/'$SRC' augmentation level/,/augmentation level/p' $INFILE \
|sed '/augmentation level/d' |sed '/rule/d' > $TMP

cat $TMP|while read LINE
do
FILE=`echo $LINE|awk '{print $3}'`
if [ -f $FILE ]
then
RESULT=`cksum $FILE`
if [ "$RESULT" = "$LINE" ]
then
tar cf $TMPTAR $FILE
cd ${TARGET}/${SRC}
tar xf $TMPTAR
echo "$SRC: $LINE has been successful" >> $LOG
cd ${PARENT}/${SRC}
else
echo "$SRC: $LINE has checksum problems" >> $ERRLOG
fi
else
echo "$SRC: $LINE doesnot exist" >> $ERRLOG
fi
done


done


rm $TMP $TMPTAR

//END

See if it works... I am just tarring it up and untarring so that it will take care of permissions/ownership and subdirectories etc.,


-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: A Powerful shell script!

7 means only a portion of it helped. So, what's the portion this script is missing?. If you let me know I will try to put it in also.
You can copy & paste this script and can have it working.

Response is very important so that we will know our mistakes.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Jayesh shah
Frequent Advisor

Re: A Powerful shell script!

Actually,The script provided by Sridhar does almost everything that I require.

There is only one small problem (rather I should have not taken note of it while assigning points)

mkdir should have been mkdir -p.

The other point is : the PARENT is dependent on the augmentation level.Well,My mistake,I did not give full info.

The last line may or may not be augmentation level line.

Sridhar,Your help is really appreciated.
-Jayesh
Sridhar Bhaskarla
Honored Contributor

Re: A Powerful shell script!

Jayesh,

You are right.. It should be mkdir -p.

The second thing is that the some portion of your PARENT DIRECTORY is independent of your augmentation level. So, we can just assign it to $PARENT as it doesnt' change.

If you check the other line, I am going into the dependent directory by doing a

cd ${PARENT}/${SRC} where SRC is derived based on each "augmentation level" header.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Robin Wakefield
Honored Contributor

Re: A Powerful shell script!

Hi Jayesh,

Here's how to do it within awk. Pls note, I've put echo statements in to show what it *would* do. Remove the "echo" to actually run the script.

===========================================
#!/bin/sh

INDIR=/ha/be/sa_ems
OUTDIR=/BE
INPUT=your_input_file_name

awk '
/augmentation level/{SUBDIR=$1;next}
NF==3{
FNAME=$3;$3=INDIR"/"SUBDIR"/"$3;line=$0;FILE=$3;$0="";print FILE
"cksum "FILE " 2>/dev/null"|getline;close "cksum "FILE "2>/dev/null"
print line
if (line == $0)
{
"dirname "FNAME|getline;close "dirname "FNAME
system ("echo mkdir -p "OUTDIR"/"$0)
system("echo cp -p "INDIR"/"SUBDIR"/"FNAME" "OUTDIR"/"FNAME)
}
}' INDIR=$INDIR OUTDIR=$OUTDIR $INPUT

============================================

Rgds, Robin.
Jayesh shah
Frequent Advisor

Re: A Powerful shell script!

The while loop breaks before reading the complete file ?

What could be the reason?
-----------------------------
cat $TMP|while read LINE
do
FILE=`echo $LINE|awk '{print $3}'`
if [ -f $FILE ]
then
RESULT=`cksum $FILE`
if [ "$RESULT" = "$LINE" ]
then

.....
-----------------------------