Forum > Linux
[SOLVED]Bash commands and scripts in Lazarus.
johnathan:
Hi
Can anybody help
I would like to run bash and/or bash script from within Lazarus if this is possible.
Things I am already able to do in Lazarus.
Read a text file into a filelistbox and process the information in Lazarus for example rename a list of files to another directory.
As a starter I would like to run the following bash command from inside a Lazarus program. I will not be affended if it is very simple as it needs to be for me.
Obviously the bash command can be run externally and then load the file into Lazarus or read the file names from a filelistbox into a listbox but I would like try to expand my use of Lazarus.
(bash command) ls -1 > /mnt/trials/fileslist.txt
If you can help many thanks
john
TRon:
--- Quote from: johnathan on October 17, 2023, 07:55:02 pm ---I would like to run bash and/or bash script from within Lazarus if this is possible.
--- End quote ---
Yes, that is possible. See wiki executing external commands.
--- Quote ---(bash command) ls -1 > /mnt/trials/fileslist.txt
--- End quote ---
Especially the piping that you show there is making things overly complicated for nothing. You can 'catch' the output from an external command yourself.
I made up some simple example (showing some variations) for Free Pascal using RunCommand (afaik the most simplified version to run an external command that also 'catches' the output). It can easily be integrated into your Lazarus project. The resulting output is by default stored in a string that you can assign to a memo (property text) or added to a memo.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program run; {$mode objfpc}{$H+} uses classes, process; procedure dodo;const cmd_ls = '/bin/ls'; cmd_bash = '/bin/bash'; cmd_chmod = 'chmod';var rc_result: string; Line: string; Lines: TStringList;begin // run ls command directly (with full path) writeln('>$ ', cmd_ls, ' -l'); if RunCommand(cmd_ls, ['-l'], rc_result) then begin writeln('output:'); writeln(rc_result); writeln; writeln('alternative way of output:'); Lines := TStringList.Create; Lines.Text := rc_result; for Line in Lines do Writeln(Line); Lines.Free; end else writeln('error running command ', cmd_ls); writeln; // run s command directly (without path) writeln('>$ ', 'ls -l'); if RunCommand('ls', ['-l'], rc_result) then begin writeln('output:'); writeln(rc_result); end else writeln('error running command ', 'ls -l'); writeln; // run ls command using bash writeln('>$ ', cmd_bash, ' -c ls -l'); if RunCommand(cmd_bash, ['-c', 'ls -l'], rc_result) then begin writeln('output:'); writeln(rc_result); end else writeln('error running command ', cmd_bash); writeln; // run bash internal command writeln('>$ ', cmd_bash, ' -c compgen -b'); if RunCommand(cmd_bash, ['-c', 'compgen -b'], rc_result) then begin writeln('output:'); writeln(rc_result); end else writeln('error running command ', cmd_bash); writeln; // create a bash script file writeln('creating hello.sh script'); Lines := TStringList.Create; Lines.Add('#! ' + cmd_bash); Lines.Add('echo "Hello bash"'); Lines.SaveToFile('hello.sh'); Lines.Free; writeln; // run chmod commnand directly (without path) to make the script executable writeln('>$ ', cmd_chmod, ' u+x hello.sh'); if RunCommand(cmd_chmod, ['u+x', 'hello.sh'], rc_result) then begin Writeln('modified bash script file permissions'); end else writeln('error running command ', cmd_chmod, ' unable to modify file permissons'); writeln; // run the script directly writeln('>$ ', 'hello.sh'); if RunCommand('hello.sh', [], rc_result) then begin writeln('output:'); writeln(rc_result); end else writeln('error running script'); writeln;end; begin dodo;end.
I am not 100% sure if running it from Lazarus requires the full path to the commands that are being executed (it works when started from a bash terminal window) but you'll notice that soon enough and can easily be fixed if that would to be the case.
Seeing that you do not have many posts icw your question, I assume you are new to Lazarus in which case note that writeln's do not work with Lazarus unless you start your Lazarus application form a terminal window. If you want to 'fix' that then replace the writeln's with adding the strings to a memo or stringlist (or whatever is most convenient for your application)
johnathan:
Hi
Thanks for the reply. I have not had any success so far but will continue to experiment.
I notice that runcommand has been depreciated in Linux which could be a problem.
Meanwhile I have no trouble using bash to generate ".txt" with bash to obtain the data I require and using Linux Lazarus IDE to process the data within them.
Thanks for your time
john
marcov:
--- Quote from: johnathan on October 23, 2023, 07:16:27 pm ---
I notice that runcommand has been depreciated in Linux which could be a problem.
--- End quote ---
Runcommand is not deprecated, some older forms of the command are deprecated in favor of newer ones.
TRon:
--- Quote from: johnathan on October 23, 2023, 07:16:27 pm ---Thanks for the reply. I have not had any success so far but will continue to experiment.
--- End quote ---
You would have to be more specific than that in case you require additional help. All I can say is that it works for me tm (with Lazarus as well).
--- Quote ---I notice that runcommand has been depreciated in Linux which could be a problem.
--- End quote ---
As stated by marcov, only a specific version of runcommand, see also manual :
--- Quote ---The version using CmdLine attempts to split the command line in a binary and separate command-line arguments. This version of the function is deprecated.
--- End quote ---
--- Quote ---Meanwhile I have no trouble using bash to generate ".txt" with bash to obtain the data I require and using Linux Lazarus IDE to process the data within them.
--- End quote ---
And I emphasize again that runcommand returns the output of the command into a string (no matter how many lines that output contains as the line separators are part of that one string as well), so there would be no need to use bash and/or pipe the output from the executed command to a textfile to then have to read it in order to do something with the output.
See also attachment.
Navigation
[0] Message Index
[#] Next page