Unix Shell - Running a script: How to reference the directory the script is located in.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Running a script: How to reference the directory the script is located in.





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 Running a script: How to reference the directory the script is located in.
jhagge@gmail.com

2006-10-20, 7:23 pm

I am writing a script using the Bourne shell. I know very little about
Unix, but am familiar with DOS and writing batch files.

The script will be in a directory - I will call this directory DOGFOOD
to make my explanation easier.... In DOGFOOD there will be some files
I want to copy to a specific location on the users hard drive, this is
the only purpose of the script. The DOGFOOD directory could be
located anywhere on the hard drive (it will be downloaded from the
internet so I won't have control over where they save it). I am unsure
how to reference the DOGFOOD directory in the script so I can copy the
files in DOGFOOD to where they need to go. My best guess is to create
a randomly named file in DOGFOOD, then do a find for the file and
somehow grab the path from the output of find... surely there is a
better way to do this.

My other question is similar. This script is running on an OS X
machine. How do I reference the "system drive" ? On Windows there is
an environment variable for this. The closest thing I know of in the
Unix world is ~ for the users home directory, but I need to copy files
outside of the home directory on the system drive.

Thanks for your help.

Chris F.A. Johnson

2006-10-20, 7:23 pm

On 2006-10-20, jhagge@gmail.com wrote:
> I am writing a script using the Bourne shell. I know very little about
> Unix, but am familiar with DOS and writing batch files.


The Unix shell is to DOS batch files as the Viper is to a
velocipede.

> The script will be in a directory - I will call this directory DOGFOOD
> to make my explanation easier.... In DOGFOOD there will be some files
> I want to copy to a specific location on the users hard drive, this is
> the only purpose of the script. The DOGFOOD directory could be
> located anywhere on the hard drive (it will be downloaded from the
> internet so I won't have control over where they save it). I am unsure
> how to reference the DOGFOOD directory in the script so I can copy the
> files in DOGFOOD to where they need to go. My best guess is to create
> a randomly named file in DOGFOOD, then do a find for the file and
> somehow grab the path from the output of find... surely there is a
> better way to do this.


You can use 'type -d' to find a directory:

find / -type d -name DOGFOOD -print

> My other question is similar. This script is running on an OS X
> machine. How do I reference the "system drive" ? On Windows there is
> an environment variable for this. The closest thing I know of in the
> Unix world is ~ for the users home directory, but I need to copy files
> outside of the home directory on the system drive.


The entire file system is under the root directory, /; what part
of it do you want? Executable files are mostly in /bin and
/usr/bin; configuration files are mostly in /etc.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Daniel Rock

2006-10-20, 7:23 pm

jhagge@gmail.com wrote:
> I am writing a script using the Bourne shell. I know very little about
> Unix, but am familiar with DOS and writing batch files.
>
> The script will be in a directory - I will call this directory DOGFOOD
> to make my explanation easier.... In DOGFOOD there will be some files
> I want to copy to a specific location on the users hard drive, this is
> the only purpose of the script. The DOGFOOD directory could be
> located anywhere on the hard drive (it will be downloaded from the
> internet so I won't have control over where they save it). I am unsure
> how to reference the DOGFOOD directory in the script so I can copy the
> files in DOGFOOD to where they need to go. My best guess is to create
> a randomly named file in DOGFOOD, then do a find for the file and
> somehow grab the path from the output of find... surely there is a
> better way to do this.


dir=$(cd $(dirname "$0"); pwd)

Above for POSIX shell. If you have a really old Bourne Shell:

dir=`dirname "$0"`
dir=`cd "$dir"; pwd`

--
Daniel
Chris Mattern

2006-10-23, 7:21 pm

In article <1161382368.881754.155050@h48g2000cwc.googlegroups.com>,
jhagge@gmail.com wrote:
>I am writing a script using the Bourne shell. I know very little about
>Unix, but am familiar with DOS and writing batch files.
>
>The script will be in a directory - I will call this directory DOGFOOD
>to make my explanation easier.... In DOGFOOD there will be some files
>I want to copy to a specific location on the users hard drive, this is
>the only purpose of the script. The DOGFOOD directory could be
>located anywhere on the hard drive (it will be downloaded from the
>internet so I won't have control over where they save it). I am unsure
>how to reference the DOGFOOD directory in the script so I can copy the
>files in DOGFOOD to where they need to go. My best guess is to create
>a randomly named file in DOGFOOD, then do a find for the file and
>somehow grab the path from the output of find... surely there is a
>better way to do this.


I don't understand this. If you don't know where DOGFOOD is, how
will you create a file in it? If you intend for the user to run this
script while DOGFOOD is his current directory, then you can get
the full path for DOGFOOD with "pwd", which prints the full path
of the current directory.

>
>My other question is similar. This script is running on an OS X
>machine. How do I reference the "system drive" ? On Windows there is
>an environment variable for this. The closest thing I know of in the
>Unix world is ~ for the users home directory, but I need to copy files
>outside of the home directory on the system drive.
>
>Thanks for your help.
>


I'm not entirely sure how OS X does this, but it is supposed be
to UNIX-based, and in UNIX, there is no such thing as "the system
drive", there is only one unified directory tree. Drives as
such are not seen unless you are mounting and unmounting filesystems.
What are you trying to do and why do you need to copy files
outside the user's home directory? Where do you want them to go
and what do you think this will accomplish? Be aware that if it is the
user that is running the script, it is unlikely that he will have
permission to write anywhere besides his home directory (and the
temp directory).


--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com