Page 1 of 1
Using bash commands from PB?
Posted: Tue Mar 03, 2015 3:13 pm
by John Duchek
Is it possible to issue a bash command from pb and get a return? For example, is there a way to get a current directory using 'pwd' and get a return of '/home/john' ?
If there is I would really like to know how. I could use that kind of function often.
Thanks,
John
Re: Using bash commands from PB?
Posted: Tue Mar 03, 2015 7:49 pm
by bbanelli
John Duchek wrote:Is it possible to issue a bash command from pb and get a return? For example, is there a way to get a current directory using 'pwd' and get a return of '/home/john' ?
If there is I would really like to know how. I could use that kind of function often.
Thanks,
John
Something like this?
Code: Select all
*MemoryID = AllocateMemory(100)
pwd = RunProgram("pwd","",GetCurrentDirectory(),#PB_Program_Open|#PB_Program_Read)
ReadProgramData(pwd,*MemoryID,100)
Debug PeekS(*MemoryID,-1,#PB_Ascii)
Re: Using bash commands from PB?
Posted: Tue Mar 03, 2015 11:23 pm
by broozar
out of curiosity, any reason why you chose ReadProgramData over ReadProgramString? Since -String does not make you mess with buffers, peeks and formats/encodings, it seems to me to be the safer method. am I wrong?
Re: Using bash commands from PB?
Posted: Wed Mar 04, 2015 9:21 am
by bbanelli
broozar wrote:out of curiosity, any reason why you chose ReadProgramData over ReadProgramString? Since -String does not make you mess with buffers, peeks and formats/encodings, it seems to me to be the safer method. am I wrong?
Because nobody expects the Spanish inquisition!
That "safer" method can cost you some time and nerves if there is an ambiguous output of the program whose output you expect. So, IMO, it is definitely much safer to use "Data" method, since it will either work as its "String" function, or actually produce proper results when needed. And it really isn't so hard to use, is ti?

Re: Using bash commands from PB?
Posted: Wed Mar 04, 2015 10:09 am
by Danilo
bbanelli wrote:Code: Select all
*MemoryID = AllocateMemory(100)
pwd = RunProgram("pwd","",GetCurrentDirectory(),#PB_Program_Open|#PB_Program_Read)
ReadProgramData(pwd,*MemoryID,100)
Debug PeekS(*MemoryID,-1,#PB_Ascii)
Aren't Linux programs able to output Unicode characters to the console? Or is the console output UTF8?
Re: Using bash commands from PB?
Posted: Wed Mar 04, 2015 11:01 am
by bbanelli
Danilo wrote:bbanelli wrote:Code: Select all
*MemoryID = AllocateMemory(100)
pwd = RunProgram("pwd","",GetCurrentDirectory(),#PB_Program_Open|#PB_Program_Read)
ReadProgramData(pwd,*MemoryID,100)
Debug PeekS(*MemoryID,-1,#PB_Ascii)
Aren't Linux programs able to output Unicode characters to the console? Or is the console output UTF8?
To be honest, I wasn't paying much attention, but you are probably right, it should.
Code: Select all
bubba@ubuntu-x86:~$ echo $LANG
en_US.UTF-8
Code: Select all
*MemoryID = AllocateMemory(100)
pwd = RunProgram("pwd","",GetCurrentDirectory(),#PB_Program_Open|#PB_Program_Read)
ReadProgramData(pwd,*MemoryID,100)
Debug "No flag: " + PeekS(*MemoryID,-1)
Debug "ASCII flag: " + PeekS(*MemoryID,-1, #PB_Ascii)
Debug "UTF-8 flag: " + PeekS(*MemoryID,-1, #PB_UTF8)

Re: Using bash commands from PB?
Posted: Wed Mar 04, 2015 1:43 pm
by broozar
now who said it was safer

i made the comment because i noticed that my output of "999" was returning nonsense when using Data+PeekI (result: funny long row of numbers), then i tried Data+PeekS with no format parameter (result: empty string), then Data+PeekS with UTF8 (proper result), so then I thought "... hang on a minute, why not use -String to begin with?", and that's how it came about.
do you think it is required/a good idea to parse the LANG env variable before interpreting the output, so you can set Ascii or UTF8 before using PeekS?
Re: Using bash commands from PB?
Posted: Wed Mar 04, 2015 1:56 pm
by ts-soft
broozar wrote:do you think it is required/a good idea to parse the LANG env variable before interpreting the output, so you can set Ascii or UTF8 before using PeekS?
Is a good idea, but "normally", linux use only utf-8!
Re: Using bash commands from PB?
Posted: Wed Mar 04, 2015 2:15 pm
by bbanelli
broozar wrote:now who said it was safer

i made the comment because i noticed that my output of "999" was returning nonsense when using Data+PeekI
Someone will probably correct me if I'm wrong, but stdin, stdout and stderr are always FILE pointer types in return value. I think you cannot expect PeekI to return proper value in that case.
do you think it is required/a good idea to parse the LANG env variable before interpreting the output, so you can set Ascii or UTF8 before using PeekS?
Question prior to this would be is $LANG variable defined standard in all Linux console environments?
Re: Using bash commands from PB?
Posted: Thu Mar 05, 2015 2:51 pm
by John Duchek
Thank you all for the discussion. The code works great. However, I don't understand parts of it.
Is there someplace where this approach is discussed so I could read more about it?
Thanks,
John
Re: Using bash commands from PB?
Posted: Thu Mar 05, 2015 2:55 pm
by John Duchek
Never mind...I found what I needed. I was not aware of the RunProgram(). slick.
Again, thanks,
John