Hi all,
It is possible to pass command line to pascal:
Example:
ls uname -r
Thanks

Mailing lists / fpc-pascal / 2013-06
27 messages · 7 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi all,
It is possible to pass command line to pascal:
Example:
ls uname -r
Thanks
You can pass parameters to any program. But maybe your question is more like "how do I access these parameters?". To answer this, please take a look at the documentation:
http://www.freepascal.org/docs-html/rtl/system/paramstr.html http://www.freepascal.org/docs-html/rtl/system/paramcount.html
Regards,
Sven
May be he means to execute a command in a shell. ?
Yes
On Sat, Jun 29, 2013 at 10:42 PM, Rainer Stratmann <
you wroteYes
It is possible.
http://www.freepascal.org/docs-html/rtl/unix/fpsystem.html
I Tried, but only return an integer not a string value.
FWIW: top posting is like reading a book backwards... please post below existing quotes...
Johan Tu Toit wroteI Tried, but only return an integer not a string value.
what were you expecting to see as a result?
return string
So you want to run a program and get its output, am I right? In that case, have a look at:
http://wiki.lazarus.freepascal.org/Executing_External_Programs Please DON'T skip the little details.
If you use trunk version, there are simple wrappers in process unit to do in in a single line.
you wroteSo you want to run a program and get its output, am I right? In that case,
Rainer Stratmann wroteMay be he means to execute a command in a shell. ?
Johan Tu Toit wroteYes
I Tried, but only return an integer not a string value.
you wroteI Tried, but only return an integer not a string value.
Thats right.
You have to put the result in a file and then afterwards read the file.
ls uname -r > file.txt
The integer is the result code of the execution.
It contains information if the execution was successful.
I'm sorry, but that's misleading on at least two accounts. The first is that redirection (> and so on) is a property of the shell, so it won't work unless you pass the entire command to a shell with (typically) the -c option. The second is that as linked by leledumbo earlier http://wiki.lazarus.freepascal.org/Executing_External_Programs there are specific facilities in the runtimes to do this sort of thing and they don't, as a rule, involve temporary files.
If I had to go on, what's that -r supposed to be doing? Are you trying to execute uname -r and in that case where does the ls come into it? Perhaps what you're really trying to do is something like
ls /boot/vmlinux-`uname -r`
(note those backticks which are NOT standard quotes) in which case you'd be better first getting the uname output and then using standard file operations to get the properties you're interested in. Finally, Linux (but not necessarily other unix implementations) puts version information into /proc/version which is accessible as a standard file- no external program required.
So you want to run a program and get its output, am I right? In that case, have a look at:
In our previous episode, leledumbo said:
If you use trunk version, there are simple wrappers in process unit to do in in a single line.
Afaik they made it to 2.6.2.
Note that you have to pass the shell yourself (/bin/sh but note that might not always be bash).
I'm sorry, but that's misleading on at least two accounts. The first is that redirection…
It is not too difficult to find out.
If you speed up your engine then you can find out that he wants to know the actual Linux kernel number.
'uname -r'
With 'uname -r > file.txt' you redirect the output in a file. Then cou can read the file.
I am sorry, but you try to make a very simple thing complicated.
In our previous episode, Rainer Stratmann said:
If you speed up your engine then you can find out that he wants to know the actual Linux kernel number.
Then why doesn't he simply use
uses baseunix;
var u: UtsName;
begin
fpuname(u);
writeln(u.release);
end.Because he didn't know before, I guess :-)
It is not too difficult to find out. If you speed up your engine then you can find out…
In which case why is he putting ls in front of it? -r is a valid option to both uname and ls, so since we're already looking at a broken command it's entirely valid to ask which it's supposed to apply to.
With 'uname -r > file.txt' you redirect the output in a file. Then cou can read the file. I am sorry, but you try to make a very simple thing complicated.
You might call having to invoke a shell with the -c option to get the redirection working, working out where to put the temporary file, deleting it on completion and making sure that all error conditions are handled "simple". I certainly don't.
But as Marco has already reminded us, the information is already available from a standard library call. Calling an external program is not something to be approached for a trivial reason, unless you're writing shell or in a scripting language such as Perl where it might be less trouble than searching CPAN for the appropriate extension module.
return string
Johan Tu Toit wroteOn 6/30/13, waldo kitty wrote: > > FWIW: top posting is like reading a book backwards... please post below > existing quotes...
*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
i fixed your quotes and reply...
what string? the program executed from within your program doesn't return strings... the programs termination code is numeric...
on the other hand, the program executed from within your program may emit some text to the screen... this text may be being sent to stdout or stderr or both... there are methods of capturing this output... is this what you are asking for?
Thats right. You have to put the result in a file and then afterwards read the file. ls…
what?! that's not right... i've never done that... not even with Turbo Pascal 6... it does take a bit of setup to be able to capture the output of a program executed from within one's program but once the routines are written, they are easy to use... i'm extremely sure that FPC has such routines available...
The integer is the result code of the execution. It contains information if the execution was successful.
true...
In which case why is he putting ls in front of it? -r is a valid option to both uname and…
you wroteIn which case why is he putting ls in front of it?
I guess he simply does not know by now and tries out something.
-r is a valid option to both uname and ls, so since we're already looking at a broken command it's entirely valid to ask which it's supposed to apply to.
I bet he wants to know the Linux version.
'uname -r' is the unambiguous assignment for that.
It is not necessary to delete the file.
All error conditions you can read in the man page of the command.
But as Marco has already reminded us, the information is already available from a standard library call.
Very good! Then you bet, too?
Calling an external program is not something to be approached for a trivial reason,
It works perfectly with linux. And is simple. The command is executed like every other function in your pascal program.
unless you're writing shell or in a scripting language such as Perl where it might be less trouble than searching CPAN for the appropriate extension module.
Why do you try to make it magotty and more complicated than necessary?
In which case why is he putting ls in front of it? -r is a valid option to both uname and…
agreed... the example given by the OP is incorrect to start with...
$ ls uname -r
ls: cannot access uname: No such file or directory$ sudo ls uname -r
[sudo] password for user:
ls: cannot access uname: No such file or directory
perhaps someone should slow down their engine and look closer at what is posted? ;)
I guess he simply does not know by now and tries out something. I bet he wants to know…
don't guess... others cannot learn from assumptions and guesses...
while that is true, there is still the ambiguous 'ls' in front which will return an error of "file not found" and an exit code of 1...
so you propose to leave temporary files laying about all over the place polluting the filesystem?
All error conditions you can read in the man page of the command.
only if man is installed and if the manpages for that command exist... this cannot be assumed to be true all the time and for all situations...
> But as Marco has already reminded us, the information is already > available from a standard library call. Very good! Then you bet, too?
only IF that is the information the OP is looking for... we don't know yet what that information is because the leading 'ls' has not been addressed...
but capturing the output is not simple... redirection is a function of the shell and your example doesn't load the shell to start with...
make it what?? you cannot redirect without the shell or by taking additional steps to emulate what the shell does to allow redirecting...
agreed... the example given by the OP is incorrect to start with... $ ls uname -r ls:…
perhaps someone should slow down their engine and look closer at what is posted? ;)
hahaha
agreed... the example given by the OP is incorrect to start with... $ ls uname -r ls:…
sorry it is my fault it was suppose to be: $uname -r
don't guess... others cannot learn from assumptions and guesses... while that is true,…
you wrote> It is not necessary to delete the file. so you propose to leave temporary files laying about all over the place polluting the filesystem?
hahaha
All over the world!
sorry it is my fault it was suppose to be: $uname -r
Johan Tu Toit wrotesorry it is my fault it was suppose to be: $uname -r
ok... thank you for clarifying that ;)
have you learned what you need to do from the link leledumbo gave earlier in this thread?
http://wiki.lazarus.freepascal.org/Executing_External_Programs
In our previous episode, waldo kitty said:
I added a short paragraph about runcommand