Menu

Bash

Krzysztof Kamil Jacewicz

[ATUlib]: Bash

Executes specified command via bash shell. On non UNIX systems it will do nothing, but will still compile, It is blocking, so be careful not to pass a command that will wait for an input that will not happen (see example). This call does not handle output capturing, only the exit code. For capturing output check routines in the [ATUprocess] unit instead.

Function header

Function  Bash(cmd: string): integer;

Parameters

  • cmd - pass any command string that you would pass to a Linux terminal.

Result

Returns the exit code of the executed command or -1 if the specified command was not recognized as a valid shell command.

Example

//this will print files and folders in the current folder to the stdout:
bash('ls');

//this will copy a file:
bash('cp /etc/apt/sources.list ~/');

//this will remove a directory along with all its content:
bash('rm -r ~/SomeFolder');

//This one is even better, because the one above might use interactiv way to ask user 
//to confirm deleting certain files/folders, and since we call it programatically there is 
//no user to to answe the prompt and the command above could actually stay active forever, 
//and since this API is blocking, it could freze the application, so much better would be:
bash('rm -f -r ~/SomeFolder');

//but in case a command still allows for a scenario when a user prompt could freeze the call, 
//you can even send the entire shell command into a background and return immediately:
bash('rm -f -r ~/SOmeFolder &');
//the above call may still stay active until the end of you appliction's execution, but at least 
//it will not freeze the app, because it will send shell command into the background and return.

//this is adviced AGAINST:
bash('sudo touch /etc/apt/sources.list');

//instead, if you must use:
bash('gksudo touch /etc/apt/sources.list');
//because sudo is an interactive shell command, and it would not really work for us,
//while gksudo would actually show a GUI prompt for password, so that user could 
//use it and allow the shell command for actually receiving the input and it 
//could finish and return.

Related

Wiki: ATUlib

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.