Bash.AST.Redirect (Bash v0.3.0)

Copy Markdown View Source

I/O redirection.

Examples

# < input.txt
%Redirect{
  direction: :input,
  fd: 0,
  target: {:file, %Word{parts: [{:literal, "input.txt"}]}}
}

# > output.txt
%Redirect{
  direction: :output,
  fd: 1,
  target: {:file, %Word{parts: [{:literal, "output.txt"}]}}
}

# >> append.txt
%Redirect{
  direction: :append,
  fd: 1,
  target: {:file, %Word{parts: [{:literal, "append.txt"}]}}
}

# 2>&1 (redirect stderr to stdout)
%Redirect{
  direction: :duplicate,
  fd: 2,
  target: {:fd, 1}
}

# &> all_output.txt (redirect both stdout and stderr)
%Redirect{
  direction: :output,
  fd: :both,
  target: {:file, %Word{parts: [{:literal, "all_output.txt"}]}}
}

# <<EOF (heredoc)
# content
# EOF
%Redirect{
  direction: :heredoc,
  fd: 0,
  target: {:heredoc, %Word{parts: [{:literal, "content\n"}]}, "EOF", false}
}

# <<< "string" (herestring)
%Redirect{
  direction: :herestring,
  fd: 0,
  target: {:word, %Word{parts: [{:literal, "string"}]}}
}

Summary

Types

direction()

@type direction() :: :input | :output | :append | :duplicate | :heredoc | :herestring

t()

@type t() :: %Bash.AST.Redirect{
  direction: direction(),
  fd: integer() | :both | {:var, String.t()},
  meta: Bash.AST.Meta.t(),
  target: target()
}

target()

@type target() ::
  {:file, Bash.AST.Word.t()}
  | {:fd, integer()}
  | {:heredoc, content :: Bash.AST.Word.t(), delimiter :: String.t(),
     strip_tabs :: boolean()}
  | {:word, Bash.AST.Word.t()}