Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29257 Discussions

Simple Way to Get Shell Command Output

Blane_J_
New Contributor I
3,608 Views

Hi, for the "SYSTEM" intrinsic only return the exit code of a shell command but not the output from it, so is there any approach to get the output within fortran code ? Such as, the current date string returned by shell command "%DATE%" ? I have tried to echo the date string to txt file and then read it, but it's really bothered, are there any simpler ways ?

 

0 Kudos
13 Replies
andrew_4619
Honored Contributor III
3,608 Views

Well if I just wanted the date there is a standard Fortran intrinsic you can call: DATE_AND_TIME

But in answer to you question it can be done but there isn't a "simple" way.  Getting data via text files is one way. Creating or attaching to an existing console window is also possible and reading then reading from its text buffer is another way. The latter requires understanding programming with the windows API.

 

0 Kudos
Blane_J_
New Contributor I
3,608 Views

Can you show me an example of the way of attarching and then reading from the text buffer of an existing console ? Thanks a lot.

0 Kudos
jimdempseyatthecove
Honored Contributor III
3,608 Views

Reading the text from a console window is non-trivial. At least until you do it once (your way), then reuse the subroutine you wrote. This is additionally problematic when you do not want the console window to hang around. Redirecting "terminal" output to file is much better. And once you have written his subroutine, you can reuse it later.

For standard types of functions you have the Fortran standard functions as well as the Windows functions (on Windows machines), or equivalent Linux functions on those machines. You can also the C interoperability capability to call C/C++ functions (your own or from various libraries).

Jim Dempsey

0 Kudos
jimdempseyatthecove
Honored Contributor III
3,608 Views

Blane J. wrote:

Can you show me an example of the way of attarching and then reading from the text buffer of an existing console ? Thanks a lot.

Use Google, enter "console functions windows", no quotes.

One of the first links will be: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx

The function you might want is ReadConsoleOutputCharacter.

However, this also requires you to learn how to get the Windows handle to the console (of interest) screen buffer.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
3,608 Views

The simpler way would be to execute a command with a redirect to a file and then read the file. We've supported the standard EXECUTE_COMMAND_LINE for a while now.

What do you really want to do?

0 Kudos
jimdempseyatthecove
Honored Contributor III
3,608 Views

Note, #5 will get you access to the information sitting in the console window and not what may have scrolled off the bottom of the window. (this is what you asked for).

Jim Dempsey

0 Kudos
Blane_J_
New Contributor I
3,608 Views

Thank you for your suggestion, Jim and Steve.

And by the way, DATE_AND_TIME intrinsic only returns time info, I don't aware of any intrinsic can do time calculation. So what I really want to do is to call Linux shell command "date" to do some time calculation within  fortran code, and then get the output time string and use it after. It seems that the most simple way is to redirect output to file and then read it which I am already in use.

0 Kudos
andrew_4619
Honored Contributor III
3,608 Views

https://software.intel.com/en-us/node/580486#880ABC94-8A0B-4045-B07D-D8E9A6EC007E

No it can return date and time like the name says

 

0 Kudos
Steven_L_Intel1
Employee
3,608 Views

Blane wants to do date calculations, which indeed there is no Fortran intrinsic for. Given that this is the Windows forum, I would have recommended Windows API routines for doing conversion to and from various date formats, but Blane mentions Linux so I don't know if there's an accessible routine to do that sort of thing. There's probably something in a C library...

That said, Fortran programmers have been doing date calculations for decades - it's not terribly difficult and I am sure there are examples all over the net.

0 Kudos
andrew_4619
Honored Contributor III
3,608 Views

The is a comprehensive and free set of Fortran date time tools at https://github.com/milancurcic/datetime-fortran ....

 

0 Kudos
Blane_J_
New Contributor I
3,608 Views

Thank you both. I see, so I think the better way is to do the calculation (what ever shell command could do) using user defined subroutine when there's no accessble intrinsic doing the same thing just like "datetime" above.  By the way, would fortran plan to have the sort of intrinsic funtion which can retrive output from shell command in the future ?

0 Kudos
Steven_L_Intel1
Employee
3,608 Views

Not in standard Fortran. It did add EXECUTE_COMMAND_LINE in Fortran 2008, which we support, but capturing the output is not included. (I don't know of similar routines in C that would capture the output.) Redirecting the command output to a file or named pipe and reading it is quite common.

0 Kudos
rase
New Contributor I
3,608 Views

A good source of calendar functions in Fortran is John Burkardt's calpak library. Look at URL:

http://people.sc.fsu.edu/~jburkardt/f_src/calpak/calpak.html


 

0 Kudos
Reply