LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-07-2012, 10:59 AM   #1
mike1982
LQ Newbie
 
Registered: Feb 2012
Posts: 1

Rep: Reputation: Disabled
Question a script to "make" several times in a loop


Dear All,

I am Running a fortran code with a .F file,that should be "make" each time and then run the executable file which have been produced. but i want to change some parameters in .F file each time and "make" this file and run exacutable file . dose any script exist to do this work?

the procedure is like below

for(10 times e.g)
{

1- change the parameter in .F
2- make
3-./myexecutable

}
thanks a lot.
 
Old 02-07-2012, 04:24 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,048

Rep: Reputation: 471Reputation: 471Reputation: 471Reputation: 471Reputation: 471
No experience with fortran but can't you just call 'make PARAM=<blah>' ? .. then you could wrap it in a loop like:

Code:
for param in x y z a b c
do
   make PARAM=param
   ./executable
done
 
Old 02-09-2012, 06:57 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 9,364
Blog Entries: 1

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
Moderator response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-10-2012, 12:54 PM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian
Posts: 401

Rep: Reputation: 187Reputation: 187
Hi.

There are few ways to do what you want.

gfortran has native preprocessing support (enabled with -cpp option). You can define a macro on command line using -D option:
Code:
$ cat test.f 
      program hello
      print *, X
      end program hello
$ gfortran -cpp -D X='"hello"' test.f && ./a.out 
 hello
$ gfortran -cpp -D X=3.1415 test.f && ./a.out 
   3.1415000
If your compiler does not support preprocessing, you can just use cpp (the C PreProcessor) manually:
Code:
$ cpp -P -D X=15 ./test.f > test2.f
$ gfortran test2.f && ./a.out 
          15
The `-P' flag inhibits generation of linemarkers in the output from the preprocessor which may confuse your compiler.

If you really need to use `make', try something like (in case of gfortran)
Code:
$ cat Makefile 
.PHONY: test
test: test.f
        gfortran $(CPPFLAGS) $< -o $@
$ make CPPFLAGS='-cpp -DX=5'
gfortran -cpp -DX=5 test.f -o test
$ ./test
           5
Use make only if your program consists of multiple files.

Now you can change parameter in a loop as follows:
Code:
$ for x in Hello cruel world; do gfortran -cpp -D X="'$x'" test.f ; ./a.out; done
 Hello
 cruel
 world
Here we comple and run the program three times.

Hope this helps.

PS: Mature programs usually use command line options and/or configuration files to alter its behaviour (so that recompilation is not necessary), but I understand that it is overkill for smaller projects.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
There is a infinite loop, when i run "make"...!!! luckybtn Linux - Newbie 9 09-13-2008 01:19 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
bash script: how do I refer to a counter i in a "for i in" loop frankie_DJ Programming 5 07-24-2007 08:45 AM
Data transfer online is slow "at times" or "stops at times" Balarabay1 Suse/Novell 14 04-30-2006 10:00 AM


All times are GMT -5. The time now is 02:47 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration