Why working directory moves with script location?

35 views
Skip to first unread message

Mariusz Grecki

unread,
Oct 18, 2019, 6:27:35 PM10/18/19
to tumbi
the problem is as follows:
I run the general script located at ~/utils (through exec ~/utils/optim.sce) which should run then specific script from the current directory (in fact I run general optimization GUI which calls later the specific goal function).
In scicoslab this runs smoothly while nsp while calling the script from other directory tries to call next script from the same directory (~/utils in my case). The consequence is that I cannot use this useful structure with a general script in some fixed location and all specific functions in my project working directory. Certainly, I can make workaround that somehow (e.g. reading the current directory and then calling scripts respectively), but I wonder whether this behavior was designed to be like that or it is an unforeseen effect? In my opinion, the old way (scicoslab) has much more sense...
Greetings
Mariusz

Jean-Philippe Chancelier

unread,
Oct 21, 2019, 12:21:30 PM10/21/19
to tu...@googlegroups.com
Hello Mariusz,
In nsp, if inside a script you run any command with relative pathnames, pathnames are considered to be relative to the pathname of the script. 
Thus if ~/utils/optim.sce contains a line like exec('foo.sce') foo.sce will be searched in the directory utils. I know that it's not the scicoslab behaviour but it was done on
purpose since it simplify what you have to do when you prepare a toolbox and you want the scripts to work when the toolbox is moved. In scicoslab, to achieve the same goal it
was necessary to obtain the pathname of the script and do something like  exec('get-pathname-of'("optim.sce")+"/foo.sce") and it was necessary to change your script if the script name was changed or it was necessary to make a cd before running the script.
In nsp you can use getcwd() to know the current working dir
and  get_current_exec_dir() and get_current_exec_file() to know where are the script which are currently being executed.

Regards
jpc
--
You received this message because you are subscribed to the Google Groups "tumbi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tumbi+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tumbi/bb5bf847-8e42-49ec-b56f-9137cfa8b2a1%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages