Running a script: How to reference the directory the script is located in.
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Running a script: How to reference the directory the script is located in.




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Running a script: How to reference the directory the script is located in.  
jhagge@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-21-06 12:23 AM

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.






[ Post a follow-up to this message ]



    Re: Running a script: How to reference the directory the script is located in.  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-21-06 12:23 AM

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





[ Post a follow-up to this message ]



    Re: Running a script: How to reference the directory the script is located in.  
Daniel Rock


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-21-06 12:23 AM

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





[ Post a follow-up to this message ]



    Re: Running a script: How to reference the directory the script is located in.  
Chris Mattern


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-24-06 12:21 AM

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?"





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:04 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register