LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2004, 02:01 AM   #1
kakho
LQ Newbie
 
Registered: Apr 2004
Posts: 3

Rep: Reputation: 0
My shell script can't seem to call another script


hi, i'm making a shell script that iteratively checks for duplication of files within a directory...
so, if i have file1.txt, file2.txt and file3.txt, then the script should check file1.txt against file2.txt, file1.txt against file3.txt, file2.txt against file3.txt and so on...
i got two files with me, here the first part
*********************************************************************************************
#duplication.sh
#this file is basically comparing two files given by duplication_loop.sh
#!/bin/sh

num=0
num2=0
num3=0
commnum=0
commnum2=0
commnum3=0

echo Checking $1 against $2...

#Replace the tabs with a single space
tr '\t' ' ' < $1 > temp1.tmp
tr '\t' ' ' < $2 > temp2.tmp

#Remove all trailing white spaces at the end of the lines and convert multiple spaces to a single space
sed -e 's/ *$//' -e 's/ */ /g' temp1.tmp > temp3.tmp
sed -e 's/ *$//' -e 's/ */ /g' temp2.tmp > temp4.tmp

#Remove all the blank lines
grep -v "^$" temp3.tmp > temp5.tmp
grep -v "^$" temp4.tmp > temp6.tmp

while read commline1
do
commnum2=`expr $commnum2 + 1` #Count the number of lines in the first file given
while read commline2
do
if [ "$commline1" = "$commline2" ];then #If the lines matched
commnum=`expr $commnum + 1` #Increment the counter
echo $commnum > commnum.tmp #Save the result of "commnum" to a temporary file or else it'll get re-initiated once it got out of the loop
fi
done < temp6.tmp
#Save the result of "commnum2" to a temporary file or else it'll get re-initiated once it got out of the loop
echo $commnum2 > commnum2.tmp
#Retrieve the "commnum" value
commnum=`cat commnum.tmp`
done < temp5.tmp

#Remove the comments
sed -e 's;#.*;;' -e '/^[ ]*$/d' temp5.tmp > temp7.tmp
sed -e 's;#.*;;' -e '/^[ ]*$/d' temp6.tmp > temp8.tmp

while read line1
do
num2=`expr $num2 + 1` #Count the number of lines in the first file given
while read line2
do
if [ "$line1" = "$line2" ];then #If the lines matched,
num=`expr $num + 1` #Increment the counter
echo $num > num1.tmp #Save the result of "num" to a temporary file or else it'll get re-initiated once it got out of the loop
fi
done < temp8.tmp
#Save the result of "num2" to a temporary file or else it'll get re-initiated once it got out of the loop
echo $num2 > num2.tmp
#Retrieve the "num" value
num=`cat num1.tmp`
done < temp7.tmp

commnum=`cat commnum.tmp`
commnum2=`cat commnum2.tmp`
num=`cat num1.tmp`
num2=`cat num2.tmp`
num3=`expr $num2 - $num` #Calculate the number of lines not matched
perc=`expr $num3 \* 100`
perc=`expr $perc / $num2`
commnum3=`expr $commnum2 - $commnum` #Calculate the number of comments not matched
commperc=`expr $commnum3 \* 100`
commperc=`expr $commperc / $commnum2`
echo Total lines matched $commnum
echo total lines not matched $commnum3
echo Percentage of total lines different $commperc\%
echo Total lines matched excluding comments $num
echo Total lines not matched excluding comments $num3
echo Percentage of total lines different excluding comments $perc\%

rm -f *.tmp
************************************************************************************************
and here's the second file
************************************************************************************************
#duplication_loop.sh
#this file iterates through the directory to compare files against each other
#!/bin/sh

i=1
j=1
ls > ltemp.rbs
no_lines=`cat ltemp.rbs | wc -l`
while [ $i -lt $no_lines ]
do
fname1=`cat ltemp.rbs | sed -n "${i}p"`
while [ $j -lt $no_lines ]
do
if [ $i -eq $j ];then
j=`expr $j + 1`
continue
else
fname2=`cat ltemp.rbs | sed -n "${j}p"`
./duplication.sh $fname1 $fname2
j=`expr $j + 1`
fi
done
i=`expr $i + 1`
done

rm -f *.rbs
*************************************************************************************************

the problem is, it's fine when i call the duplication as ./duplication.sh file1.txt file2.txt
it works fantastically
but when i call ./duplication_loop.sh, it gives me some problem that i cannot understand... somehow it cannot create the temp files.... or maybe it got deleted right after the creation...
to understand the problem more, please but and paste it to your editor and run it yourself... the filenames are duplication_loop.sh and duplication.sh
PLEASE HELP!!!!!!!! thank you so much
 
Old 04-17-2004, 08:21 AM   #2
ugge
Senior Member
 
Registered: Dec 2000
Location: Gothenburg, SWEDEN
Distribution: OpenSUSE 10.3
Posts: 1,028

Rep: Reputation: 45
Do you need to check for exakt duplicates?
Then you could run the diff command using it's built-in recursion (-r) and duplicate reporting (-s).
Might even be able to make it disregard comments.
 
Old 04-17-2004, 08:50 AM   #3
kakho
LQ Newbie
 
Registered: Apr 2004
Posts: 3

Original Poster
Rep: Reputation: 0
i was told to do something that emulates what "diff" does... please help...
 
Old 04-17-2004, 09:21 AM   #4
ugge
Senior Member
 
Registered: Dec 2000
Location: Gothenburg, SWEDEN
Distribution: OpenSUSE 10.3
Posts: 1,028

Rep: Reputation: 45
I'm not a script hacker, and right now I have to leave.

Just curious: Why? And with what tools?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
call another script and have the inital script exit mjtice Programming 1 09-11-2005 11:54 AM
Shell Script call API treotan Programming 1 08-22-2005 10:59 PM
call a c program in a shell script jagman Programming 4 04-05-2005 04:58 PM
Call a shell script from php? jharper101 Programming 2 02-15-2005 12:51 AM
Urgent: Call a C program through Linux shell script nuwandee Programming 14 04-10-2004 07:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration