Passing an Array Argument to Shell Using CFExecute

Written by

Here’s a small post offering a solution to an issue I had recently. My goal was to run a shell script using cfexecute, one of the arguments being an Array itself. If your passing arguments in this manner on Linux (my flavour) elements are copied into an array of exec() arguments and I kept getting an error back from ColdFusion stating I was trying to pass off a complex object as a simple one (or something along those lines - makes sense). So the key was trying to fathom out how to pass my array of Strings to this Shell script, after a bit of trial and error I got it working by doing the following: + From Flex I now join the Array of Strings to form a space separated String e.g. myArrayOfStrings.join(’ ‘); + I then create an Array of arguments in ColdFusion to pass to the Shell script:

1
2
3
4
5
6
7
8
9
10
<cfset args = arrayNew(1)>
<cfset args[1] = #foo#>
<cfset args[2] = #joined_strings#>

<cfexecute name = "myscript.sh"
             arguments = "#args#"
                 variable="_result"
             errorVariable="_error"
                 timeout = "99">
</cfexecute>
  • And lastly in the Shell script I create an Array from the joined_strings argument, looping through this and performing necessary actions on each element:
1
2
3
4
5
6
7
8
9
#!/bin/bash
NAME=$1
STR=$2
STRINGS=(${STR[*]})
echo: foo: $NAME
for ix in ${!STRINGS[*]}
do
    echo "${STRINGS[$ix]}"
done

Output in Flex:

1
2
3
[trace] foo: newtriks
[trace] "one"
[trace] "two"

This is not my area of expertise so if anyone can offer better solutions they would be appreciated.

This link helped with the final hurdle, thank you to the author!

Comments