lalbert/bash-writer

使用PHP快速轻松地编写bash脚本

dev-develop 2016-09-30 12:50 UTC

This package is auto-updated.

Last update: 2024-09-24 21:21:21 UTC


README

使用PHP快速轻松地编写bash脚本。

PHP Bash Writer不是设计来运行bash脚本,而是通过添加样式(颜色、粗体等)到输出以方便编写脚本。

内部使用symfony/console来管理颜色和样式(参见OutputFormatter)。

安装

安装PHP Bash Writer的最佳方式是使用composer

composer require lalbert/bash-writer

用法

$sh = new BashWriter();

$sh->addCommand('#!/bin/bash', ['print' => false]);

$sh->newLine(); // Add new blank line only in file, not on output
$sh->newLine(true); // Add new line on output (write "echo")

$sh->addCommand('cd $HOME'); // print 'cd /home/user' and run command
$sh->addCommand('ls -la', ['print' => 'List files in <comment>`(pwd)`</comment> folder']); // print 'List files in `(pwd)` folder' whith result of pwd in yellow, and run command

$sh->addCommand('touch <bg=yellow;options=bold>file.txt</>'); // print 'touch file.txt' with 'file.txt' in yellow and bold, and run 'touch file.txt'
$sh->addCommand('echo "content file" > file.txt', ['print' => false]); // add content in file.txt, shows nothing

$sh->newLine(true);
$sh->output('<info>Done</info>'); // print "Done" in green text

// save script in script.sh file
file_put_contents('script.sh', $sh);

现在,文件script.sh包含以下脚本

#!/bin/bash

echo
echo -e "cd $HOME"
cd $HOME
echo -e "List files in ^[[33m`(pwd)`^[[39m folder"
ls -la
echo -e "touch ^[[43;1mfile.txt^[[49;22m"
touch file.txt
echo "content file" > file.txt
echo
echo -e "^[[32mDone^[[39m"