23.4 The evalexec Function

23.4.1 Synopsis

evalexec (prog_file_name);
evalexec (prog_file_name, arguments);

23.4.2 Description

Execute prog_file_name and return its standard output as a string variable; prog_file_name is the name of the file storing the script or the executable and can be any valid expression:

The evalexec () function behaves exactly as the exec () one, the only difference is that output from script is returned as a variable and not included in Cows' output. Since there are many caveats related to these functions, read carefully the section Section 23.3: everything apply to evalexec () too (command line arguments, trailing ./ for script name, dependencies etc.).

23.4.3 Example

Create a file called imgs.sh with the following content (it's a simple shell script which display the number of jpeg images stored in a directory called images/ placed right under the site root):

#!/bin/sh
dir_img=$LA"images/*.jpg"
ls -l $dir_img | wc -l

Now, you have to make it executable:

chmod u+x imgs.sh

Now let's create a simple Cows script called evalexec.cws:

<h1>Executing an external Script</h1>
<cows>
n_images = evalexec ("./imgs.sh");
  n_images_int = toint (n_images);
  print ("There are " + n_images_int + " images.");
  /* Suppose you want to display images in rows of 4 images:
     the number of rows will be: */
  n_rows = n_images_int / 4;
  if ((n_images_int % 4) != 0)
    n_rows ++;
  print (n_rows + " rows needed");
</cows>

On my machine (I have 22 jpeg images in directory images/):

$ cows evalexec.cws evalexec.html
$ cat evalexec.html

<h1>Executing an external Script</h1>
There are 22 images.
6 rows needed

This manual can be downloaded from http://www.g-cows.org/.