Using bash commands from PB?

Linux specific forum
John Duchek
User
User
Posts: 83
Joined: Mon May 16, 2005 4:19 pm
Location: St. Louis, MO

Using bash commands from PB?

Post 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
John R. Duchek
St. Louis,MO
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Using bash commands from PB?

Post 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)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
broozar
User
User
Posts: 61
Joined: Sat May 08, 2010 11:21 pm
Location: Berlin, Germany

Re: Using bash commands from PB?

Post 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?
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Using bash commands from PB?

Post 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? :)
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Using bash commands from PB?

Post 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?
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Using bash commands from PB?

Post 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)
Image
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
broozar
User
User
Posts: 61
Joined: Sat May 08, 2010 11:21 pm
Location: Berlin, Germany

Re: Using bash commands from PB?

Post 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?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Using bash commands from PB?

Post 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!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 543
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Using bash commands from PB?

Post 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?
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
John Duchek
User
User
Posts: 83
Joined: Mon May 16, 2005 4:19 pm
Location: St. Louis, MO

Re: Using bash commands from PB?

Post 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
John R. Duchek
St. Louis,MO
John Duchek
User
User
Posts: 83
Joined: Mon May 16, 2005 4:19 pm
Location: St. Louis, MO

Re: Using bash commands from PB?

Post by John Duchek »

Never mind...I found what I needed. I was not aware of the RunProgram(). slick.
Again, thanks,
John
John R. Duchek
St. Louis,MO
Post Reply