The Smart Card Shell
The Smart Card Shell (scsh) is small program (~400 bytes) that runs on a smart card and sequentially executes commands stored in files on the smart card. When you trigger the execution of a file of commands by scsh, you can pass along values for variables or parameters embedded in the commands in the file just like UNIX shell files or DOS BAT files. The results of the execution of one command can be passed as parametes to subsequent commands.
Here is a paper describing scsh in detail including code that implements it.