Brian Dunning's FileMaker Custom Functions

SHEXEC ( CMD ; STDIN ; PARM )

The function executes a SHELL script in OS X and returns the standard output

  Average rating: 4.0 (2 votes) Log in to vote

Erich Schmidt   Erich Schmidt - Show more from this author

Share on Facebook Share on Twitter

  Sample input:
SHEXEC ("bc";"obase=$1¶$2";List(2;512+5+2^5))

SHEXEC("ls $HOME/$1";"";"bin")

SHEXEC("bc";"a=$1¶b=$2¶d=2*a^2+1¶d¶scale=5¶sqrt(d)¶a+2*b^2-261¶if(b) a/b";List(70;13))
  Sample output:
1000100101

List of all directory entries from $HOME/bin

9801
99.00000
147
5.38461

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

The function executes a shell script using the MBS plugin function "AppleScript.Run" and returns the standard output. Note, that the function is useable only for Mac users (OS X).


Parameters:

CMD - The script text as a string. For testing more sophisticated scripts it's recommendable to put the text into a text field of any database table. Before executing the script it can read (for instance) by an SQL-Statement into a script variable.

STDIN - The input data read by the script. If the script needs no data, specify an empty string.

PARM - The script parameters as a FileMaker list. Line n contains the n-th parameter. In shell scripts are the paraeters separated by a sequence of one ore more spaces. Therefore you must every parameter containing spaces enclose in single quotes. The first parameter must not begin with a - (minus).


Some further examples:

Example 1:

The following script removes multiple list entries and returns the remaining entries in original order. Assumed the variable $INPUT contains the following list:

one
two
three
one
four
six
three
seven
one
two
eight
nine
six
seven
five
sixty-four

The function call

SHEXEC ( "awk '{if( !($0 in line) ) line[$0]=NR}¶
END{for(i in line) print line[i],i}' | sort -n | sed 's/^[0-9]* //'";$INPUT; "")

produces the list

one
two
three
four
six
seven
eight
nine
five
sixty-four

If there is no need to keep the output in original sorted order you can use this:

SHEXEC("awk '{line[$0]}¶END{for(i in line) print i}'";$INPUT;"")

Exampel 2:

Consider the script

awk '{x[NR] = $0; m = length($0) > m ? length($0) : m;}
END{for(z = 1;z for( partner=z+1;partner <=NR; partner++) printf("%-*s - %s\n",m+offset(x[z]), x[z], x[partner])}
function offset(word){
n=gsub("[äöüßÄÖÜ]","",word);
return(n/2)
}'

Assumed the input contains the following lines:

Berlin
New York
Paris
Vienna

The output will be a list with all possible couples:
Berlin - New York
Berlin - Paris
Berlin - Vienna
New York - Paris
New York - Vienna
Paris - Vienna

Example 3:

If $INPUT contains a list like this (for instance given by an SQL-Statement):

M0002 2015-10-06 2015 22.3
M0011 2014-12-09 2014 198.01
M0022 2014-03-26 2014 12.81
M0022 2014-04-01 2014 78.99
M0002 2015-07-08 2015 12.81
M0001 2016-06-15 2016 6.71
M0003 2015-08-13 2015 24.31
M0001 2016-06-08 2016 36.91
M0022 2016-02-02 2016 1009.02
M0002 2015-04-19 2015 88.93
M0013 2015-03-06 2015 16.8
M0002 2016-01-05 2016 19.21
M0002 2016-02-26 2016 38
M0002 2016-03-17 2016 16.91

and we want to sum up all values in column 4 for same values in column 1, so we can use the following script:

awk '{sum[$1] += $4}
END {for( i in sum) print i, sum[i]}'

The SHEXEC function call put out:

M0001 43.62
M0002 198.16
M0003 24.31
M0022 1100.82
M0011 198.01
M0013 16.8

Example 4:
Assumed, that the script variable $STDIN contains the following text:

scale=0
p[1]=2
n=1
define p(x){
pz=1
for(z=1;z<=sqrt(n);z++){
if(x % p[z] == 0){
pz=0;
break;
}
}
p[++n]=x;
return(pz)
}
p[1]
for(i=3;i<=$1;i+=2){
if( p(i) ) i
}

the function call SHEXEC("bc -l";$STDIN;15013) returns a list of all prime numbers from 2 to 15013.

Example 5:
For a list like $INPUT from Example 3, containing multiple columns, the call
SHEXEC("awk -v COL=$1 '{print $COL}' ";$INPUT;n) where n is a column number (from 1 to 4) generates a list with only the selected column.


For more informations see also the manuals of the mentioned commands (bc, sort, sed, awk ).
The following script opens a pdf-file with a command description. Specify the command name (e.g. awk) as first parameter.

test "$1" && { man -t $1 | pstopdf -i -o /tmp/$1.pdf ; open /tmp/$1.pdf ;} || echo parameter missing '(command)'

 

Comments

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 15 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth:

Under construction. Email me your wish list for improvements.