23.2 The evalcmd Function

23.2.1 Synopsis

evalcmd (command);

23.2.2 Description

Execute command and return its standard output as a string variable; command can comprehend options (e.g. ls -l) and can be any valid expression:

23.2.3 Example

today = evalcmd ("date");
message = "Today: " + today;
print (message);

Cows' output:

Today: Thu May  1 10:33:14 CEST 2003

23.2.4 Example 2

// ls -l DIR | wc -l gives the number of files stored in DIR
command = "ls " + linkadjust () + "images | wc -l";
n_images = evalcmd (command);
print ("There are " + n_images + " images");

If command returns a number, you may need to convert it into an integer; suppose you need two lines of text for each image:

// ls -l DIR | wc -l gives the number of files stored in DIR
command = "ls " + linkadjust () + "images | wc -l";
n_images = evalcmd (command);
n_lines = n_images * 2; // WRONG !! n_images is a string

This is wrong ! A string multiplied by an integer gives the string repeated 3 times ("pamela" * 3 gives "pamelapamelapamela" and "4" * 3 gives 444). A conversion from string to integer is needed:

// ls -l DIR | wc -l gives the number of files stored in DIR
command = "ls " + linkadjust () + "images | wc -l";
n_images = evalcmd (command);
// n_lines = n_images * 2 // WRONG !! n_images is a string
n_lines = toint (n_images) * 2;

Most of the times you won't need this conversion since Cows's automatic type conversion will do the job for you.

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