2

I have a small php script that looks like this :

<?php
$argument1 = $argv[1];
echo "passed Value:".$argument1;
 //do something with passed value 
 //echo processed value
?>

I want to run it multiple times using cmd.

This is how I run it currently :

c:\php\php.exe MyScript.php 1

I want to run the above cmd line multiple times for different values ranging from 1 to 100.

Could anyone tell me how I can run it multiple times for different values passed to it?


Note:

I want to be able read the output of each php call and copy it

3
  • You could make a script implement a loop iterating over that number interval and calling your php script in each iteration. A possible language to implement such script would be php, actually, or any shell language installed... Commented Feb 6, 2016 at 16:51
  • "read the output of each php call and copy it" ? Read it on screen? Or into a variable? Copy to file? Commented Feb 6, 2016 at 16:57
  • Thanks for reply. Read it from cmd window and copy and paste it manually to text file or save the php output to a text file automatically Commented Feb 6, 2016 at 16:59

2 Answers 2

4
(FOR /L %x in (1,1,100) DO c:\php\php.exe MyScript.php %x)>output.txt 2>&1

>output.txt redirects STDOUT to a file. 2>&1 redirects STDERR to the same place.

(Note: this is a command line syntax. To use it in a batchfile, use %%x instead of %x)

Sign up to request clarification or add additional context in comments.

11 Comments

I pasted this on cmd :(FOR /L %x (1,1,4) DO c:\php\php.exe MyScript.php %x)>output.txt 2>&1 and I get 1 was unexpected this time ! I wanted to test if it writes output to text file! does it create the text file or should i create it ?
> creates the file (and deletes it, if it already exists). I don't have php installed, so I can't test. What do you get, if you just use c:\php\php.exe MyScript.php 1?
If i run that line i get the value of php echo which is : passed Value:1 . i want those php echo results in text file instead of seeing them only on cmd window. so if i run it for small range 1-4 i should get passed Value:1,passed Value:2,passed Value:3,passed Value:4 on a text file as well
Shouldn't this be for /l %x IN (1,1,100)?
@user1788736 I'm guessing that's the reason for the 1 was unexpected error, it expected in first.
|
2

For doing it in the shell itself:

FOR /L %parameter IN (start,step,end) DO command 

For example:

FOR /L %x IN (1,1,100) DO c:\php\php.exe MyScript.php %x

For doing it in a .bat file:

FOR /L %%parameter IN (start,step,end) DO command 

For example:

FOR /L %%x IN (1,1,100) DO c:\php\php.exe MyScript.php %%x

However, I don't know if this will output the outcome.

Source: SS64

Edit: changed %%x to %x and replaced 1 by %x -- thanks both of you :D

10 Comments

I suggest: FOR /L %%x IN (1,1,100) DO c:\php\php.exe Myscript %%x
%%x syntax is for use in batchfiles. If you use it on command line, use %x only.
@user1788736 Check the answer of Stephan
You also should edit your answer to have your second example use IN in the for command. This wouldn't work.
@anned20 geen probleem, was verder een goed antwoord. Ik zou er echter wel nog een note inzetten over %% voor batch, % voor cmd
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.