Click to See Complete Forum and Search --> : scripting conundrum


Shodan
06-01-2004, 05:47 PM
To start:
1) yes I am new to scripting
2) yes I have looked for help/additional info including the HFL

but I didn't see clues to my par-tic-u-lar sit-she-ation

here goes - this should be simple and strightforward - I want to poll directory A for subdirectories (sd) and then compare those results to a poll of directory B.

If the sd in A is not in B - then I want to copy it to B.

Period - that's it - end -o- story.

Once I master this then there are fancier things I want to do - but for now I will be happy with this.

here is what I have written thus far:

code:
#! /bin/sh
# movecare_jc
# example script of how one would move files from /newcare to /care

inputPath=/home/lab/images/directoryA/
outputPath=/home/lab/images/directoryB/

for blind in `ls -d */ $inputPath`

do
echo $blind
# this part checks if the directories exist at all
# if not, it creates the directories
if [$blind ! $outputPath]
then
mkdir $outputPath/$blind
fi
# this part copies the isotropic images and renames them
cp $inputPath/$blind/"$blind"afs.img $outputPath/$blind/"$blind"si.img
cp $inputPath/$blind/"$blind"afs.hdr $outputPath/$blind/"$blind"si.hdr
done


---------------------------------------------------------------------------
I am getting this

cp: cannot stat `/home/lab/images/directoryA//utils//utils/afs.hdr
': No such file or directory

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


I have no idea why/ what the <utils> portion is in there!

Any help is greatly appreciated.

JD

maccorin
06-02-2004, 05:49 AM
is it supposed to be copying to utils/utils/some_file ?

or is it supposed to be copying to utils/utilssome_file ?

if the second is the answer you need to sed out the / before your copy command


maccorin@lappy:~$ echo blar/ | sed 's:/$::g'
blar
maccorin@lappy:~$

Shodan
06-02-2004, 08:06 AM
Hey there mac...

well you see that is the problem, I have no idea what /utils is - and I sure don't want to copy anything to it!

directoryA/ and directoryB/ are both subdirectories of images/

i.e.:
/home/lab/images/directoryA/
/home/lab/images/directoryB/

In </home/lab/images/directoryA/> there are say 100 files and 50 subdirectories that share names with the files.
For example:
foo.img
foo.hdr
foo/
foo2.img
foo2.hdr
foo2/
etc.

All I want to do is read </home/lab/images/directoryA/> and copy only the directories to </home/lab/images/directoryB/>.

I think this should be a straight forward procedure and I can't figure out why it isn't working based on the code I am using.

Any help is appreciated.

JD

bsh152s
06-02-2004, 08:47 AM
Do what maccorin said and sed off the /, then tell us if you're getting the same errors.

Also, one thing I noticed when I ran the command "ls -d */ dir_name", it returns dir_name along with along with all of the directories in the current working directory. I don't think this is what you want.

Shodan
06-02-2004, 09:03 AM
hey there bsh152s

Ok, I will try working with sed - but this will be my first attempt with it so I don't expect to succeed.

Regarding the <ls -d */> command.. Auctually that is exactly what I want.
I want to copy all/only the directories from directoryA into directoryB.

This is how I thought the script would work.

I would create the variable 'inputpath' (i.e., directoryA)
The script would first list all of the directories in 'inputpath'
Then it would copy those directories to the variable 'outputpath' (i.e., directoryB).

For now this is all I want to do.
Once I understand the mechanics of this then I can work on fancier things.

Thanks,
JD

Shodan
06-02-2004, 12:39 PM
Originally posted by maccorin
is it supposed to be copying to utils/utils/some_file ?

or is it supposed to be copying to utils/utilssome_file ?

if the second is the answer you need to sed out the / before your copy command


maccorin@lappy:~$ echo blar/ | sed 's:/$::g'
blar
maccorin@lappy:~$



Can someone give me a run down of what the sed commands are doing?

I am leary of just doing something before I know what it does....

Thanks,
JD

Strogian
06-02-2004, 01:04 PM
sed 's:/$::g' deletes any / that occurs at the end of a line, so
weirw/weroiwer/wior/ becomes weirw/weroiwer/wior
asdf/// becomes asdf//
etc.

(if you've ever seen a regular-expression substitute it looks like this: s/old/new/ ... he's using : instead of / though, because / occurs in the expression. the g has no effect in this case, because this particular substitution can only occur once per line.)

something like "sed 's:/$::g' filename >> filename2" won't actually modify the original file, it will just make a (modified) copy of it.

Shodan
06-02-2004, 03:25 PM
To all,
Thanks for the help.
I have modified the script and gotten some success with it.
That is, the first 'do' line works - it prints a list of directories without the trailing / .....yea for me!

However, after that I get an error message saying the next command is not found.

What about my subsequent expression is wrong?

Thanks for any/all help

JD


code:
#! /bin/sh
# movedirs_jc

inputPath=/home/lab/images/dirA/
outputPath=/home/lab/images/copytrial/

cd $inputPath
for blind in `ls -d */ | sed 's:/$::g'`
do
echo $blind
# checks if the directories exist in outputPath
# if not, it creates the directories
if [$blind ! $outputPath]
then
mkdir $outputPath/$blind
fi
done

maccorin
06-02-2004, 03:34 PM
$outputdir=/some/out/dir
$inputdir=/some/in/dir

mkdir -p $outputdir

for i in `ls $inputdir`; do
if [ -d $inputdir/$i ]; then
cp -R $inputdir/$i $outputdir
fi
done


does that do what your looking for?, i think the issue is we aren't quite understanding your problem.

EDIT:
those are backticks, not quotes around ls $inputdir

Shodan
06-02-2004, 04:04 PM
maccorin,

thanks for the help.
One question though.
Will this overwrite all dirs in outputdir?

basically I want to compare inputdir and output dir.
I want to spare all dirs in output and add any new ones from inputdir.

JD

Maccorin - wow!

I played with your code and it did exactly what I was looking for - and so economical.
Man have I got a lot to learn...

Now time to work on understanding what the code really does!

More questions to come I am sure but at least I am showing progress.
Thanks again

JD

maccorin
06-02-2004, 04:15 PM
then you'll need to add a check to make sure the dir doesn't already exist. I'll leave that as an exercise to the reader, i'm not giving you the entire script ;p

Shodan
06-02-2004, 04:21 PM
maccorin

thanks again - really!

now I have to figure out how to run that check :-)

JD