If it's not possible to do what you want with the if-else control flow you could useAdvanced CLI scripting with Groovy, Rhino, Jython, etc. or try the JBoss AS7 Command-line public API and do it in java.
If you are talking about doing this interactively then I think you'll have to go with Mr. Fink's suggestion.
Stan