Author Topic: How to call terminal using Gambas?  (Read 465 times)

prathviraj

  • PrathviRaj
  • Newbie
  • *
  • Posts: 5
  • Student
    • Apsotech
How to call terminal using Gambas?
« on: October 26, 2011, 08:47:31 AM »
I am new to gambas but pretty experienced in vb. I want to develop GUI for shell script. Please explain me how to use terminal in gambas. Eg: How can I identify the number of users logged into the server using gambas.
Thank U
PrathviRaj

Linux Basic

How to call terminal using Gambas?
« on: October 26, 2011, 08:47:31 AM »

raindog

  • Jr. Member
  • **
  • Posts: 58
  • Gambas contributor
    • my homepage
Re: How to call terminal using Gambas?
« Reply #1 on: October 26, 2011, 01:00:58 PM »
You can run any command directly without using a terminal using either the SHELL or EXEC commands. SHELL is usually easier to deal with because it just launches a shell with whatever command line you put in, handles wildcards and all. EXEC takes each argument to the program separately as an array, and runs the program directly without using a shell, which may be faster or avoid problems with wildcards when you don't need them.

DIM whooutput AS String
DIM numusers AS Integer
SHELL "who | wc -l" TO whooutput

Some commands produce output constantly, and for those you can use SHELL or EXEC using the READ and WRITE arguments to read their output like a file handle each time output is produced. Other commands, like ssh, expect the user to be sitting at a terminal typing, and you can fake that using the SHELL command with its INPUT and OUTPUT arguments.

prathviraj

  • PrathviRaj
  • Newbie
  • *
  • Posts: 5
  • Student
    • Apsotech
Re: How to call terminal using Gambas?
« Reply #2 on: October 27, 2011, 11:20:13 AM »
Thank U for the replay. How to take values from shell script outputs to gambas variable?  Could u suggest any tutorial to lean using shell scripts in Gambas?
PrathviRaj

raindog

  • Jr. Member
  • **
  • Posts: 58
  • Gambas contributor
    • my homepage
Re: How to call terminal using Gambas?
« Reply #3 on: October 27, 2011, 12:48:53 PM »
The example I provided will put the shell script output in the Gambas variable called "whooutput". It's a string, so you'd then have to extract the number from the string to determine how many logged-in users there are (probably with val(whooutput)).

I've been doing Unix shellscripts for about 24 years at this point, so I'm afraid I wouldn't know where a beginner should look. There isn't much to learn regarding using them in Gambas specifically; what I told you above, plus a trip to gambasdoc.org to look for the SHELL and EXEC keywords, should be enough to get you on your way.

http://www.freeos.com/guides/lsst/ claims to be a shell script tutorial for beginners. What I saw of it seems okay. O'Reilly has books about it too, if you prefer paper. But basically, any set of commands you type in a shell can be put into a shell script. The following one-line shell script I wrote years ago will tell you what the resolution and codec of one or more video files are, by looping through them, running mplayer with some parameters on each one, filtering the output to include only the relevant lines, and prepending the filename to the resulting output:

#!/bin/bash
for f in "$@"; do echo $f: `mplayer -frames 90 -ao null -vo null -nofs -noloop $f 2>&1 | grep VIDEO`; done

The for loop (with a variable), backquotes and pipe used in that example are probably the three most common techniques you need to do what you want to do in a shell script. The "#!/bin/bash" comment goes at the beginning of the file to tell Linux that it's a bash script.

Edit: depending on how new you are to Linux/Unix, you may also need to know that "$@" in quotes gives you a list of the arguments provided to the script, "2>&1" includes error output in the regular output of a program (normally they're split), and "grep" filters output to include only lines matching its first argument.
« Last Edit: October 27, 2011, 12:54:48 PM by raindog »

prathviraj

  • PrathviRaj
  • Newbie
  • *
  • Posts: 5
  • Student
    • Apsotech
Re: How to call terminal using Gambas?
« Reply #4 on: October 29, 2011, 07:37:26 AM »
Thank you sir, I wanted to know is it possible to get the server replies into gambas directly? For Eg: Is it possible to display the message sent by other person in the office network(LAN)?
« Last Edit: October 31, 2011, 10:52:12 AM by prathviraj »
PrathviRaj