1

I'm attempting to create a custom shell script that will extract a zip file in my media/tmp directory.

I've created a labeler.php file in the shell directory. It extends Mage_Shell_Abstract and run() looks like this:

/**
 * Run script
 *
 */
public function run()
{
    $_SESSION = array();
    if ($this->getArg('unzip')) {
        $zipFile = strval($this->getArg('unzip'));
        echo "Unzipping {$zipFile}";
    }elseif ($this->getArg('pendingList')) {
        echo "this\n";
    } else {
        echo $this->usageHelp();
    }
}

The command that I am executing from is php -f shell/labeler.php -- unzip file.zip. The output that I get is: Unzipping 1

6
  • What if you don't use strval on the argument ? Commented Feb 29, 2016 at 15:59
  • I added strval because the output was 1. I probably should have removed it from my example above. Commented Feb 29, 2016 at 16:00
  • What if you run php -f shell/labeler.php --unzip file.zip (without the space between -- and unzip) ? Commented Feb 29, 2016 at 16:01
  • Or more probably like this : php -f shell/labeler.php -- --unzip file.zip Commented Feb 29, 2016 at 16:02
  • Your last suggestion works perfectly. Any idea why? Prior to posting php -f shell/labeler.php -- unzip file.zip and php -f shell/labeler.php unzip file.zip produced the same output of 1. And php -f shell/labeler.php --unzip file.zip outputs usage suggestions. Which is bothersome because I don't see how my code is different from the model I followed in indexer.php Commented Feb 29, 2016 at 16:04

1 Answer 1

9

From what I know I think the right syntax is the following:

php -f shell/labeler.php -- --unzip file.zip

The syntax for shell script in Magento is :

php -f script.php -- [options]

Where options could be just a flag like this:

php -f script.php -- test

In that case $this->getArg('test') will return 1 (true).

Or it could be a value like this:

php -f script.php -- --test mytest

In that case $this->getArg('test') will return 'mytest'

1
  • I'll accept your answer when enough time has passed. This has earned a permanent home in my @link phpDoc. Commented Feb 29, 2016 at 16:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.