Terminal with Shell

Post Reply
cage
Posts: 80
Joined: Monday 02nd September 2019 5:47am
Location: Phoenix Arizona

Terminal with Shell

Post by cage » Sunday 16th August 2020 4:56am

Dose anyone know how to determine when a shell process ends using the vt100 terminal. This is the command I am using in a experimental program I am working on.
TerminalView1.Shell("ls -a")
Any suggestions will be very much welcome

User avatar
BruceSteers
Posts: 149
Joined: Thursday 23rd July 2020 5:20pm

Re: Terminal with Shell

Post by BruceSteers » Sunday 16th August 2020 11:17am

I found the way to use Terminal view like that was to use the Process.State to see if it's active.
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: Select all

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
If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 516
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Terminal with Shell

Post by cogier » Sunday 16th August 2020 3:23pm

I could not get this to work but BruceSteers pointed me in the right direction. I could not get BruceSteers version to work until I added '\n' to the command. Anyway here is my version. Copy the code to a Graphical Application and add the 'gb.form.terminal' component. Hope it helps.
''NEEDS gb.form.terminal

TerminalView1 As TerminalView
ComboBox1 As ComboBox
pProcess As Process
Spring1 As Spring
Button1 As Button
Timer1 As Timer
HBox1 As HBox
HBox2 As HBox

Public Sub Form_Open()

  Setup
  pProcess = TerminalView1.Exec(["bash"])

End

Public Sub Timer1_Timer()

  If pProcess Then
    If pProcess.State = Process.Stopped Then
      Timer1.Stop
      Message("Finished", "OK")
      TerminalView1.Clear
      pProcess = TerminalView1.Exec(["bash"])
      TerminalView1.SetFocus
    End If
  Endif

End

Public Sub Button1_Click()

  Timer1.Start
  TerminalView1.Input(ComboBox1.Text & ";exit\n")

End

Public Sub Setup() 'This just sets up the Form and its components

  With Me
    .H = 448
    .W = 448
    .Padding = 5
    .Arrangement = Arrange.Vertical
  End With

  With HBox1 = New HBox(Me)
    .Expand = True
    .Padding = 5
  End With

  With TerminalView1 = New TerminalView(HBox1)
    .Expand = True
    .Blink = True
  End With

  With HBox2 = New HBox(Me)
    .W = 488
    .H = 28
  End With

  With ComboBox1 = New ComboBox(HBox2) As "ComboBox1"
    .W = 100
    .ReadOnly = True
    .List = ["ls -Ra", "ls -a", "du", "tree"]
  End With

  Spring1 = New Spring(hBox2)

  With Button1 = New Button(HBox2) As "Button1"
    .W = 90
    .Text = "&Go"
    .Picture = Picture["icon:/22/right"]
  End With

  With Timer1 = New Timer As "Timer1"
    .Enabled = True
    .Delay = 250
  End With

End

User avatar
BruceSteers
Posts: 149
Joined: Thursday 23rd July 2020 5:20pm

Re: Terminal with Shell

Post by BruceSteers » Sunday 16th August 2020 5:40pm

aah lol , yeah don't forget the return key \n :D
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: Select all

Dim ComText As String
Shell "ls -a" Wait To ComText
Print ComText
Advantage 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.
If at first you don't succeed , try it differently.

cage
Posts: 80
Joined: Monday 02nd September 2019 5:47am
Location: Phoenix Arizona

Re: Terminal with Shell

Post by cage » Sunday 16th August 2020 9:01pm

Charlies code did work for me. I modified the code to meet my needs. The ls command was one of the commands I was experimenting with. The goal was to create a program that would use some of the off the wall commands for friends who are not comfortable using the terminal command line. Personally I have no problem using the terminal command line for things. Anyways here is my modified code:
' Gambas class file
' Thanks Charlie for the code
'NEEDS gb.form.terminal
 
TerminalView1 As TerminalView
pProcess As Process
Timer1 As Timer
HBox1 As HBox
TermCmd As String
CmdNum As Integer
 
Public Sub Form_Open()
 
  Setup
  pProcess = TerminalView1.Exec(["bash"])
 
End
 
Public Sub Timer1_Timer()
 
  If pProcess Then
    If pProcess.State = Process.Stopped Then
      Timer1.Stop
      If CmdNum = 1 Then
        Message("Finished", "OK")
      Endif
      pProcess = TerminalView1.Exec(["bash"])
      TerminalView1.SetFocus
    End If
  Endif
 
End
 
Public Sub Setup() 'This just sets up the Form and its components
 
  With Me
    .H = 1024
    .W = 1024
    .Padding = 5
    .Arrangement = Arrange.Vertical
  End With
 
  With HBox1 = New HBox(Me)
    .Expand = True
    .Padding = 5
  End With
  
  With TerminalView1 = New TerminalView(HBox1)
    .Expand = True
    .Blink = True
  End With
 
   With Timer1 = New Timer As "Timer1"
    .Enabled = True
    .Delay = 250
  End With
 
End

Public Sub mnuLs_Click()
    CmdNum = 1
    TermCmd = "ls"
   Timer1.Start
  TerminalView1.Input(TermCmd & ";exit\n")

End

Public Sub mnuLUpdate_Click()
  CmdNum = 1
  TermCmd = " pkexec pacman --sync --refresh --sysupgrade "
  Timer1.Start
  TerminalView1.Input(TermCmd & ";exit\n")
  
End

Public Sub mnuClear_Click()

  TermCmd = "clear"
  Timer1.Start
  TerminalView1.Input(TermCmd & ";exit\n")

End

Public Sub mnuClose_Click()
  'Thanks Bruce for this little tid bit.
  Quit 0

End
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.
Last edited by cage on Monday 17th August 2020 3:14am, edited 2 times in total.

User avatar
BruceSteers
Posts: 149
Joined: Thursday 23rd July 2020 5:20pm

Re: Terminal with Shell

Post by BruceSteers » Sunday 16th August 2020 9:46pm

Happy to set you all on the right path 😉

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 😎
If at first you don't succeed , try it differently.

cage
Posts: 80
Joined: Monday 02nd September 2019 5:47am
Location: Phoenix Arizona

Re: Terminal with Shell

Post by cage » Monday 17th August 2020 3:11am

Thank you very much Bruce. That works like a champ.

User avatar
BruceSteers
Posts: 149
Joined: Thursday 23rd July 2020 5:20pm

Re: Terminal with Shell

Post by BruceSteers » Sunday 30th August 2020 2:41pm

Just a thought m8 I dunno if it will help but i did think of you with the GForm app i've made.

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 :)
If at first you don't succeed , try it differently.

Post Reply