Files
lush/issues/07-redirection.md
Cormac Shannon bad40fb65b Standardize issue status labels to done/open/in progress
Rename resolved→done (#1-4, #11), closed→done (#13, #18),
open (post-core)→open (#7), mark #9 as superseded by #21.
2026-03-15 19:33:49 +00:00

49 lines
1.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 07 — Implement I/O redirection for commands
**Status:** open
**Blocked by:** #03
## Syntax
```lua
`ls -l` > "output.txt" -- redirect stdout to file
`ls -l` >> "output.txt" -- append stdout to file
`cmd` 2> "err.txt" -- redirect stderr to file
`cmd` 2>&1 -- merge stderr into stdout
`cmd` < "input.txt" -- redirect stdin from file
```
### Alternatively
```lua lush
`ls -l > output.txt` -- redirect stdout to file
`ls -l >> output.txt` -- append stdout to file
`cmd 2> err.txt` -- redirect stderr to file
`cmd 2>&1` -- merge stderr into stdout
`cmd < input.txt` -- redirect stdin from file
```
## Implementation
- Before `execvp()` in the child process, use `dup2()` to redirect file descriptors
- Parse redirection operators as part of the command syntax
- Open target files with appropriate flags:
- `>` — `O_WRONLY | O_CREAT | O_TRUNC`
- `>>` — `O_WRONLY | O_CREAT | O_APPEND`
- `<` — `O_RDONLY`
- `2>&1` — `dup2(stdout_fd, STDERR_FILENO)`
## Challenge
`>` and `>>` conflict with Lua's greater-than and right-shift operators. Like piping, these operators must only be valid in command context. The parser can disambiguate because the left-hand side is a command expression.
## Alternatives
- Although shell redirection isnt available, simple redirection can be achieved using the `io` library.
```lua lush
file = io.open("OUTFILE.txt", "w")
file:write(`ls /`.stdout)
file:close()
```