def split_by_pipes(cmd):
    """
    Split the command by shell pipes, but preserve contents in 
    parentheses and braces. Also handles nested parens and braces.

    :param str cmd: Command to investigate.
    :return list: List of sub commands to be linked
    """ 

    # Build a simple finite state machine to split on pipes, while
    # handling nested braces or parentheses.   
    stack_brace = []
    stack_paren = []
    cmdlist = []
    newcmd = str()
    for char in cmd:
        if char is "{":
            stack_brace.append("{")
        elif char is "}":
            stack_brace.pop()
        elif char is "(":
            stack_paren.append("(")
        elif char is ")":
            stack_paren.pop()

        if len(stack_brace) > 0 or len(stack_paren) > 0:
            # We are inside a parenthetic of some kind; emit character
            # no matter what it is
            newcmd += char
        elif char is "|":
            # if it's a pipe, finish the command and start a new one
            cmdlist.append(newcmd)
            newcmd = str()
            next
        else:
            # otherwise, emit character.
            newcmd += char

    # collect the final command before returning
    cmdlist.append(newcmd)
    return cmdlist