使用laravel命令类运行bash脚本,找不到错误

发布于 2025-02-08 08:18:11 字数 2808 浏览 2 评论 0原文

我想使用laravel命令类运行简单的bash脚本,但是我没有发现错误。 如果我转到/var/www/mysite/storage/app/scripts,然后从命令行中运行脚本,那么一切都可以。

$ sh remove-spaces.sh

我的Laravel代码有什么问题?

lowercase.sh

for file in /var/www/mysite/storage/app/img/*; 
do mv "$file" "`echo $file | tr '[A-Z]' '[a-z]'`"; 
done

删除空间.sh

for file in /var/www/mysite/storage/app/img/*; 
do mv "$file" `echo $file | tr ' ' '-'`; 
done

renamePicturesCommand

namespace App\Console\Commands\Pictures;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class RenamePicturesCommand extends Command
{
    protected $removeSpaces;
    protected $lowercase;
    protected $signature = 'pictures:rename';

    public function __construct()
    {
        parent::__construct();
        $this->removeSpaces = new Process(['sh /var/www/mysite/storage/app/scripts/remove-spaces.sh']);
        $this->lowercase = new Process(['sh /var/www/mysite/storage/app/scripts/lowercase.sh']);
    }


    public function handle()
    {
        $this->removeSpaces->run();
        if (!$this->removeSpaces->isSuccessful()) {
            throw new ProcessFailedException($this->removeSpaces);
        }
        echo $this->removeSpaces->getOutput();

        $this->lowercase->run();
        if (!$this->lowercase->isSuccessful()) {
            throw new ProcessFailedException($this->lowercase);
        }
        echo $this->lowercase->getOutput();
    }
}

错误输出

http@0bb690b74597:/var/www/mysite$ php artisan pictures:rename

   Symfony\Component\Process\Exception\ProcessFailedException

  The command "'sh /var/www/mysite/storage/app/scripts/remove-spaces.sh'" failed.

Exit Code: 127(Command not found)

Working directory: /var/www/mysite

Output:
================


Error Output:
================
sh: 1: exec: sh /var/www/mysite/storage/app/scripts/remove-spaces.sh: not found

  at app/Console/Commands/Pictures/RenamePicturesCommand.php:59
     55▕         // execute command
     56▕         $this->removeSpaces->run();
     57▕         // executes after the command finishes
     58▕         if (!$this->removeSpaces->isSuccessful()) {
  ➜  59▕             throw new ProcessFailedException($this->removeSpaces);
     60▕         }
     61▕         echo $this->removeSpaces->getOutput();
     62▕
     63▕

      +13 vendor frames
  14  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

I want to run simple bash scripts using laravel command class, but I have not found error.
If I go to /var/www/mysite/storage/app/scripts and run script from there in command line, then everything is OK.

$ sh remove-spaces.sh

What is wrong in my Laravel code?

lowercase.sh

for file in /var/www/mysite/storage/app/img/*; 
do mv "$file" "`echo $file | tr '[A-Z]' '[a-z]'`"; 
done

remove-spaces.sh

for file in /var/www/mysite/storage/app/img/*; 
do mv "$file" `echo $file | tr ' ' '-'`; 
done

RenamePicturesCommand

namespace App\Console\Commands\Pictures;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class RenamePicturesCommand extends Command
{
    protected $removeSpaces;
    protected $lowercase;
    protected $signature = 'pictures:rename';

    public function __construct()
    {
        parent::__construct();
        $this->removeSpaces = new Process(['sh /var/www/mysite/storage/app/scripts/remove-spaces.sh']);
        $this->lowercase = new Process(['sh /var/www/mysite/storage/app/scripts/lowercase.sh']);
    }


    public function handle()
    {
        $this->removeSpaces->run();
        if (!$this->removeSpaces->isSuccessful()) {
            throw new ProcessFailedException($this->removeSpaces);
        }
        echo $this->removeSpaces->getOutput();

        $this->lowercase->run();
        if (!$this->lowercase->isSuccessful()) {
            throw new ProcessFailedException($this->lowercase);
        }
        echo $this->lowercase->getOutput();
    }
}

error output

http@0bb690b74597:/var/www/mysite$ php artisan pictures:rename

   Symfony\Component\Process\Exception\ProcessFailedException

  The command "'sh /var/www/mysite/storage/app/scripts/remove-spaces.sh'" failed.

Exit Code: 127(Command not found)

Working directory: /var/www/mysite

Output:
================


Error Output:
================
sh: 1: exec: sh /var/www/mysite/storage/app/scripts/remove-spaces.sh: not found

  at app/Console/Commands/Pictures/RenamePicturesCommand.php:59
     55▕         // execute command
     56▕         $this->removeSpaces->run();
     57▕         // executes after the command finishes
     58▕         if (!$this->removeSpaces->isSuccessful()) {
  ➜  59▕             throw new ProcessFailedException($this->removeSpaces);
     60▕         }
     61▕         echo $this->removeSpaces->getOutput();
     62▕
     63▕

      +13 vendor frames
  14  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2025-02-15 08:18:11

您可以尝试将命令和参数作为命令数组中的两个单独的项目提供,

namespace App\Console\Commands\Pictures;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class RenamePicturesCommand extends Command
{
    protected $removeSpaces;
    protected $lowercase;
    protected $signature = 'pictures:rename';

    public function __construct()
    {
        parent::__construct();
        $this->removeSpaces = new Process(['sh', '/var/www/mysite/storage/app/scripts/remove-spaces.sh']);
        $this->lowercase = new Process(['sh', '/var/www/mysite/storage/app/scripts/lowercase.sh']);

        //Or you can use Process::fromShellCommandline
        //$this->removeSpaces =Process::fromShellCommandline('sh /Volumes/samsung-250/Sites/stackoverflow/storage/app/scripts/lowercase.sh');
        //$this->lowercase =Process::fromShellCommandline('sh /Volumes/samsung-250/Sites/stackoverflow/storage/app/scripts/remove-spaces.sh');
    }


    public function handle()
    {
        $this->removeSpaces->run();
        if (!$this->removeSpaces->isSuccessful()) {
            throw new ProcessFailedException($this->removeSpaces);
        }
        echo $this->removeSpaces->getOutput();

        $this->lowercase->run();
        if (!$this->lowercase->isSuccessful()) {
            throw new ProcessFailedException($this->lowercase);
        }
        echo $this->lowercase->getOutput();
    }
}

您也可以使用存储幕墙实现相同的结果,而无需任何shell脚本

namespace App\Console\Commands\Pictures;

use Illuminate\Console\Command;
use Illuminate\Support\LazyCollection;

class RenamePicturesCommand extends Command
{
    protected $signature = 'pictures:rename';


    public function handle()
    {
        $this->info('Renaming pictures...');
        $this->newLine(2);

        $files = LazyCollection::make(Storage::disk('local')->files('img'))
            //Remove dotfiles
            ->reject(fn($filePath) => Str::startsWith($filePath, '.'));

        $progress = $this->output->createProgressBar($files->count());
        $progress->start();

        $modified = [];

        $files->each(function ($file) use ($progress, &$modified) {

            $new = strtolower(preg_replace('/\s+/', '-', $file));

            $temp = ['old' => $file, 'new' => $new, 'status' => 'success'];

            if(! Storage::disk('local')->move($file, $new)) {
                $temp['status'] = 'failed';
                $temp['new'] = $file;
            }

            $modified[] = $temp;

            $progress->advance();
        });

        
        $this->newLine(2);
        $this->info('Finished Renaming pictures');
        $this->newLine(2);
        $this->table(['Old', 'New', 'Status'], $modified);
    }
}

You may try to provide the command and argument as two separate items in the commands array

namespace App\Console\Commands\Pictures;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class RenamePicturesCommand extends Command
{
    protected $removeSpaces;
    protected $lowercase;
    protected $signature = 'pictures:rename';

    public function __construct()
    {
        parent::__construct();
        $this->removeSpaces = new Process(['sh', '/var/www/mysite/storage/app/scripts/remove-spaces.sh']);
        $this->lowercase = new Process(['sh', '/var/www/mysite/storage/app/scripts/lowercase.sh']);

        //Or you can use Process::fromShellCommandline
        //$this->removeSpaces =Process::fromShellCommandline('sh /Volumes/samsung-250/Sites/stackoverflow/storage/app/scripts/lowercase.sh');
        //$this->lowercase =Process::fromShellCommandline('sh /Volumes/samsung-250/Sites/stackoverflow/storage/app/scripts/remove-spaces.sh');
    }


    public function handle()
    {
        $this->removeSpaces->run();
        if (!$this->removeSpaces->isSuccessful()) {
            throw new ProcessFailedException($this->removeSpaces);
        }
        echo $this->removeSpaces->getOutput();

        $this->lowercase->run();
        if (!$this->lowercase->isSuccessful()) {
            throw new ProcessFailedException($this->lowercase);
        }
        echo $this->lowercase->getOutput();
    }
}

You can also achieve the same results with Storage facade without any shell script

namespace App\Console\Commands\Pictures;

use Illuminate\Console\Command;
use Illuminate\Support\LazyCollection;

class RenamePicturesCommand extends Command
{
    protected $signature = 'pictures:rename';


    public function handle()
    {
        $this->info('Renaming pictures...');
        $this->newLine(2);

        $files = LazyCollection::make(Storage::disk('local')->files('img'))
            //Remove dotfiles
            ->reject(fn($filePath) => Str::startsWith($filePath, '.'));

        $progress = $this->output->createProgressBar($files->count());
        $progress->start();

        $modified = [];

        $files->each(function ($file) use ($progress, &$modified) {

            $new = strtolower(preg_replace('/\s+/', '-', $file));

            $temp = ['old' => $file, 'new' => $new, 'status' => 'success'];

            if(! Storage::disk('local')->move($file, $new)) {
                $temp['status'] = 'failed';
                $temp['new'] = $file;
            }

            $modified[] = $temp;

            $progress->advance();
        });

        
        $this->newLine(2);
        $this->info('Finished Renaming pictures');
        $this->newLine(2);
        $this->table(['Old', 'New', 'Status'], $modified);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文