Terminal with Shell
Posted
#1
(In Topic #448)
Regular

Code (gambas)
Any suggestions will be very much welcome
Posted
Guru

I also had a job getting standard commands to do anything with the Shell or Exec methods.
Only got it to work by first issuing it the 'bash' command, then Input my command followed by ;exit
then a shell is opened , active till the command is done then quits. process state is 1 while bash shell is active
and goes to 0 after exiting.
Not sure if that's the right way to do it but after many hours experimenting that's the workaround i came up with.
(another option is to periodcally read the TerminalView.Text property and process the text (should be something to compare during and after command)
I did something like this…
Code
Public p as Process
Public t as Timer
Public Sub Form_Open()
t = New Timer As "Tim"
t.Delay = 1000
t.Enabled = True
End
Public Sub Tim_Timer()
If p Then
debug p.State ' Here p.State is either 1 or 0 depending on shell being alive (not exited)
If p.State = 0 Then p.Kill()
Endif
End
Public Sub Button1_Click()
If p Then p.kill ' Just in case
p = TerminalView1.Exec(["bash"]) ' get a valid shell
TerminalView1.Input("ls -a;exit") ' send command followed by ;exit then the process stops and p.state becomes 0
End
Not sure if that helps fella
Posted
Guru

Code (gambas)
- ''NEEDS gb.form.terminal
- TerminalView1 As TerminalView
- Spring1 As Spring
- Setup
- Timer1.Stop
- TerminalView1.Clear
- TerminalView1.SetFocus
- Timer1.Start
- .H = 448
- .W = 448
- .Padding = 5
- .Arrangement = Arrange.Vertical
- .Padding = 5
- .W = 488
- .H = 28
- .W = 100
- .List = ["ls -Ra", "ls -a", "du", "tree"]
- .W = 90
- .Text = "&Go"
- .Delay = 250
Posted
Guru

Nice code Cogier
You get the gist I hope.
Knowing what the terminal view is actually doing internally (idle or not) while it has an active shell in it seems limited but we can monitor the process.status of whether the shell is active.
so trailing your command with ';exit\n' works for that
(i remembered the \n that time
I assume you know the standard Shell command ?
I'll not presume to know your reasons for using the terminal view to run ls but if i want to run a shell command like that and get it's output i'd use the internal Shell command
Ie. to get the output from your 'ls -a' command…
Code
Dim ComText As String
Shell "ls -a" Wait To ComText
Print ComTextAdvantage here of course is in using the 'Wait' argument or not so your program can either launch a command in the background while it continues or it waits for the shell command to be run before continuing.
Posted
Regular

Code (gambas)
- ' Gambas class file
- ' Thanks Charlie for the code
- 'NEEDS gb.form.terminal
- TerminalView1 As TerminalView
- Setup
- Timer1.Stop
- TerminalView1.SetFocus
- .H = 1024
- .W = 1024
- .Padding = 5
- .Arrangement = Arrange.Vertical
- .Padding = 5
- .Delay = 250
- CmdNum = 1
- TermCmd = "ls"
- Timer1.Start
- CmdNum = 1
- TermCmd = " pkexec pacman --sync --refresh --sysupgrade "
- Timer1.Start
- TermCmd = "clear"
- Timer1.Start
- 'Thanks Bruce for this little tid bit.
- Quit 0
I used a menu for the commands which friends prefer. I also put in the command to shut down the terminal otherwise the terminal continues to run after the form is closed. Thanks guys for your input and thanks Charlie once again for the code.
Posted
Guru

Ps. Another way to get your forms to close properly is simply put 'Quit 0' at the end of your form_close procedure. That will kill any lingering unwanted processes on exit <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>
Posted
Regular

Posted
Guru

Mostly my thinking was some problems i've had using the terminal view, for some shell stuff i need a proper terminal lol.
I wondered if the app might help as your program could be shell based to solve issues but still have a GUI for your friends.
Just a thought
1 guest and 0 members have just viewed this.


