0

I come across a school work which require us to compile a c file and running various parameter to execute and get the result, which I have to manually key in the parameter line one by one.

I was wondering is there any way to execute the command by writing it into a file and run it at once? Something like a makefile but for the command line and not compilation.

For example:

I have to run a program that require me to manually key in

./program 10

./program 100

./program 1000

./program 10000

./program ....

./program ....

./program .....

Is there a way I can write all of these into a file and run it at once without manually key in everything?

Thank you!

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Azr4el
  • 31
  • 4
  • Give it a try with command line scripting and ask here for help on the script, please. – Baris Demiray Sep 29 '16 at 08:22
  • 1
    You should remove the [tag:C] and [tag:C++] tags and add a [tag:sh] or [tag:shell] tag. Your question has nothing to do with C or C++ – tofro Sep 29 '16 at 08:50

5 Answers5

0

Yes there is, and you can use the Makefile for this: add a test target and write all you test cases as commands for this target. Also make this target depend on the executable, so all you will need to recompile and run the tests is make test

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

You can use the && operator;

./program 10 && ./program 100 && ...

On a technical note the use of && (essentially a logical AND) generally means that the subsequent commands are only executed if the previous one succeeded (i.e. returns 0).

Community
  • 1
  • 1
Niall
  • 30,036
  • 10
  • 99
  • 142
0

Make a script like this:

#!/bin/bash
for i in 10 100 1000; do
  ./program $i
done

Save it as go.

Make it executable:

chmod +x go

Run it:

./go
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

Windows batch example (source)

for %%s in ("10" "100" "1000" "10000") do call program.exe %%s
  1. save in a text file e.g. loop.bat (in the same location as program.exe)
  2. open cmd.exe (Windows + R -> cmd.exe -> enter)
  3. navigate to location of loop.bat
  4. enter "loop.bat"

Windows powershell example (source)

$array = @("10", "100", "1000")
foreach ($element in $array) {
    & .\program.exe $element
}
  1. save in a text file e.g. loop.ps1 (in the same location as program.exe)
  2. open powershell (Windows Key + R -> powershell -> enter)
  3. navigate to location of loop.ps1
  4. .\loop.ps1
Community
  • 1
  • 1
robor
  • 2,969
  • 2
  • 31
  • 48
0

sure,

Place all the command lines into a file, one command line per line in the file,

in linux, lets call that file: values

./program  .value. 

one line per each value.

Then execute the file.

source values

or

. values

in windows call that file values.bat

Then execute the file with

values.bat
user3629249
  • 16,402
  • 1
  • 16
  • 17