Save command line output to variable in Fortran












2














Is there a way to store the output of a command line utility to a variable in Fortran?



I have a BASH based utility which gives me a number which needs to be used in a Fortran program. I want to call the utility through the program itself, and avoid writing the output to a file if possible.



Something like this maybe?



integer a
write(a,*) call execute_command_line('echo 5')


Or like this maybe?



read(call execute_command_line('echo 5'),*) a


I don't think either of these is right though. I would like to know if there is actually a method to do this. I read the docs for execute_command_line but I don't think there is an output argument for the subroutine which does this.










share|improve this question


















  • 1




    You'll have to use a temporary "file". I can't immediately find a question which I thought covered this, but some discussion is in this one.
    – francescalus
    Nov 23 '18 at 13:53










  • I have been trying and failing to make the fortranposix library mentioned in the question (an fPIC error for an object file which is not there in the Makefile). While I am figuring it out, is there any other library which adds posix functionalities like popen to Fortran? Or any other way which does not depend on external, non default libraries? @francescalus
    – Yuki.kuroshita
    Nov 23 '18 at 15:20










  • Some compilers bundle POSIX-like things in their runtime, so you may have luck there. But, to be honest, I'd just write standard output of the command to a real file and read it from my Fortran program.
    – francescalus
    Nov 23 '18 at 15:22










  • Thing is, this will go into a program for differential evolution with millions of generations, with 40 models, each with 4 model parameters, looped over 120 data points, so total iterations would be 19200000000 at max. I don't know if writing to a file and then reading from it each time would be the best thing to do here. The whole point of writing a differential evolution algorithm is to make things faster. I would have preferred to not have to read and write a file in each iteration
    – Yuki.kuroshita
    Nov 23 '18 at 15:33






  • 2




    This article about named pipes is interesting networkworld.com/article/3251853/linux/… (which says that everything is performed on memory. But I wonder whether a huge number of system calls background is no problem or have some practical issue...)
    – roygvib
    Nov 23 '18 at 19:47


















2














Is there a way to store the output of a command line utility to a variable in Fortran?



I have a BASH based utility which gives me a number which needs to be used in a Fortran program. I want to call the utility through the program itself, and avoid writing the output to a file if possible.



Something like this maybe?



integer a
write(a,*) call execute_command_line('echo 5')


Or like this maybe?



read(call execute_command_line('echo 5'),*) a


I don't think either of these is right though. I would like to know if there is actually a method to do this. I read the docs for execute_command_line but I don't think there is an output argument for the subroutine which does this.










share|improve this question


















  • 1




    You'll have to use a temporary "file". I can't immediately find a question which I thought covered this, but some discussion is in this one.
    – francescalus
    Nov 23 '18 at 13:53










  • I have been trying and failing to make the fortranposix library mentioned in the question (an fPIC error for an object file which is not there in the Makefile). While I am figuring it out, is there any other library which adds posix functionalities like popen to Fortran? Or any other way which does not depend on external, non default libraries? @francescalus
    – Yuki.kuroshita
    Nov 23 '18 at 15:20










  • Some compilers bundle POSIX-like things in their runtime, so you may have luck there. But, to be honest, I'd just write standard output of the command to a real file and read it from my Fortran program.
    – francescalus
    Nov 23 '18 at 15:22










  • Thing is, this will go into a program for differential evolution with millions of generations, with 40 models, each with 4 model parameters, looped over 120 data points, so total iterations would be 19200000000 at max. I don't know if writing to a file and then reading from it each time would be the best thing to do here. The whole point of writing a differential evolution algorithm is to make things faster. I would have preferred to not have to read and write a file in each iteration
    – Yuki.kuroshita
    Nov 23 '18 at 15:33






  • 2




    This article about named pipes is interesting networkworld.com/article/3251853/linux/… (which says that everything is performed on memory. But I wonder whether a huge number of system calls background is no problem or have some practical issue...)
    – roygvib
    Nov 23 '18 at 19:47
















2












2








