使用laravel命令类运行bash脚本,找不到错误
我想使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将命令和参数作为命令数组中的两个单独的项目提供,
您也可以使用存储幕墙实现相同的结果,而无需任何shell脚本
You may try to provide the command and argument as two separate items in the commands array
You can also achieve the same results with Storage facade without any shell script