How to Call BASH Commands From Within PureBasic?

Linux specific forum
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

How to Call BASH Commands From Within PureBasic?

Post by oldefoxx »

I want bash to do some things for me. And I know to use RunProgram() to make it happen. I read the help file, but while it runs fine, I get no results. I want to learn what I am doing wrong. Here are a few of the things I want to do that are straight forward in a terminal or script:

$ pwd ; get the present working directory
$ echo ~ ; get user's home directory
$ echo $HOME ; same as previous
$ sudo updatedb ; prep for following command. Note the user password will be requested when this command is executed.
$ locate ui. ; search the databases for a supplied but partial
;.......................................................... path/filename.ext "pattern" match, Like
l.......................................................... FindString() done externally. Need to capture
;.......................................................... the StdOut from this program and parse it into
;.......................................................... an array line by line.

$ locate > tmp.file; cat tmp.file ; allow for a piped call in RunProgram()

This is a slightly tricky one. I'm conducting two different grep searches of existing files, one to find "purebasic". the other to find "pb", combine them into tmp.1, then using sort on tmp.1 to reorganize the lines sequentially and write these to tmp.2, then using awk to out duplicate lines from tmp.2 and copying the rest back to tmp.1. Then using a combined cat tmp.1 | less command so that I can screen the results.

At this point I could add a step to screen out the binary files as well, but I chose not to. A little bit of study on what's where might be useful in the future.

Code: Select all

 grep -riHn purebasic --include="*.pb*" --include="*.txt" --include="*.chm" > tmp.1; grep -riHn pb --include="*.pb*" --include="*.txt" --include="*.chm" --include="&.pbf"  >> tmp.1; sort -n tmp.1 > tmp.2; awk '!x[$0]++' tmp.2 > tmp.1; rm tmp.2; cat tmp.1 | less
Not being all that familiar with bash, I had to keep going to the internet and asking google how do I do this and how do I do that. With typos always getting in the way, it took a few hours to piece this together. Still a lot faster than programming it myself. That's why I want to enhance my PureBasic efforts with what's already out there in terms of utilities via RunProgram(). It makes it all platform dependent, but Windows developers can do much the same thing at their end.

A question comes to mind, is that every time you open a terminal window, you start off as user and have to use some variation of the su or sudo command to become super user, which requires a password entry. Could that be faked? Could you pass the password to the use of "sudo -i" or "sudo -su", or to the first occurance of "sudo in a command? And would it carry forward to the use of additional sudo commands while running the same program, so you don't have to keep entering your password over and over as you go through steps that might be best done in a script file?

You can certainly generate a script file, which is just a text-based file, and then to a "chmod +x filename" to make it executable, then execute it via the RunProgram() option. but how persistent is that RunProgram() with respect to sudo?

And there is the shell itself. Maybe you want to preceed "find" with "sudo find", and maybe for a different command you want a specific shell like /bin/bash, so you might want to do "/bin/bash echo" rather than default to doing "[/bin/sh] echo" or vice versa.
Should you just do "sudo" or "/bin/bash" as the filename and pass the rest as a command line argument? I was getting "Finished" remarks from the compiler as I tried different things, but getting no results, except for a quick flash as a message came up and went away.. Take the same line and run it in a terminal window, and I got results.
has-been wanna-be (You may not agree with what I say, but it will make you think).
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: How to Call BASH Commands From Within PureBasic?

Post by Lebostein »

I have the same question. How I start Windows command line tools like "start" or "call" with PureBasic? With RunProgram() this don't work....
http://ss64.com/nt/start.html
http://ss64.com/nt/call.html
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to Call BASH Commands From Within PureBasic?

Post by Marc56us »

Lebostein wrote:I have the same question. How I start Windows command line tools like "start" or "call" with PureBasic? With RunProgram() this don't work....
http://ss64.com/nt/start.html
http://ss64.com/nt/call.html
RunProgram do for PB what Start do for Windows, so Start in not needed to run program from PB
START
Start a program, command or batch script (opens in a new window.)


Call is an internal command of CMD (cmd.exe and old command.com), you can't call it directly.
CALL
Call one batch program from another, or call a subroutine.


Historically, the CALL command was used to compel a BATCH script called by another to return to the calling program once it has finished processing.
The old batch files (*.bat) launch by command.com, run action line by line but does not store all lines in memory, so does not return to main file when not using 'call' command.
(many news sysadmin forget this command, and does not understand why the main batch stop after calling another)
This is not the case with new shell command cmd.exe who store all batch line. So 'Call' command is now not necessary

:wink:
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Re: How to Call BASH Commands From Within PureBasic?

Post by oldefoxx »

Very informative posts. Will be helpful when I get free of my present concerns.
has-been wanna-be (You may not agree with what I say, but it will make you think).
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to Call BASH Commands From Within PureBasic?

Post by Marc56us »

With #PB_Program_Wait RunProgram() will wait as far as end of the main script/program ($filename)
RunProgram(Filename$,

No matter what program that it launches, RunProgram will wait until the end of it and only it.
Whatever if main script call another.
If Program is a script, it wait until last line.
No need to put "exit 0" at end of script

Example (/tmp/hello.sh)

Code: Select all

#!/bin/bash
echo "Hello World"   > /tmp/rapport.txt
echo "Hello again"  >> /tmp/rapport.txt
echo "Bye"          >> /tmp/rapport.txt
sleep 3
This script is very fast, so to confirm that RunProgram() wait, I put a 3 sec pause at end

Don't forget #!/bin/bash at top of script
Don't forget # chmod +x /tmp/hello.sh

Code: Select all

Debug "Start (End will appear 3 sec after last script line execution)"
RunProgram("/tmp/hello.sh", "", "", #PB_Program_Wait)
Debug "End - Main script is all done"
Now try

Code: Select all

#!/bin/bash
echo "Hello World"   > /tmp/rapport.txt
echo "Hello again"  >> /tmp/rapport.txt
echo "Bye"          >> /tmp/rapport.txt
xterm
sleep 3 
RunProgram() wait until you close xterm

Change « xterm » to « xterm & »
Script no wait for user to close

RunProgram() always watching the first program and in the case of a script expects the final line.
So put all you commands in a script then launch it

Usage of "sudo -i" is strongly disapproved.
Or you are a member of the "su group" and this is your password your must provide for sudo, or this is a job for root user himself.

:wink:
Post Reply