Unix Programming - new to shell scripting

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2005 > new to shell scripting





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author new to shell scripting
James

2005-07-22, 2: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


joe@invalid.address

2005-07-22, 2: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
Fletcher Glenn

2005-07-22, 5: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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com