[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to change directory of work?


From one of my projects, I hope this helps

# Current Working Directory

Gsh manages the current working directory for the shell environment. The
processes current working directory is set after each command execution.
The env variable PWD and the global variable \$pwd are set after each
function to the current processes working directory.

Some cli functions use the env variable PWD others use the process cwd
current working directory. The shell attempts to keep both in sync.

Linux process table contains an inode reference to the current working
directory. The shell keeps this in sync with the PWD environment
variable.

Using this chdir and setting PWD to the current directories. It is possible to manage the actual current directory in the gambas environment.

Extern chdir(Dest As String) As Integer In "libc:6"

'Try this in your app: to sync where the os thinks you are and where cli commands think you are

     Try chdir(Env["PWD"])              ' but be aware to change back after you are done

Full Example script follow:

#!/usr/bin/env gbs3
' Gambas Script File Created 04/14/2025 22:49:56
Extern chdir(Dest As String) As Integer In "libc:6"
Extern getcwd(buf As Pointer, size As Integer) As String In "libc:6"

Dim dirbuf As Pointer = Alloc(2048)
Print "start at ";; getcwd(dirbuf, 2048)
Try Mkdir "/tmp/junk1"
chdir("/tmp/junk1")
Print "now at ";; getcwd(dirbuf, 2048)
env["PWD"] = getcwd(dirbuf, 2048)               ' keep both the system cwd and the cli PWD in sync
Dim x As File
x = Open getcwd(dirbuf, 2048) &/ "this" For Write Create
Print #x, " this message"
Close x
Exec ["ls", "/tmp/junk1"]

Quit 0
Catch
Error "Script Error >";; error.text & "\n" & error.where


On 4/14/25 15:38, T Lee Davidson wrote:
On 4/14/25 3:59 PM, Benoît Minisini wrote:
Le 14/04/2025 à 18:49, T Lee Davidson a écrit :
Otherwise you can actually specify the working directory in Exec or Shell by specifying it the "PWD" environment variable.

Exec ["pwd"] With ["PWD=/root"] Wait

It's an undocumented Gambas feature.

Actually, Benoît, it is documented on the SHELL page in the "Environment" section:
https://gambaswiki.org/wiki/lang/shell#t3


Sorry, but I don't see that this specific feature has been documented. I mean that if you specify "PWD=XXX" in the process environment, it is detected by the Gambas interpreter, and then the current working directory of the executed process is set to XXX. It's really an hack. This is why I didn't document it. I even don't know why I'm currently talking about that. :-)


Lol.

Okay, I see. You're talking specifically about the fact, that Gambas will actually detect and respect the value of the PWD environment variable, not being documented. I stand corrected. :-)


--
~~~~ Brian

Attachment: OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


References:
how to change directory of work?roberto.premoli@xxxxxxxxxx
Re: how to change directory of work?Bruce Steers <bsteers4@xxxxxxxxx>
Re: how to change directory of work?Benoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
Re: how to change directory of work?T Lee Davidson <t.lee.davidson@xxxxxxxxx>
Re: how to change directory of work?Benoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
Re: how to change directory of work?T Lee Davidson <t.lee.davidson@xxxxxxxxx>