G-Cows: Scripting Language for Web Content Management | ||
---|---|---|
Prev | Chapter 23 Executing external commands, scripts and programs | Next |
evalcmd (command
);
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:
evalcmd ("literal_command")
evalcmd (variable_storing_command_name)
evalcmd (variable_storing_command_name + variable_storing_options)
today = evalcmd ("date"); message = "Today: " + today; print (message);
Cows' output:
Today: Thu May 1 10:33:14 CEST 2003
// 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/.