def replace_vars(cmd, params):
    """ Replace any occurance of $VAR in cmd, with its value in params if
        one present. Returns string with replacement."""
    fullcmd = ""
    args = cmd.split(" ")
    for arg in args:
        if arg.startswith("$"):
            param = arg[1:]
            if param in params:
                fullcmd = fullcmd + params[param]
            else:
                fullcmd = fullcmd + arg
        else:
            fullcmd = fullcmd + arg
        fullcmd = fullcmd + " "
    fullcmd = fullcmd.strip()
    return fullcmd