Open shell with php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PHP Shell Framework — creating shell applications

piotrooo/php-shell-framework

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This framework can be used to creating fully shell scripts.

  • Copy /path/to/php-shell-framework/config/psf.config.php to your code base location /path/to/application/psf.config.php and set application_dirs to corresponding console applications.
  • Inside application dir create catalog Console which contains shell apps.

To create new application, you must create a new PHP file in your_name_path/Console location, name of file this should be YourApplicationNameShell.php.

Newly created file should have number of requirements:

  • Name of class inside file should be corresponding name to file.
  • Class should extends from Shell and implements ApplicationInterface .

So created appliaction should looks like:

 namespace Console; use Psf\Interfaces\ApplicationInterface; use Psf\Shell; class HelloShell extends Shell implements ApplicationInterface < public function configure() < >public function main() < $this->out("Hello world"); > > ?>

After create application, we want run it from our console.

Constraint app determines which application should be called. After this call, our application print — by use out method — on STDOUT string Hello world.

$ php psf.php app:hello -N --user=Piotr 

Application can accept short and long types of parameters.

PHP Shell Framework implements this approach:

  • -n short parameter without argument, return true
  • -n hello short parameter with argument, space separed, return hello
  • -nhello short parameter with argument, no space separed, return hello
  • —user long parameter without argument, return true
  • —user=Piotr long parameter with argument, equal sign separed, return Piotr

To add support to parameters in application in method configure , we must set parameters of each parameter.

public function configure() < $this ->addParameter('N', 'namespace') ->addParameter('u', 'user'); >

This configuration allows us to many possibilities call our parameters.

$ php psf.php app:hello -N php/\shell/\output 
$ php psf.php app:hello --namespace php/\shell/\output 

In main method we get parameter like this:

$namespace = $this->getParameterValue('namespace'); 

this getter working on -N and —namespace parameter equally.

Special case. If we call application like that:

$ php psf.php app:hello -N php/\shell/\output --namespace php/\formatter 

The getParameterValue method will return php/formatter .

When you want display someting on STDOUT you can use out method:

You can aslo defined how many new lines should be after output message:

$this->out("Hello World Today. ", 5);

Sometimes you need different levels of verbosity. PHP Shell Framework provide three levels:

Default all outputs working in NORMAL level. If you want change level you must define this in out method.

$this->out('This message is in normal verbosity'); $this->out('This message is in quiet verbosity', 1, Writer::VERBOSITY_QUIET); $this->out('This message is in verbose verbosity', 1, Writer::VERBOSITY_VERBOSE);

If you want run application in NORMAL level:

This message is in normal verbosity This message is in quiet verbosity 

If you want run application in QUIET level:

$ php psf.php app:hello --quiet 
This message is in quiet verbosity 

If you want run application in VERBOSE level:

$ php psf.php app:hello --verbose 
This message is in normal verbosity This message is in quiet verbosity This message is in verbose verbosity 

Styling output is done by user-defined tags — like XML. PHP Shell Framework using style formetter will replace XML tag to correct defined ANSI code sequence.

To declare new XML tag and corresonding with him ANSI code you do:

$styleFormat = new StyleFormatter('gray', 'magenta', array('blink', 'underline')); $this->setFormatter('special', $styleFormat);

This would you to allow tag in you output messages and will set text color to gray , background color to magenta and have two effects — blink and underline .

You can use following color for text attributes:

Also you can use following effects:

Method read reads and interprest characters from STDIN , which usually recives what the user type at the keyboard.

$this->out("Type how old are you: ", 0); $age = $this->read(); if (!empty($age)) < $this->out('You have ' . $age . ' years old - nice!'); >

This piece of code wait unit user type something on keyboard.

In framework we can use helpers to generate some views.

Table is simple helper which generate tabular data.

$table = $this->getHelper('Table'); $table ->setHeaders(array('ID', 'Name', 'Surname')) ->setRows(array( array('1', 'John', 'Smith'), array('2', 'Brad', 'Pitt'), array('3', 'Denzel', 'Washington'), array('4', 'Angelina', 'Jolie') )); $table->render($this->getStdout());
+----+----------+------------+ | ID | Name | Surname | +----+----------+------------+ | 1 | John | Smith | | 2 | Brad | Pitt | | 3 | Denzel | Washington | | 4 | Angelina | Jolie | +----+----------+------------+ 

