views:

36

answers:

4

I need to run a scrip via command line but the files necessary for me to run it require me to be in the directory of the script. But I cannot do it for every command line i need to run. Their are over five thousand. Could someone tell me how to either format the list easily or add something to the format that would make it run. I have something like this....

php /path/to/the/script/01240/script.php
php /path/to/the/script/03770/script.php
php /path/to/the/script/02110/script.php
php /path/to/the/script/02380/script.php
php /path/to/the/script/03220/script.php
php /path/to/the/script/02340/script.php
php /path/to/the/script/03720/script.php
php /path/to/the/script/03460/script.php
php /path/to/the/script/0180/script.php
php /path/to/the/script/02000/script.php
php /path/to/the/script/01830/script.php
php /path/to/the/script/0980/script.php
php /path/to/the/script/0400/script.php
php /path/to/the/script/02750/script.php
php /path/to/the/script/0760/script.php
php /path/to/the/script/02690/script.php

..... and it goes on for 5000 more lines.

A: 

You don't need to edit your list by hand. Write another script which reads in your main script, and adds cd commands before/after each one. Then run the result of that.

skaffman
Im not that good at code, could you show me smippets or write it for me. I know its simple.
wait. Thats too easy. for each file as line include, right?
@user355367: Something like that, yes. It shouldn't be difficult.
skaffman
A: 

You can create a new file called php1.bat with the following content:

pushd %~p1
php %1
popd

This will change to the directory of the parameter, execute php and jump back.

After this, replace every occurence of php / in your script by call php1 / with your favorite search/replace all editor, so it doesn't execute php, but php1.bat.

schnaader
A: 
find -type f -iname script.php -execdir php {} \;

Or, if the scripts are differently named:

find -type f -iname '*.php' -execdir php {} \;

Edit: If it's a list of specific scripts rather then all: An alternative would be to define an auto_prepend_file in your php.ini (or a custom php.ini for this script), which would enable you to put there:

<?php
    chdir(dirname($argv[1]));
?>
Wrikken
A: 

I can suggest two possible solutions:

One is to add an option to your script so you can specify the working directory. Each process has its own working directory, so this would mean your PHP script changes its directory but the shell you are running the script from does not. Once the PHP script finishes, you're back in your shell in the same directory you started from.

<?php

$options = getopt("d:");
if (isset($options["d"])) {
  chdir($options["d"]) or die("Cannot chdir to " . $options["d"]);
} 

...do the rest of the script...

Then invoke your script as:

php script.php -d /path/to/the/script/01240/

The other solution is to change directory as you invoke your PHP script. Remember I said each process has its own working directory. But you can make the shell open a sub-process simply using parentheses. Then use cd in that subshell to change directory and invoke the PHP script. Once you finish the subshell in parens, you're back to where you started.

shell$ ( cd /path/to/the/script/01240/ ; php ~/bin/script.php)
shell$ ( cd /path/to/the/script/03770/ ; php ~/bin/script.php)
shell$ ( cd /path/to/the/script/02110/ ; php ~/bin/script.php)

But I'm guessing that your script is simply opening files with a relative pathname. If you literally do some code in the script relative to dirname(__FILE__) which is the location of the invoked script, then these solutions won't work.

Bill Karwin