System ()
Vai alla navigazione
Vai alla ricerca
La funzione
int system(const char *__command);
passa il nome del comando o del programma che deve essere eseguito.
Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta. Da sottolineare che, poiché questa funzione esterna di C "system" è omonima alla funzione di Gambas "System", bisognerà assegnarle un nome a piacere, ma si dovrà anche richiamare il suo vero nome con il comando Exec.
Dunque avremo ad esempio:
Private Extern system_C(__command As String) As Integer In "libc:6" Exec "system"
Semplice esempio di uso in Gambas:
' int system(const char *__command)
' Execute the given line as a shell command.
Private Extern system_C(__command As String) As Integer In "libc:6" Exec "system"
Public Sub Main()
' Viene lanciato il programma editor di testo "Gedit":
system_C("gedit")
End
Lo stesso esempio precedente, ma anche aprendo un file di testo:
' int system(const char *__command)
' Execute the given line as a shell command.
Private Extern system_C(__command As String) As Integer In "libc:6" Exec "system"
Public Sub Main()
' Apre il file di testo con il programma "gedit":
system_C("gedit /percorso/del/file/di/testo")
End
Altro esempio:
' int system(const char *__command)
' Execute the given line as a shell command.
Private Extern system_C(__command As String) As Integer In "libc:6" Exec "system"
Public Sub Main()
' Mostra quanto contenuto dalla cartella "/tmp":
system_C("ls /tmp")
End
Altro esempio con file audio:
' int system(const char *__command)
' Execute the given line as a shell command.
Private Extern system_C(__command As String) As Integer In "libc:6" Exec "system"
Public Sub Main()
' Apre ed esegue il file audio con il programma "vlc":
system_C("vlc /percorso/del/file/audio")
End
In quest'altro esempio si colora di rosso il testo scritto in console/Terminale:
' int system(const char *__command)
' Execute the given line as a shell command.
Private Extern system_C(__command As String) As Integer In "libc:6" Exec "system"
Public Sub Main()
' Si imposta il colore (da 0 a 7):
system_C("tput setaf 1")
Print "Testo qualsiasi"
End