Additionaly we can add single row to our table by using addRow method:

$table->addRow(array('5', 'Peter', 'Nosurname'));
+----+----------+------------+ | ID | Name | Surname | +----+----------+------------+ | 1 | John | Smith | | 2 | Brad | Pitt | | 3 | Denzel | Washington | | 4 | Angelina | Jolie | | 5 | Peter | Nosurname | +----+----------+------------+ 

This helper provide progress functionality.

$progress = $this->getHelper('ProgressBar'); $progress->initialize($this->getStdout(), 9); for ($i = 0; $i < 9; $i++) < $progress->increment(); sleep(1); >

Loader helper get possibility of display loader pseudo animation.

$loader = $this->getHelper('Loader'); $loader->initialize($this->getStdout()); for ($i = 0; $i < 10; $i++) < $loader->start(); sleep(1); >

Also we can customizing loader through setting display char sequence by method setCharSequence :

$loader->setCharSequence(array('.', '..', '. '));

About

PHP Shell Framework — creating shell applications

Источник

Run Shell Scripts in PHP and Open Shell File

Run Shell Scripts in PHP and Open Shell File

  1. Run Shell File in Text Mode Using shell_exec()
  2. Use shell_exec() to Return Binary Format in CLI

PHP allows us to use the shell_exec(); function to deal with shell files. However, if your OS is Windows, you should consider using popen() and pclose() functions because the pipes are executed in text mode, which usually keeps it from binary outputs.

We will implement two scripts in our shell.php file. First we will open the .sh file using shell_exec(); function.

Then, we will use shell_exec() to open the cmd interface and run a few windows commands.

Run Shell File in Text Mode Using shell_exec()

Function syntax and parameters: shell_exec(string $cmd); . This function returns shell output in the string format.

We have created a demo demo.sh file in this tutorial.

#!/bin/sh  echo "Hello world"; echo "This is a shell .sh file for demo"; // your shell commands go here 

You can create a .sh file using any text editor and save it with a .sh file extension. After that, please run the following PHP script ( shell.php ) to open it in Notepad because it will throw string format in text mode.

 head>  title>  Run Shell File in PHP and open CLI (shell) to run shell scripts  title>  head>  head>  form action="shell.php" method ="post" align="center">  input type="submit" value="Open .sh file using shell_exec() in PHP" name="openshellfile" />  input type="submit" value="Run cmd/shell on Windows using shell_exec() in PHP" name="opencmd" />  form>  body>  html> 
php // When user submits the form  if(isset($_POST['openshellfile'])) echo $res=shell_exec('PATH to the file/demo.sh'); > ?> 

Use shell_exec() to Return Binary Format in CLI

The shell_exec() function can be used for a variety of functions. Our method is an excellent way to use shell_exec() without running a function in the background.

You do not need to use popen() or pclose() either.

php // Ping facebook cmd (shell) // open cmd shell  if (isset($_POST['opencmd']))   //You do not need to use popen() or pclose() either to run this shell command  // you can add any command here, it will work!  //For example, you can control your Windows (CLI)  //You will only change the command after cmd.ex /k "Your Command"  $open = shell_exec('start cmd.exe /k ping "facebook.com"');  echo $open;  //function shell($open)   //check your php version  > ?> 

Although we could have run any command using our script, we only pinged facebook.com . For example, if you want to open a calculator through this function, type calc instead of ping «facebook.com» .

Before adding the above code in your PHP script, you first need to change the input field’s name in the HTML. Then add the above code in your shell.php file that we previously ran.

The scope of the command can be anything, and you can type any windows command and assign it to the $open variable. The script will run your command directly into the Windows shell (CLI in this example).

We executed a cmd command to ping facebook.com using shell_exec(); in the code above. However, you can type any command in the function parameter, and it will work.

Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.

Источник

Читайте также:  Площадь круга программа питон
Оцените статью