@@ -453,56 +453,54 @@ def parse(self, line: str, expand: bool = True) -> Statement:
453453 arg_list = tokens [1 :]
454454 tokens = []
455455
456- # check for a pipe to a shell process
457- # if there is a pipe, everything after the pipe needs to be passed
458- # to the shell, even redirected output
459- # this allows '(Cmd) say hello | wc > countit.txt'
456+ pipe_to = ''
457+ output = ''
458+ output_to = ''
459+
460+ # Find which redirector character appears first in the command
461+ try :
462+ pipe_index = tokens .index (constants .REDIRECTION_PIPE )
463+ except ValueError :
464+ pipe_index = len (tokens )
465+
460466 try :
461- # find the first pipe if it exists
462- pipe_pos = tokens .index (constants .REDIRECTION_PIPE )
467+ redir_index = tokens .index (constants .REDIRECTION_OUTPUT )
468+ except ValueError :
469+ redir_index = len (tokens )
470+
471+ try :
472+ append_index = tokens .index (constants .REDIRECTION_APPEND )
473+ except ValueError :
474+ append_index = len (tokens )
475+
476+ # Check if output should be piped to a shell command
477+ if pipe_index < redir_index and pipe_index < append_index :
463478
464479 # Get the tokens for the pipe command and expand ~ where needed
465- pipe_to_tokens = tokens [pipe_pos + 1 :]
480+ pipe_to_tokens = tokens [pipe_index + 1 :]
466481 utils .expand_user_in_tokens (pipe_to_tokens )
467482
468483 # Build the pipe command line string
469484 pipe_to = ' ' .join (pipe_to_tokens )
470485
471486 # remove all the tokens after the pipe
472- tokens = tokens [:pipe_pos ]
473- except ValueError :
474- pipe_to = ''
487+ tokens = tokens [:pipe_index ]
475488
476- # check for output redirect
477- output = ''
478- output_to = ''
479- try :
480- output_pos = tokens .index (constants .REDIRECTION_OUTPUT )
481- output = constants .REDIRECTION_OUTPUT
489+ # Check for output redirect/append
490+ elif redir_index != append_index :
491+ if redir_index < append_index :
492+ output = constants .REDIRECTION_OUTPUT
493+ output_index = redir_index
494+ else :
495+ output = constants .REDIRECTION_APPEND
496+ output_index = append_index
482497
483- # Check if we are redirecting to a file
484- if len (tokens ) > output_pos + 1 :
485- unquoted_path = utils .strip_quotes (tokens [output_pos + 1 ])
498+ if len (tokens ) > output_index + 1 :
499+ unquoted_path = utils .strip_quotes (tokens [output_index + 1 ])
486500 output_to = os .path .expanduser (unquoted_path )
487501
488502 # remove all the tokens after the output redirect
489- tokens = tokens [:output_pos ]
490- except ValueError :
491- pass
492-
493- try :
494- output_pos = tokens .index (constants .REDIRECTION_APPEND )
495- output = constants .REDIRECTION_APPEND
496-
497- # Check if we are redirecting to a file
498- if len (tokens ) > output_pos + 1 :
499- unquoted_path = utils .strip_quotes (tokens [output_pos + 1 ])
500- output_to = os .path .expanduser (unquoted_path )
501-
502- # remove all tokens after the output redirect
503- tokens = tokens [:output_pos ]
504- except ValueError :
505- pass
503+ tokens = tokens [:output_index ]
506504
507505 if terminator :
508506 # whatever is left is the suffix
0 commit comments