Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I use the same path in windows & unix

Status
Not open for further replies.

kirankv123

Programmer
Nov 2, 2006
5
US
Hi,

I want to open a text file which can be accessible from windows & unix machines and wants to use the same script from both unix & windows..How I can use the same path to open a file from the same script.

thanks


 
Just use relative paths using the unix file system stand. Perl run on windows is able to understand this just fine:

Code:
open(INPUT, "your/relative/path/file.txt") or die "can't open file: $!";

- Miller
 
Thanks Miller....Is there anyway that we figure out where my script is running like in windows/unix??
 
to know what OS the script is running in:

Code:
print $^O;

that's an upper case 'O' as in TOP and not the number 0.


- Kevin, perl coder unexceptional!
 
This is untested but it should give you a start. The important bit is the variable $^O (that's a capital oh, not a zero.)
Code:
my $filename;

if ($^O eq 'MSWin32') {
    # Windows
    $filename = 'c:/path/to/file.txt';
} elsif ($^O eq 'name of your *nix os') {
    # *nix
    $filename = '/path/to/file.txt';
}

open INPUT, "< $filename" or die "Cannot open file '$filename'\n$!\n";
 
hehehe.... another way is to use the -V switch:

from the command line:

Code:
perl -V:osname -V:osvers

or from a perl script (useful for getting the data from your shared server that doesn't give you commnad line access):

Code:
my $V = `perl -V:osname -V:osvers`;
print $V;




- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top