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    
Last Updated Ratings Unique User Downloads Download Rankings
2016-06-18 (3 years ago) RSS 2.0 feedNot enough user ratingsTotal: 144 All time: 8,891 This week: 369Up
Version License PHP version Categories
shell-action-tools 1.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
  Performance   Level  
Name: Karl Holz <contact>
Classes: 13 packages by
Country: Canada Canada
Age: 39
All time rank: 51419 in Canada Canada
Week rank: 96 Up2 in Canada Canada Up
Innovation award
Innovation award
Nominee: 8x

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:144
This week:0
All time:8,891
This week:369Up

For more information send a message to info at phpclasses dot org.