1836763 Members
2571 Online
110109 Solutions
New Discussion

ksh help please

 
SOLVED
Go to solution
John Stiles
Frequent Advisor

ksh help please

Hi All,

I know most of you can do this in your sleep, not me.

What I need should be fairly simple. I want to move selected files from one drive/jobdir to another drive/jobdir. The job # is always the same on both drives ex. 29999 but of course the job number changes with each job.

So in the following script, I enter $cadf 29999
at the prompt and want to select from a list of files to move from /appe/anydir/29999 to /appc/anydir/29999, (anydir would be subdirs between drive name and job number).

Here's what I have that works so far. I have trouble getting scripts to be interactive asking questions.

#!/bin/ksh
# @(#)cadf
# located /sjs/cadf
#
# program searches /appe dirs for job number dir, gets a list of files in dir,
# asks which files you want, then moves the files to /appc dir for same job
# number
#


set -x # for debugging

E=$(find /appe/eng -name $1)
cd $E
engdir=$(pwd)
C=$(find /appc/cad -name $1)


for f in $engdir
do
mv -i $engdir/* $C/
done


Thanks for any help.

John
all things must pass
21 REPLIES 21
curt larson_1
Honored Contributor
Solution

Re: ksh help please

could you answer a couple questions

1) is there only one /appe/anydir/29999
or is it possible that /appe/anydira/29999 and /appe/anydirb/29999 both exist.

2) same as 1 except for /appc/anydir

3) is there a one to one correspondence between /appe/anydir/29999 and /appc/anydir/29999, ie /appe/a/b/29999 always matches /appc/a/b/29999

4) will both directories always exist

5) do you want to overwrite any files in /appe/anydir/29999 regardless if they exist or not.
John Stiles
Frequent Advisor

Re: ksh help please

curt,

1-2) /appe and /appc are always constant drive names.
the job number only exists once on each drive.

3) not always. anydir can sometimes be variable.

4) yes.

5) yes. I would want to overwrite files going from /appe/... to /appc/...
It would be nice to have it ask and/or give the option to change the name.

Thanks,

John
all things must pass
curt larson_1
Honored Contributor

Re: ksh help please

something quick

fromDir=/appe/eng
toDir=/apc/cad
file=$1

PS3='please enter a number -> '
select i in $(find $fromDir -name $file)
do
case $i
${fromDir}*${file})
tmpDir=${i#$fromDir/}
tmpDir="$toDir/$tmpDir"
break;;

*) print "invalid number";;
esac


mv $fromDir/* $tmpDir;;

John Stiles
Frequent Advisor

Re: ksh help please

curt,

ran it using cadf 29999

get /sjs/cadf[58]: syntax error at line 62 : `${fromDir}*${file}' unexpected

John

Getting near quitting time. They make me leave at 3:30 on Friday. I'll be able to work on it more on Monday.

Have a Great Weekend.
all things must pass
John Stiles
Frequent Advisor

Re: ksh help please

curt,

the 29999 is a directory. the files under it are what we need, the file names are unknown.

John
all things must pass
curt larson_1
Honored Contributor

Re: ksh help please

thanks for answering the questions. that gives me a better understanding of what your doing. And brings up a couple more questions

do you want to only ask for a new file name if the file already exists

of ask for each file

your start is pretty good

E=$(find /appe/eng -name $1)
cd $E
engdir=$(pwd)
C=$(find /appc/cad -name $1)


for f in $(ls $engdir )
do
if [ -f ${engdir}/$f ] ;then #only do files
if [ -f $C/$f ] ;then
# do this if the file already exists
print "$C/f already exists do you want to overwrite it (y/n)? "
read answer
case $answer in
#if yes just overwrite the file
[Y|y]*) mv $engdir/$f $C/$f;;
#anything else ask for a new file name
*) print "what do you want to rename the file, $f, as? "
read answer
#might want to do some testing for valid per naming standard
mv $engdir/$f $C/$answer ;;
esac
else #file doesn't exist so just copy it
mv $engdir/$f $C/$f
fi
fi
done
curt larson_1
Honored Contributor

Re: ksh help please

my first script has a typo

select i in $(find $fromDir -name $file)
do
case $i
${fromDir}*${file})
tmpDir=${i#$fromDir/}
tmpDir="$toDir/$tmpDir"
break;;

should be

case $i in #need that "in"
${fromDir}*${file})
John Stiles
Frequent Advisor

Re: ksh help please

curt,

quick response, they are kicking me out.

"do you want to only ask for a new file name if the file already exists" yes

would like a list to choose which files to copy first. with numbers for each filename perhaps, then choose 1,3 4-7 or quit choices.

Then it would see if the file exists on /appc and allow for overwrite or rename choice?

John

Talk to you Monday.
all things must pass
curt larson_1
Honored Contributor

Re: ksh help please

just a couple of considerations

*) notDone=/usr/bin/false
while $notDone
do
print "what do you want to rename the file, $f, as? "
read answer
#might want to do some testing for valid per naming standard
if [ "$answer = "" ] ;then #user just typed enter
print "you'll have to enter a file name"
else
if [ -f $C/$f ] ;then
print "$f already exists, you'll need to select a different name"
else
mv $engdir/$f $C/$answer
notDone=/usr/bin/false
fi
fi
done;;
esac
curt larson_1
Honored Contributor

Re: ksh help please

thanks for all the points. here is a script that should come close to what your wanting to do
John Stiles
Frequent Advisor

Re: ksh help please

Curt,

I take it that parts of my script should be modified and left at the beginning?

I ran your script and it listed all dirs/files in /appe/eng then gives me the folowing syntax error:
+ PS3=please enter a number ->
/sjs/cadf[19]: syntax error at line 23 : `${fromDir}*${file}' unexpected

I tried adding my 2 find variables and got it to list the dir but still get the same syntax error.

You're way over my head here. That's what makes this forum such an asset. I do O.K. with small scripts doing 1-2 things, but having a single script do it all is where I'm weak. I usally just end up writing several scritps that are run from a main script. Not really the way I want it, but it works most of the time.
In my next post I'll attach an example script that I wrote so you can see an example of what I mean.

John



all things must pass
John Stiles
Frequent Advisor

Re: ksh help please

Curt,

attached single/multi script example mentioned above.

John
all things must pass
John Stiles
Frequent Advisor

Re: ksh help please

Curt,

BTW, points are the easy part, you earned them! You are doing the hard part.

This is the best forum on the net. All of you are a big help.

John
all things must pass
curt larson_1
Honored Contributor

Re: ksh help please

I take it that parts of my script should be modified and left at the beginning?

No, the script I attached should be all you need. The #!/usr/bin/ksh should start at column 1. And, feel free to add as many comments as you need. After all, your the one that needs to know how things are done.

I ran your script and it listed all dirs/files in /appe/eng then gives me the folowing syntax error:
+ PS3=please enter a number ->
/sjs/cadf[19]: syntax error at line 23 : `${fromDir}*${file}' unexpected

this is for the same reason I had mention previously, the "case" statement just after the PS3 assignment is missing it's "in".

select i in $(find $fromDir -name $file)
do
case $i
${fromDir}*${file})

should be

select i in $(find $fromDir -name $file)
do
case $i in ${fromDir}*${file})

But, with the script a provided none of that is necessary.


curt larson_1
Honored Contributor

Re: ksh help please

You're way over my head here.

ask as many questions as you'd like. I'll, or others, will try to answer them as best we can.
John Stiles
Frequent Advisor

Re: ksh help please

Curt,

That works. I'll test it for different scenario's. Also in an attempt to learn from this experience, I'll try to make it do other things. ex. for the question, allow multiple choices 1-3,6,8-10 or all files.

If I get stuck again, do I post it here, or post a new question? It may be toward the middle to latter part of this week before I get it done. They make me work here too? can you believe it?

John
all things must pass
John Stiles
Frequent Advisor

Re: ksh help please

Curt,

Let me be the first to congratulate you on your ne hat.

Congratulations and great job!!!

Keep up the good work.

John
all things must pass
John Stiles
Frequent Advisor

Re: ksh help please

oops. spelling counts...
that would be "new hat" not "ne hat"
all things must pass
curt larson_1
Honored Contributor

Re: ksh help please

If I get stuck again, do I post it here, or post a new question?

once the question poser has assigned a value of 8-10 for one of his answers the thread gets a bunny icon attached to it. Usually this means that the author has received a very good answer to his question.

And being the author already has an answer most forum members will discontinue following that thread.

So, I'd post a new question. That way more forum members will be likely to answer your question. I'd also put a url to this thread in the new post. That way someone can always go back to this thread as reference if they desire.
curt larson_1
Honored Contributor

Re: ksh help please

I'll try to make it do other things. ex. for the question, allow multiple choices 1-3,6,8-10 or all files.


all files is pretty easy. attached is the script with an all files option. you can do a diff and see what changes i've made. Maybe that will help you with making further changes.

Congratulations on the new hat and great job!!!

Thanks

It is always nice to help those that appreciate my work.
John Stiles
Frequent Advisor

Re: ksh help please

Curt,

Thanks for all the help.

The script works great! It'll also give me alot of things that I can apply to existing scripts.

I consider this question closed.

John
all things must pass