new to shell scripting
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > new to shell scripting




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    new to shell scripting  
James


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-22-05 07:56 AM

I am new to shell scripting and trying to learn bourne shell scripting I was
try to write a script that check the content of directory A and B and
deletes any files in A that are not in B

So far I have

#!/bin/sh
for d in dirB/*; do
if [ -f dirA/$d ]
then
echo "$d"
fi
done

I used echo instead of rm because I am new to shell script and didn't want
to delete all my files.

Thank-you







[ Post a follow-up to this message ]



    Re: new to shell scripting  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-22-05 07:56 AM

"James" <bob_then@yahoo.com.au> writes:

> I am new to shell scripting and trying to learn bourne shell
> scripting I was try to write a script that check the content of
> directory A and B and deletes any files in A that are not in B
>
> So far I have
>
> #!/bin/sh
> for d in dirB/*; do
>    if [ -f dirA/$d ]
>    then
>       echo "$d"
>    fi
> done
>
> I used echo instead of rm because I am new to shell script and
> didn't want to delete all my files.

That's a good way to test things. Another way is to try the parts of
the script on the command line. In this case,

$ for d in dirB/*;do echo "$d";done

would show you that "dirB/" is part of what's returned. What you want
d to be is just the file name. That can be gotten with the basename
command. Try instead

for d in dirB/*; do
d=`basename "$d"`
if [ -f dirA/"$d" ]
then
echo "$d"
fi
done

Joe





[ Post a follow-up to this message ]



    Re: new to shell scripting  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-22-05 10:55 PM

joe@invalid.address wrote:
> "James" <bob_then@yahoo.com.au> writes:
>
> 
>
>
> That's a good way to test things. Another way is to try the parts of
> the script on the command line. In this case,
>
> $ for d in dirB/*;do echo "$d";done
>
> would show you that "dirB/" is part of what's returned. What you want
> d to be is just the file name. That can be gotten with the basename
> command. Try instead
>
> for d in dirB/*; do
>   d=`basename "$d"`
>   if [ -f dirA/"$d" ]
>   then
>     echo "$d"
>   fi
> done
>
> Joe

Do you understand that all you will indentify with this loop is the
files that are common to A and B.  What you want to identify is]
files that are not in B.  So, you should go through the list
of files in A.  If the files DO NOT exist in B then erase them.

--

Fletcher Glenn






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:50 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register