Home > Archive > Unix Programming > January 2008 > Batch file in Unix
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Batch file in Unix
|
|
| L Benoit 2008-01-18, 7:14 pm |
| G'day,
I'm a newbie to Unix (and programming at all).
The situation is: I have a C-program "MyProgram" which
depends on an integer parameter z.
MyProgram is running on a Unix machine.
Currently I'm typing
MyProgram -z > MyResults.txt
and the result is written in the file "MyResults.txt".
Since I have a large number of various parameter z to be put
into MyProgram I cannot do it manually.
Also, I cannot change the code of MyProgram as I do not
have the source code available.
Therefore I'm thinking of writing a little program
which produces command lines like
MyProgram -z1 > MyResults.txt
MyProgram -z2 > MyResults.txt
....
MyProgram -zn > MyResults.txt
These command lines are then to be stored into one file,
say "MyCommands.txt".
Question:
1. How can I get Unix to execute each command line
of MyCommands.txt?
2. I suspect that each new result will overwrite
the previous result. So how can I make sure that
the new result is *only added* to the previous
ones in the same file "MyResults.txt"?
Thanks a lot,
L
| |
| Ben Bacarisse 2008-01-18, 7:14 pm |
| L Benoit <legendre_benoit@yahoo.ca> writes:
> The situation is: I have a C-program "MyProgram" which
> depends on an integer parameter z.
> MyProgram is running on a Unix machine.
>
> Currently I'm typing
>
> MyProgram -z > MyResults.txt
>
> and the result is written in the file "MyResults.txt".
>
> Since I have a large number of various parameter z to be put
> into MyProgram I cannot do it manually.
> Also, I cannot change the code of MyProgram as I do not
> have the source code available.
>
> Therefore I'm thinking of writing a little program
> which produces command lines like
>
> MyProgram -z1 > MyResults.txt
> MyProgram -z2 > MyResults.txt
> ...
> MyProgram -zn > MyResults.txt
No need to write a program to do that unless the pattern of the zn is
very complex. Real examples would help (i.e. dod you just want 0, 1,
2, 3 and so on or 8723, 546381, -265347, etc?).
> These command lines are then to be stored into one file,
> say "MyCommands.txt".
>
> Question:
> 1. How can I get Unix to execute each command line
> of MyCommands.txt?
Put:
#!/bin/sh
at the top and make it executable (chmod +x MyCommands.txt). Of
course you can just ask a shell to read it:
sh MyCommands.txt
(or bash, or csh, or zsh, ...)
> 2. I suspect that each new result will overwrite
> the previous result. So how can I make sure that
> the new result is *only added* to the previous
> ones in the same file "MyResults.txt"?
change > to >> and you get appending.
--
Ben.
| |
| Chris F.A. Johnson 2008-01-18, 7:14 pm |
| On 2008-01-18, L Benoit wrote:
>
> I'm a newbie to Unix (and programming at all).
>
> The situation is: I have a C-program "MyProgram" which
> depends on an integer parameter z.
> MyProgram is running on a Unix machine.
>
> Currently I'm typing
>
> MyProgram -z > MyResults.txt
>
> and the result is written in the file "MyResults.txt".
>
> Since I have a large number of various parameter z to be put
> into MyProgram I cannot do it manually.
> Also, I cannot change the code of MyProgram as I do not
> have the source code available.
>
> Therefore I'm thinking of writing a little program
> which produces command lines like
>
> MyProgram -z1 > MyResults.txt
> MyProgram -z2 > MyResults.txt
> ...
> MyProgram -zn > MyResults.txt
>
> These command lines are then to be stored into one file,
> say "MyCommands.txt".
>
> Question:
> 1. How can I get Unix to execute each command line
> of MyCommands.txt?
Give the file executable status:
chmod +x MyCommands.txt
Then call it as you would any other program. Either put it in a
directory that's in your PATH (e.g., $HOME/bin) or give the path to
the file (e.g., ./MyCommands.txt).
> 2. I suspect that each new result will overwrite
> the previous result. So how can I make sure that
> the new result is *only added* to the previous
> ones in the same file "MyResults.txt"?
Enclose the commands in braces to make it a compound command and
redirect its output:
{
MyProgram -z1
MyProgram -z2
...
MyProgram -zn
} > MyResults.txt
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
|
|
|
|
|