2


2





Is there a way to store the output of a command line utility to a variable in Fortran?



I have a BASH based utility which gives me a number which needs to be used in a Fortran program. I want to call the utility through the program itself, and avoid writing the output to a file if possible.



Something like this maybe?



integer a
write(a,*) call execute_command_line('echo 5')


Or like this maybe?



read(call execute_command_line('echo 5'),*) a


I don't think either of these is right though. I would like to know if there is actually a method to do this. I read the docs for execute_command_line but I don't think there is an output argument for the subroutine which does this.










share|improve this question













Is there a way to store the output of a command line utility to a variable in Fortran?



I have a BASH based utility which gives me a number which needs to be used in a Fortran program. I want to call the utility through the program itself, and avoid writing the output to a file if possible.



Something like this maybe?



integer a
write(a,*) call execute_command_line('echo 5')


Or like this maybe?



read(call execute_command_line('echo 5'),*) a


I don't think either of these is right though. I would like to know if there is actually a method to do this. I read the docs for execute_command_line but I don't think there is an output argument for the subroutine which does this.







fortran gfortran






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 13:32









Yuki.kuroshita

16910




16910








  • 1




    You'll have to use a temporary "file". I can't immediately find a question which I thought covered this, but some discussion is in this one.
    – francescalus
    Nov 23 '18 at 13:53










  • I have been trying and failing to make the fortranposix library mentioned in the question (an fPIC error for an object file which is not there in the Makefile). While I am figuring it out, is there any other library which adds posix functionalities like popen to Fortran? Or any other way which does not depend on external, non default libraries? @francescalus
    – Yuki.kuroshita
    Nov 23 '18 at 15:20










  • Some compilers bundle POSIX-like things in their runtime, so you may have luck there. But, to be honest, I'd just write standard output of the command to a real file and read it from my Fortran program.
    – francescalus
    Nov 23 '18 at 15:22










  • Thing is, this will go into a program for differential evolution with millions of generations, with 40 models, each with 4 model parameters, looped over 120 data points, so total iterations would be 19200000000 at max. I don't know if writing to a file and then reading from it each time would be the best thing to do here. The whole point of writing a differential evolution algorithm is to make things faster. I would have preferred to not have to read and write a file in each iteration
    – Yuki.kuroshita
    Nov 23 '18 at 15:33






  • 2




    This article about named pipes is interesting networkworld.com/article/3251853/linux/… (which says that everything is performed on memory. But I wonder whether a huge number of system calls background is no problem or have some practical issue...)
    – roygvib
    Nov 23 '18 at 19:47
















  • 1




    You'll have to use a temporary "file". I can't immediately find a question which I thought covered this, but some discussion is in this one.
    – francescalus
    Nov 23 '18 at 13:53










  • I have been trying and failing to make the fortranposix library mentioned in the question (an fPIC error for an object file which is not there in the Makefile). While I am figuring it out, is there any other library which adds posix functionalities like popen to Fortran? Or any other way which does not depend on external, non default libraries? @francescalus
    – Yuki.kuroshita
    Nov 23 '18 at 15:20










  • Some compilers bundle POSIX-like things in their runtime, so you may have luck there. But, to be honest, I'd just write standard output of the command to a real file and read it from my Fortran program.
    – francescalus
    Nov 23 '18 at 15:22










  • Thing is, this will go into a program for differential evolution with millions of generations, with 40 models, each with 4 model parameters, looped over 120 data points, so total iterations would be 19200000000 at max. I don't know if writing to a file and then reading from it each time would be the best thing to do here. The whole point of writing a differential evolution algorithm is to make things faster. I would have preferred to not have to read and write a file in each iteration
    – Yuki.kuroshita
    Nov 23 '18 at 15:33






  • 2




    This article about named pipes is interesting networkworld.com/article/3251853/linux/… (which says that everything is performed on memory. But I wonder whether a huge number of system calls background is no problem or have some practical issue...)
    – roygvib
    Nov 23 '18 at 19:47










