Posted: Sun Nov 23, 2008 11:20 am
Any another ideas for getting the return of the command "which" ?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
program.s = "pbcompiler"
p = popen_("/usr/bin/which " + program, "r")
buffer.s = Space(256)
out.s = ""
While Not feof_(p)
n = fread_(@buffer, 1, 256, p)
PokeC(@buffer + n * SizeOf(character), 0)
out + buffer
Wend
pclose_(p)
Debug Len(out)
Debug out
GUI Shortcut !flaith wrote:How do you run Purebasic : By a batch or by a GUI Shortcut ?
remi_meier wrote:Not very nice (not for unicode and no error-checking), but for the
meantime, it could be useful![]()
Code: Select all
program.s = "pbcompiler" p = popen_("/usr/bin/which " + program, "r") buffer.s = Space(256) out.s = "" While Not feof_(p) n = fread_(@buffer, 1, 256, p) PokeC(@buffer + n * SizeOf(Character), 0) out + buffer Wend pclose_(p) Debug Len(out) Debug out
Code: Select all
which which
Code: Select all
update-alternatives --list which
Code: Select all
franklin@novatux-laptop:~$ which which
/usr/bin/which
Code: Select all
franklin@novatux-laptop:~$ update-alternatives --list which
Pas d'alternatives pour which.
Code: Select all
franklin@novatux-laptop:~$ which ls
/bin/ls
franklin@novatux-laptop:~$ which man
/usr/bin/man
This code works for me. (PB 4.30 beta4, should work with earlier versions too though).Progi1984 wrote:I tried this code :And Sortie$ returns ""Code: Select all
Compilateur = RunProgram("which", "pbcompiler ", "", #PB_Program_Open|#PB_Program_Read) Sortie$ = "" If Compilateur While ProgramRunning(Compilateur) Sortie$ + ReadProgramString(Compilateur) + Chr(13) Wend Sortie$ + Chr(13) + Chr(13) Sortie$ + "Code de retour : " + Str(ProgramExitCode(Compilateur)) EndIf MessageRequester("Sortie", Sortie$)
Code: Select all
Debug GetEnvironmentVariable("PATH")
Code: Select all
echo $PATH
Code: Select all
If system_("which bash >/tmp/which_test.txt 2>/dev/null") = 0
If ReadFile(0, "/tmp/which_test.txt")
Debug ReadString(0)
CloseFile(0)
Else
Debug "-- cannot read output file --"
EndIf
Else
Debug "-- calling which failed or target file not found --"
EndIf
DeleteFile("/tmp/which_test.txt")
Code: Select all
Compilateur = RunProgram("which", "pbcompiler ", "", #PB_Program_Open|#PB_Program_Read)
Sortie$ = ""
If Compilateur
While ProgramRunning(Compilateur)
Sortie$ + ReadProgramString(Compilateur) + Chr(13)
Wend
Sortie$ + Chr(13) + Chr(13)
Sortie$ + "Code de retour : " + Str(ProgramExitCode(Compilateur))
EndIf
MessageRequester("Sortie", Sortie$)
Code: Select all
Debug GetEnvironmentVariable("PATH")
Code: Select all
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Code: Select all
echo $PATH
Code: Select all
franklin@novatux-laptop:~$ echo $PATH
/media/DISK/Programs/purebasic/compilers:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Code: Select all
#purebasic
export PUREBASIC_HOME=/media/DISK/Programs/purebasic
export PATH=$PUREBASIC_HOME/compilers:$PATH
Code: Select all
If system_("which bash >/tmp/which_test.txt 2>/dev/null") = 0
If ReadFile(0, "/tmp/which_test.txt")
Debug ReadString(0)
CloseFile(0)
Else
Debug "-- cannot read output file --"
EndIf
Else
Debug "-- calling which failed or target file not found --"
EndIf
DeleteFile("/tmp/which_test.txt")
Code: Select all
If system_("which pbcompiler >/tmp/which_test.txt 2>/dev/null") = 0
If ReadFile(0, "/tmp/which_test.txt")
Debug ReadString(0)
CloseFile(0)
Else
Debug "-- cannot read output file --"
EndIf
Else
Debug "-- calling which failed or target file not found --"
EndIf
DeleteFile("/tmp/which_test.txt")
Affirmative, thanks for your answer et explanationsfreak wrote:> Which error do I have ?
Well, your code has no error. The returnvalue of 1 tells you that which simply could not find the PureBasic compiler.
The .bashrc setting only affects the console, not the GUI-started PureBasic IDE. Try to compile the code to an executable and run it from the console. This should show the result you expect.
> And how can I solve it ?
You just have to be prepared for the fact that which may not find the purebasic path. (either because your program is run from the GUI, or because the user never added purebasic to the path anywhere)
If the exitcode is not 0, just tell the user that PureBasic was not found and offer a way to enter the path manually. Thats all you can do.