Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script Path

229 views
Skip to first unread message

Chawki Jreige

unread,
Sep 5, 2015, 6:01:30 AM9/5/15
to
Is there a way to determine the path of the script from within that same script?

I've tried to use the following:

set scriptPath [info script]

But this only gives me the name of my script, as in 'myScript.tcl'. I'd like to know if I can obtain the full path?

Thank you.

Alexandre Ferrieux

unread,
Sep 5, 2015, 8:35:00 AM9/5/15
to
The value returned by [info script] is the one passed to [source] or as an argument to tclsh. If this value is an absolute path, you'll get it back unmodified. If it is relative or without "/", you'll get it "as is".
Just prepend with [pwd] to get back a full path:

proc absolute path {
switch -glob -- $path {
/* {return $path}
* {return [file join [pwd] $path]}
}
}

-Alex

Uwe Klein

unread,
Sep 5, 2015, 8:50:04 AM9/5/15
to
you get the path element that is handed as arg to [source].

If source got handed an absolute path your already "there"

if source got handed a relative path you have to append
this to the current working directory.

i.e.
set pwd [pwd]
set filename [info script]

switch -- [file pathtype $filename] \
relative {
set fullpath [ [ file join $pwd $filename]
} default {
set fullpath $filename
}

uwe

Robert Heller

unread,
Sep 5, 2015, 9:25:15 AM9/5/15
to
And [file normalize $path] might also be useful:

% file normalize [file join [pwd] ../foo.tcl]
/home/foo.tcl


>
>
>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Rich

unread,
Sep 5, 2015, 10:21:59 AM9/5/15
to
You are looking for [file normalize]

file normalize name
Returns a unique normalized path representation for
the file-system object (file, directory, link, etc),
whose string value can be used as a unique identifier for
it. A normalized path is an absolute path which has all
"../" and "./" removed. Also it is one which is in the
"standard" format for the native platform. On Unix, this
means the segments leading up to the path must be free of
symbolic links/aliases (but the very last path component
may be a symbolic link), and on Windows it also means we
want the long form with that form's case-dependence
(which gives us a unique, case-dependent path). The one
exception concerning the last link in the path is
necessary, because Tcl or the user may wish to operate on
the actual symbolic link itself (for example file delete,
file rename, file copy are defined to operate on symbolic
links, not on the things that they point to).

Example:

/tmp/subdir1/subdir2$ cat this-script.tcl
#!/usr/bin/tclsh

puts "\[info script]=[info script]"
puts "\[file normalize \[info script]]=[file normalize [info script]]"

/tmp/subdir1/subdir2$ ./this-script.tcl
[info script]=./this-script.tcl
[file normalize [info script]]=/tmp/subdir1/subdir2/this-script.tcl

/tmp/subdir1/subdir2$ tclsh this-script.tcl
[info script]=this-script.tcl
[file normalize [info script]]=/tmp/subdir1/subdir2/this-script.tcl

/tmp/subdir1/subdir2$ ../subdir2/this-script.tcl
[info script]=../subdir2/this-script.tcl
[file normalize [info script]]=/tmp/subdir1/subdir2/this-script.tcl

/tmp/subdir1/subdir2$ tclsh ../subdir2/this-script.tcl
[info script]=../subdir2/this-script.tcl
[file normalize [info script]]=/tmp/subdir1/subdir2/this-script.tcl

Chawki Jreige

unread,
Sep 5, 2015, 5:27:11 PM9/5/15
to
Thank you for the responses. Just what I needed to know!

heinrichmartin

unread,
Sep 8, 2015, 4:26:33 AM9/8/15
to
On Saturday, September 5, 2015 at 2:35:00 PM UTC+2, Alexandre Ferrieux wrote:
> The value returned by [info script] is the one passed to [source] or as an argument to tclsh. If this value is an absolute path, you'll get it back unmodified. If it is relative or without "/", you'll get it "as is".
> Just prepend with [pwd] to get back a full path:
>
> proc absolute path {
> switch -glob -- $path {
> /* {return $path}
> * {return [file join [pwd] $path]}
> }
> }
>
> -Alex

afaik, the distinction is unnecessary as file join discards paths that precede absolute paths:

expect:/tmp$ file join [pwd] a/b
/tmp/a/b
expect:/tmp$ file join [pwd] /a/b
/a/b

micro-performance might be a reason though:

expect:/tmp$ time {pwd} 100000
0.96053 microseconds per iteration
expect:/tmp$ time {switch -glob -- /a/b /* {} * {pwd}} 100000
0.20309 microseconds per iteration
expect:/tmp$ set path /a/b
/a/b
expect:/tmp$ time {switch -glob -- $path /* {} * {pwd}} 100000
0.3763 microseconds per iteration

regards,
Martin

Donal K. Fellows

unread,
Sep 8, 2015, 5:05:43 AM9/8/15
to
On 08/09/2015 09:26, heinrichmartin wrote:
> micro-performance might be a reason though:

It's definitely not a good reason! Write the code to be clear and
understandable first. What's more, you can do much to make things
clearer to you while boosting performance. (For example, you usually
only need to make paths absolute at program startup time, when users
tend to be relatively patient with code being somewhat slower; after
that, everything is already clear as possible.)

Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.

Alexandre Ferrieux

unread,
Sep 8, 2015, 5:32:35 PM9/8/15
to
On Tuesday, September 8, 2015 at 11:05:43 AM UTC+2, Donal K. Fellows wrote:
> On 08/09/2015 09:26, heinrichmartin wrote:
> > micro-performance might be a reason though:
>
> It's definitely not a good reason! Write the code to be clear and
> understandable first.

And indeed that wasn't. I had simply forgotten that property of [file join] (pruning left of //)...

-Alex

Andreas Kupries

unread,
Oct 17, 2015, 2:08:31 AM10/17/15
to
Robert Heller <hel...@deepsoft.com> writes:
> And [file normalize $path] might also be useful:
>
> % file normalize [file join [pwd] ../foo.tcl]
> /home/foo.tcl

Can be done simpler:

% file normalize ../foo.tcl

The normalize automatically uses CWD for relative paths.


--
So long,
Andreas Kupries <akup...@shaw.ca>
<http://core.tcl.tk/akupries/>
Developer @ HP Enterprise

Tcl'2015, Oct 19-23, Manassas, VA, USA, http://www.tcl.tk/community/tcl2015/
-------------------------------------------------------------------------------
0 new messages