1




1




You'll have to use a temporary "file". I can't immediately find a question which I thought covered this, but some discussion is in this one.
– francescalus
Nov 23 '18 at 13:53




You'll have to use a temporary "file". I can't immediately find a question which I thought covered this, but some discussion is in this one.
– francescalus
Nov 23 '18 at 13:53












I have been trying and failing to make the fortranposix library mentioned in the question (an fPIC error for an object file which is not there in the Makefile). While I am figuring it out, is there any other library which adds posix functionalities like popen to Fortran? Or any other way which does not depend on external, non default libraries? @francescalus
– Yuki.kuroshita
Nov 23 '18 at 15:20




I have been trying and failing to make the fortranposix library mentioned in the question (an fPIC error for an object file which is not there in the Makefile). While I am figuring it out, is there any other library which adds posix functionalities like popen to Fortran? Or any other way which does not depend on external, non default libraries? @francescalus
– Yuki.kuroshita
Nov 23 '18 at 15:20












Some compilers bundle POSIX-like things in their runtime, so you may have luck there. But, to be honest, I'd just write standard output of the command to a real file and read it from my Fortran program.
– francescalus
Nov 23 '18 at 15:22




Some compilers bundle POSIX-like things in their runtime, so you may have luck there. But, to be honest, I'd just write standard output of the command to a real file and read it from my Fortran program.
– francescalus
Nov 23 '18 at 15:22












Thing is, this will go into a program for differential evolution with millions of generations, with 40 models, each with 4 model parameters, looped over 120 data points, so total iterations would be 19200000000 at max. I don't know if writing to a file and then reading from it each time would be the best thing to do here. The whole point of writing a differential evolution algorithm is to make things faster. I would have preferred to not have to read and write a file in each iteration
– Yuki.kuroshita
Nov 23 '18 at 15:33




Thing is, this will go into a program for differential evolution with millions of generations, with 40 models, each with 4 model parameters, looped over 120 data points, so total iterations would be 19200000000 at max. I don't know if writing to a file and then reading from it each time would be the best thing to do here. The whole point of writing a differential evolution algorithm is to make things faster. I would have preferred to not have to read and write a file in each iteration
– Yuki.kuroshita
Nov 23 '18 at 15:33




2




2




This article about named pipes is interesting networkworld.com/article/3251853/linux/… (which says that everything is performed on memory. But I wonder whether a huge number of system calls background is no problem or have some practical issue...)
– roygvib
Nov 23 '18 at 19:47






This article about named pipes is interesting networkworld.com/article/3251853/linux/… (which says that everything is performed on memory. But I wonder whether a huge number of system calls background is no problem or have some practical issue...)
– roygvib
Nov 23 '18 at 19:47














1 Answer
1






active

oldest

votes


















4














Since you're using BASH, lets assume you're working on some kind of unix-like system. So you could use a FIFO. Something like



program readfifo
implicit none
integer :: u, i
logical :: ex
inquire(exist=ex, file='foo')
if (.not. ex) then
call execute_command_line ("mkfifo foo")
end if
call execute_command_line ("echo 5 > foo&")
open(newunit=u, file='foo', action='read')
read(u, *) i
write(*, *) 'Managed to read the value ', i
end program readfifo


