• Post Reply Bookmark Topic Watch Topic
  • New Topic

Script question.. help needed  RSS feed

 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit confused. I'm just starting to delve into Unix and have a script I am supposed to write and am having a problem.
Essentially my script finds directories/files/linkes etc.. from the current directory I am in and those below it. I understand the commands to do this, however I am having problems when trying this scenario...
my current directory:
asmt05
which has:
part12/ part4/ part3/ report.ksh
for simplicity sake, assume my report.ksh only has one command find . -type f
If I run the script, all directories are accessed. What can I do if I only want to run this script on part12/ from where I am at without placing the script into part12/ itself?
Any suggestions would be helpful.
Regards, Michael
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use cd in your script.
cd part12
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could note that the first parameter to "find" is actually the directory to start from, so you could just use
find part12 -type f
If you want to run the script separately for separate directories, then you could also make it a parameter:
find $1 -type f
and call it using
./report.ksh part12
./report.ksh part3
etc.
 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems more along the lines of what I am attempting to do- or think I am attempting to do. Essentially I was curious if I could run a script under one directory upon another directory without placing that script in it or without making mention of a specific directory in the script.
I'll try out this parameter you've mentioned and note the differences.
Thanks, Michael
Originally posted by Frank Carver:

If you want to run the script separately for separate directories, then you could also make it a parameter:
find $1 -type f
and call it using
./report.ksh part12
./report.ksh part3
etc.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a somewhat contorted way of doing the same thing. The prune subcommand will examine the pattern match set up by the name directive and ignore them, only traversing the subdirectories that don't match the pattern:

I mostly showed this so you could see pattern-matching rules work in find and that doing things the wrong way around is lots more fun than mere straightforward answers.
 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has all been helpful and has expanded my knowledge of Unix, however I often make things more complicated then they need to be. My misconception of my script starting in a higher directory and working upon one directory lower as opposed to being in that lower directory then giving a path to where the script is in the above directory -I now find- is the simplest solution of all
Regards, Michael
Originally posted by Michael Ernest:
Here's a somewhat contorted way of doing the same thing. The prune subcommand will examine the pattern match set up by the name directive and ignore them, only traversing the subdirectories that don't match the pattern:

I mostly showed this so you could see pattern-matching rules work in find and that doing things the wrong way around is lots more fun than mere straightforward answers.
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wanted to stick to your original:

you can do the following:
  • Create or dedicate a directory where you will keep all of your scripts. Let's call it $HOME/bin.
  • Put the report.ksh in this directory.
  • [list] In your .profile add this line [/list]

    Now, wherever you run your script it, the find command will start from the 'current' directory.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!