Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-06

It is possible to pass command line to pascal

27 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Johan Tu Toit

Hi all,

It is possible to pass command line to pascal:
Example:
ls uname -r

Thanks

Rainer Stratmann
to Sven Barth
you wrote 12 lines
On 29.06.2013 21:27, Johan Tu Toit wrote: > Hi all, > > It is possible to pass command line to pascal: > Example: > ls uname -r 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

May be he means to execute a command in a shell. ?

Johan Tu Toit
to Rainer Stratmann

Yes

On Sat, Jun 29, 2013 at 10:42 PM, Rainer Stratmann <

Show quoted text 21 lines
wrote: On Saturday 29 June 2013 22:40:21 you wrote: > On 29.06.2013 21:27, Johan Tu Toit wrote: > > Hi all, > > > > It is possible to pass command line to pascal: > > Example: > > ls uname -r > > 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 May be he means to execute a command in a shell. ? _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Rainer Stratmann
to Johan Tu Toit
you wrote
Yes

It is possible.

http://www.freepascal.org/docs-html/rtl/unix/fpsystem.html

Show quoted text 23 lines
On Sat, Jun 29, 2013 at 10:42 PM, Rainer Stratmann < > wrote: > On Saturday 29 June 2013 22:40:21 you wrote: > > On 29.06.2013 21:27, Johan Tu Toit wrote: > > > Hi all, > > > > > > It is possible to pass command line to pascal: > > > Example: > > > ls uname -r > > > > 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 > > May be he means to execute a command in a shell. ? > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Johan Tu Toit
to Rainer Stratmann

I Tried, but only return an integer not a string value.

Rainer Stratmann wrote 36 lines
On Saturday 29 June 2013 23:24:44 you wrote: > Yes It is possible. http://www.freepascal.org/docs-html/rtl/unix/fpsystem.html > > > On Sat, Jun 29, 2013 at 10:42 PM, Rainer Stratmann < > > > wrote: > > On Saturday 29 June 2013 22:40:21 you wrote: > > > On 29.06.2013 21:27, Johan Tu Toit wrote: > > > > Hi all, > > > > > > > > It is possible to pass command line to pascal: > > > > Example: > > > > ls uname -r > > > > > > 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 > > > > May be he means to execute a command in a shell. ? > > _______________________________________________ > > fpc-pascal maillist - > > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
waldo kitty
to Johan Tu Toit

FWIW: top posting is like reading a book backwards... please post below existing quotes...

Johan Tu Toit wrote
I Tried, but only return an integer not a string value.

what were you expecting to see as a result?

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Johan Tu Toit
to waldo kitty

return string

waldo kitty wrote 18 lines
FWIW: top posting is like reading a book backwards... please post below existing quotes... On 6/29/2013 18:59, Johan Tu Toit wrote: > I Tried, but only return an integer not a string value. what were you expecting to see as a result? -- NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
leledumbo
to Johan Tu Toit

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.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/It-is-possible-to-pass-command-line-to-pascal-tp5715511p5715519.html Sent from the Free Pascal - General mailing list .
Rainer Stratmann
to leledumbo
you wrote
So you want to run a program and get its output, am I right? In that case,
Rainer Stratmann wrote
May be he means to execute a command in a shell. ?
Johan Tu Toit wrote
Yes
Rainer Stratmann
to Johan Tu Toit: I Tried, but only return an integer not a string value.
you wrote
I 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.

Show quoted text 39 lines
On 6/29/13, Rainer Stratmann wrote: > On Saturday 29 June 2013 23:24:44 you wrote: >> Yes > > It is possible. > > http://www.freepascal.org/docs-html/rtl/unix/fpsystem.html > >> On Sat, Jun 29, 2013 at 10:42 PM, Rainer Stratmann < >> >> > wrote: >> > On Saturday 29 June 2013 22:40:21 you wrote: >> > > On 29.06.2013 21:27, Johan Tu Toit wrote: >> > > > Hi all, >> > > > >> > > > It is possible to pass command line to pascal: >> > > > Example: >> > > > ls uname -r >> > > >> > > 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 >> > >> > May be he means to execute a command in a shell. ? >> > _______________________________________________ >> > fpc-pascal maillist - >> > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Mark Morgan Lloyd
to Rainer Stratmann
Rainer Stratmann wrote 8 lines
On Sunday 30 June 2013 00:59:59 you wrote: > I 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

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.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Rainer Stratmann
to Mark Morgan Lloyd: I'm sorry, but that's misleading on at least two accounts. The first is that redirection…
you wrote 19 lines
Rainer Stratmann wrote: > On Sunday 30 June 2013 00:59:59 you wrote: >> I 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 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?

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.

Marco van de Voort
to Rainer Stratmann

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.
Rainer Stratmann
to Marco van de Voort
you wrote 14 lines
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 :-)

Mark Morgan Lloyd
to Rainer Stratmann: It is not too difficult to find out. If you speed up your engine then you can find out…
Rainer Stratmann wrote 25 lines
On Sunday 30 June 2013 12:32:30 you wrote: > Rainer Stratmann wrote: >> On Sunday 30 June 2013 00:59:59 you wrote: >>> I 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 > 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? 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'

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.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
waldo kitty
to Johan Tu Toit: return string
Johan Tu Toit wrote
On 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...

Show quoted text 7 lines
> > On 6/29/2013 18:59, Johan Tu Toit wrote: >> I Tried, but only return an integer not a string value. > > what were you expecting to see as a result? > return string

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?

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
waldo kitty
to Rainer Stratmann: Thats right. You have to put the result in a file and then afterwards read the file. ls…
Rainer Stratmann wrote 8 lines
On Sunday 30 June 2013 00:59:59 you wrote: > I 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

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...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Rainer Stratmann
to Mark Morgan Lloyd: In which case why is he putting ls in front of it? -r is a valid option to both uname and…
you wrote
In 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.

Show quoted text 10 lines
> 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.

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?

waldo kitty
to Mark Morgan Lloyd: In which case why is he putting ls in front of it? -r is a valid option to both uname and…
Mark Morgan Lloyd wrote 14 lines
Rainer Stratmann wrote: > On Sunday 30 June 2013 12:32:30 you wrote: >> If I had to go on, what's that -r supposed to be doing? > > 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' 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.

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? ;)

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
waldo kitty
to Rainer Stratmann: I guess he simply does not know by now and tries out something. I bet he wants to know…
Rainer Stratmann wrote 5 lines
On Sunday 30 June 2013 14:14:56 you wrote: > > In which case why is he putting ls in front of it? I guess he simply does not know by now and tries out something.

don't guess... others cannot learn from assumptions and guesses...

Show quoted text 6 lines
> -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.

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...

Show quoted text 11 lines
>> 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. It is not necessary to delete the file.

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...

Show quoted text 5 lines
> 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.

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...

Show quoted text 5 lines
> 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?

make it what?? you cannot redirect without the shell or by taking additional steps to emulate what the shell does to allow redirecting...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Johan Tu Toit
to waldo kitty: 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

waldo kitty wrote 41 lines
On 6/30/2013 08:14, Mark Morgan Lloyd wrote: > Rainer Stratmann wrote: >> On Sunday 30 June 2013 12:32:30 you wrote: >>> If I had to go on, what's that -r supposed to be doing? >> >> 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' > > 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. 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? ;) -- NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
waldo kitty
to Johan Tu Toit: sorry it is my fault it was suppose to be: $uname -r
Johan Tu Toit wrote
sorry 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

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.