Note that the semantics of FIFO's wrt blocking can be a bit tricky (that's why there is the '&' after the echo command, you might want to read up on it a bit and experiment (particularly make sure you haven't got a zillion bash processes hanging around when you do this multiple times).






share|improve this answer





















  • So, is using named pipes faster than using normal files? Like in the above code, if I don't use the command mkfifo foo, and just write it to a normal file, and then read from it, would it be comparably slower? Because as far as I understood, even making a named pipe will have to write a file to the disk
    – Yuki.kuroshita
    Nov 23 '18 at 17:20






  • 1




    @Yuki.kuroshita: Well, you have to create the FIFO entry in the directory yes, but then all I/O to/from that FIFO never hits the filesystem. So if you create the FIFO only once, and then keep it open in Fortran, I guess it would be pretty efficient. Well, roughly as efficient as a pipe, since that's what it is.
    – janneb
    Nov 23 '18 at 18:15










  • Thank you for the explanation
    – Yuki.kuroshita
    Nov 23 '18 at 19:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447665%2fsave-command-line-output-to-variable-in-fortran%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Since you're using BASH, lets assume you're working on some kind of unix-like system. So you could use a FIFO. Something like



program readfifo
implicit none
integer :: u, i
logical :: ex
inquire(exist=ex, file='foo')
if (.not. ex) then
call execute_command_line ("mkfifo foo")
end if
call execute_command_line ("echo 5 > foo&")
open(newunit=u, file='foo', action='read')
read(u, *) i
write(*, *) 'Managed to read the value ', i
end program readfifo


Note that the semantics of FIFO's wrt blocking can be a bit tricky (that's why there is the '&' after the echo command, you might want to read up on it a bit and experiment (particularly make sure you haven't got a zillion bash processes hanging around when you do this multiple times).






share|improve this answer





















  • So, is using named pipes faster than using normal files? Like in the above code, if I don't use the command mkfifo foo, and just write it to a normal file, and then read from it, would it be comparably slower? Because as far as I understood, even making a named pipe will have to write a file to the disk
    – Yuki.kuroshita
    Nov 23 '18 at 17:20






  • 1




    @Yuki.kuroshita: Well, you have to create the FIFO entry in the directory yes, but then all I/O to/from that FIFO never hits the filesystem. So if you create the FIFO only once, and then keep it open in Fortran, I guess it would be pretty efficient. Well, roughly as efficient as a pipe, since that's what it is.
    – janneb
    Nov 23 '18 at 18:15










  • Thank you for the explanation
    – Yuki.kuroshita
    Nov 23 '18 at 19:28
















4














Since you're using BASH, lets assume you're working on some kind of unix-like system. So you could use a FIFO. Something like



program readfifo
implicit none
integer :: u, i
logical :: ex
inquire(exist=ex, file='foo')
if (.not. ex) then
call execute_command_line ("mkfifo foo")
end if
call execute_command_line ("echo 5 > foo&")
open(newunit=u, file='foo', action='read')
read(u, *) i
write(*, *) 'Managed to read the value ', i
end program readfifo


Note that the semantics of FIFO's wrt blocking can be a bit tricky (that's why there is the '&' after the echo command, you might want to read up on it a bit and experiment (particularly make sure you haven't got a zillion bash processes hanging around when you do this multiple times).






share|improve this answer





















  • So, is using named pipes faster than using normal files? Like in the above code, if I don't use the command mkfifo foo, and just write it to a normal file, and then read from it, would it be comparably slower? Because as far as I understood, even making a named pipe will have to write a file to the disk
    – Yuki.kuroshita
    Nov 23 '18 at 17:20






  • 1




    @Yuki.kuroshita: Well, you have to create the FIFO entry in the directory yes, but then all I/O to/from that FIFO never hits the filesystem. So if you create the FIFO only once, and then keep it open in Fortran, I guess it would be pretty efficient. Well, roughly as efficient as a pipe, since that's what it is.
    – janneb
    Nov 23 '18 at 18:15










  • Thank you for the explanation
    – Yuki.kuroshita
    Nov 23 '18 at 19:28














4












4








4






Since you're using BASH, lets assume you're working on some kind of unix-like system. So you could use a FIFO. Something like



program readfifo
implicit none
integer :: u, i
logical :: ex
inquire(exist=ex, file='foo')
if (.not. ex) then
call execute_command_line ("mkfifo foo")
end if
call execute_command_line ("echo 5 > foo&")
open(newunit=u, file='foo', action='read')
read(u, *) i
write(*, *) 'Managed to read the value ', i
end program readfifo


Note that the semantics of FIFO's wrt blocking can be a bit tricky (that's why there is the '&' after the echo command, you might want to read up on it a bit and experiment (particularly make sure you haven't got a zillion bash processes hanging around when you do this multiple times).






share|improve this answer












Since you're using BASH, lets assume you're working on some kind of unix-like system. So you could use a FIFO. Something like



program readfifo
implicit none
integer :: u, i
logical :: ex
inquire(exist=ex, file='foo')
if (.not. ex) then
call execute_command_line ("mkfifo foo")
end if
call execute_command_line ("echo 5 > foo&")
open(newunit=u, file='foo', action='read')
read(u, *) i
write(*, *) 'Managed to read the value ', i
end program readfifo


Note that the semantics of FIFO's wrt blocking can be a bit tricky (that's why there is the '&' after the echo command, you might want to read up on it a bit and experiment (particularly make sure you haven't got a zillion bash processes hanging around when you do this multiple times).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 16:32









janneb

27.8k25777




27.8k25777












  • So, is using named pipes faster than using normal files? Like in the above code, if I don't use the command mkfifo foo, and just write it to a normal file, and then read from it, would it be comparably slower? Because as far as I understood, even making a named pipe will have to write a file to the disk
    – Yuki.kuroshita
    Nov 23 '18 at 17:20






  • 1




    @Yuki.kuroshita: Well, you have to create the FIFO entry in the directory yes, but then all I/O to/from that FIFO never hits the filesystem. So if you create the FIFO only once, and then keep it open in Fortran, I guess it would be pretty efficient. Well, roughly as efficient as a pipe, since that's what it is.
    – janneb
    Nov 23 '18 at 18:15










  • Thank you for the explanation
    – Yuki.kuroshita
    Nov 23 '18 at 19:28


















  • So, is using named pipes faster than using normal files? Like in the above code, if I don't use the command mkfifo foo, and just write it to a normal file, and then read from it, would it be comparably slower? Because as far as I understood, even making a named pipe will have to write a file to the disk
    – Yuki.kuroshita
    Nov 23 '18 at 17:20






  • 1




    @Yuki.kuroshita: Well, you have to create the FIFO entry in the directory yes, but then all I/O to/from that FIFO never hits the filesystem. So if you create the FIFO only once, and then keep it open in Fortran, I guess it would be pretty efficient. Well, roughly as efficient as a pipe, since that's what it is.
    – janneb
    Nov 23 '18 at 18:15










  • Thank you for the explanation
    – Yuki.kuroshita
    Nov 23 '18 at 19:28
















So, is using named pipes faster than using normal files? Like in the above code, if I don't use the command mkfifo foo, and just write it to a normal file, and then read from it, would it be comparably slower? Because as far as I understood, even making a named pipe will have to write a file to the disk
– Yuki.kuroshita
Nov 23 '18 at 17:20




So, is using named pipes faster than using normal files? Like in the above code, if I don't use the command mkfifo foo, and just write it to a normal file, and then read from it, would it be comparably slower? Because as far as I understood, even making a named pipe will have to write a file to the disk
– Yuki.kuroshita
Nov 23 '18 at 17:20




1




1




@Yuki.kuroshita: Well, you have to create the FIFO entry in the directory yes, but then all I/O to/from that FIFO never hits the filesystem. So if you create the FIFO only once, and then keep it open in Fortran, I guess it would be pretty efficient. Well, roughly as efficient as a pipe, since that's what it is.
– janneb
Nov 23 '18 at 18:15




@Yuki.kuroshita: Well, you have to create the FIFO entry in the directory yes, but then all I/O to/from that FIFO never hits the filesystem. So if you create the FIFO only once, and then keep it open in Fortran, I guess it would be pretty efficient. Well, roughly as efficient as a pipe, since that's what it is.
– janneb
Nov 23 '18 at 18:15












Thank you for the explanation
– Yuki.kuroshita
Nov 23 '18 at 19:28




Thank you for the explanation
– Yuki.kuroshita
Nov 23 '18 at 19:28


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447665%2fsave-command-line-output-to-variable-in-fortran%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista