> I often feel the need to pass output of a shell command into an AMPL
> parameter. Is that possible?
I sometimes do this by redirecting the output of the shell command to
a file, then reading the file into an ampl parameter. e.g., this can
check whether a certain file already exists:
option my_file_name "some file.txt";
option status_file "_stat_file.txt";
param readfile symbolic;
shell ('if test -f "' & $my_file_name & '" ; then echo 1 ; else echo
0 ; fi') > ($status_file);
close ($status_file);
read readfile < ($status_file);
close ($status_file);
remove ($status_file);
Note that this will only work if the shell command produces a single
word of output. If you need a longer string, e.g., the result of the
"uptime" command, you could enclose it in quotes, like this:
shell ('echo \"`uptime`\"') > ($status_file);
close ($status_file);
read readfile < ($status_file);
close ($status_file);
remove ($status_file);
I haven't figured out a way to do this without creating a temporary
file. But if you just need a status flag back from the shell command,
you can use the built-in shell_exitcode parameter. e.g., an example
from Robert Fourer of a better way to check for a file's existence:
shell ('test -f "' & $my_file_name & '"'); # or use "file -f ..." or
"dir ..."
if shell_exitcode == 0 then {
# do something with the file
} else {
# the file is not there -- do something else
}
For some reason, if the file doesn't exist ampl reports "exit code 1"
but shell_exitcode is set to 256. But the example above is still
workable.
Matthias Fripp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to
ampl@...
To unsubscribe from this group, send email to
ampl+unsubscribe@...
For more options, visit this group at
http://groups.google.com/group/ampl?hl=en-~----------~----~----~----~------~----~------~--~---