PHP Classes

Automator Shell Action Tools: Process console input and environment variables

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 147 All time: 9,099 This week: 101Up
Version License PHP version Categories
shell-action-tools 1.0.0MIT/X Consortium ...5.5PHP 5, Console
Description 

Author

This class can process console input and environment variables.

It is a base class that can read the input of PHP scripts run from the command line and store the input lines in an array.

The class can also retrieve the values of defined environment variables, ignoring certain variable values to make it easier to process the most important variables.

Innovation Award
PHP Programming Innovation award nominee
July 2016
Number 9


Prize: One copy of DWebPro Standard License
When PHP runs from the command line it can take parameters from an array, but if the data is passed as command input it must be read as file.

However, if no input stream is passed PHP may hang waiting for the input data to be read from what the user types in the console.

This class solves this problem but making adding a timeout of 1 second when attempting to read data from the input. If no input file is passed, PHP will not hang anymore. If an input file is passed, the class will read all its lines into an array.

Additionally it can retrieve the array of environment variables, discarding a given list of irrelevant variables.

Manuel Lemos
Picture of Karl Holz
  Performance   Level  
Name: Karl Holz <contact>
Classes: 14 packages by
Country: Canada Canada
Age: 43
All time rank: 44918 in Canada Canada
Week rank: 22 Up1 in Canada Canada Up
Innovation award
Innovation award
Nominee: 9x

Example

#!/bin/bash

echo -e "Command with out arguments: \n php main.command \n"
php main.command


echo -e "Command with 2 arguments: \n arg1=\"Hello\" arg2=\"World!\" php main.command \n"
arg1="Hello" arg2="World!" php main.command

echo -e "Command with 2 arguments and piped input: \n ls -l | arg1=\"Hello\" arg2=\"World!\" php main.command \n"
ls -l | arg1="Hello" arg2="World!" php main.command

echo -e "Command with 2 arguments and input doc: \n"
cat <<+
arg1="Hello" arg2="World!" php main.command <<+
[new]
one="Hello"
two="World"
+
echo -e "+\n"
arg1="Hello" arg2="World!" php main.command <<+
[new]
one="Hello"
two="World"
+


Details

Shell Action Tools

In my research into building Automator Actions with Shell Scripts (such as bash/sh/ksh), I notices that hasn't been much written about the subject. So I have based my work on these urls:

This is a small effort to show that PHP can be used in more places than just Websites; this class is aimed a processing command input and enviroment variables passes to the script from the prevous Automator Action. Much like a pipe ( | ) when using the command line

This class will process the variables set and passed before the command (set as values in the enviroment variable $_ENV) and the document sent to standard input ({command} < STDIN).

Using the Code in main.command

> \#!/usr/bin/php > > <?php > > require 'ShellActionTools.class.php'; > > $s=new ShellActionTools(); > > echo "arg is: " . $s->arg . "\n"; > echo "arg2 is " . $s->arg2 . "\n"; > > exit();

Debuging Class on console

> $ arg="one" arg2="two" php main.command < config-file

or like this

> $ ls | arg="one" arg2="two" php main.command

As you can see with the example above, you can use this class to help you process to types of input for a shell tool; but the main focus is building an Mac OS X Automator.app Action in XCode using shell scripting -- PHP in this case. Use the debug.main.command.sh script to see the basics of this class in Action.

Ignored ENV keys

  • LDFLAGS
  • CPPFLAGS
  • TMPDIR
  • LANG
  • PKG_CONFIG_PATH
  • TERM_PROGRAM
  • TERM
  • SHELL
  • TERM_PROGRAM_VERSION
  • TERM_SESSION_ID
  • USER
  • SSH_AUTH_SOCK
  • LOGNAME
  • SECURITYSESSIONID
  • DBUS_LAUNCHD_SESSION_BUS_SOCKET
  • XPC_FLAGS
  • XPC_SERVICE_NAME
  • HOME
  • PATH
  • PWD
  • OLDPWD
  • PYTHONPATH
  • SHLVL
  • _
  • __CF_USER_TEXT_ENCODING
  • Apple_PubSub_Socket_Render

These were removed so that it would be easyer to process the enviroment variables passed or set on the command line

XCode Interface

The XCode interface builder isn't that hard to use once you become fimilar with the tools and the items you can add to the interface. The links above will give you a short guide on how to build an automator shell script action; the pages are dated, showing screen shots from Mac OS X 10.6 or 10.5.

I created my VLC-Cutter action with Mac OS X 10.11.3

Please take a look at macosxautomation.com for more ideas on what you can do with Automator workflows.

\*NIX Users, turn that frown upside down

If you take a look at the Debugging Class on the console section, you'll notice that this class can be used in normal Linux/Unix shell scripts with PHP using the same format; I'm not going to leave you linux heads empty handed, apple isn't the only fruit computer I use ;) ... Raspbery Pi's could also benifit from our PHP skills!

This class will only be focussed on dealing with the piped input and enviroment variables. I'm most likely going to build something that will deal with getops related stuff later on, maybe an extention of this class.


  Files folder image Files  
File Role Description
Accessible without login Plain text file debug.main.command.sh Example Sample output
Accessible without login Plain text file LICENCE Lic. License text
Accessible without login Plain text file main.command Example Example script
Accessible without login Plain text file README.md Doc. Class source
Plain text file ShellActionTools.class.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:147
This week:0
All time:9,099
This week:101Up