fangpeina opened a new pull request, #3379:
URL: https://github.com/apache/nuttx-apps/pull/3379
## Summary
This PR implements stderr redirection support in NSH (NuttShell), enabling
Bash-like syntax for redirecting standard error output. This enhancement
improves NSH compatibility with standard Unix shells and provides better error
handling capabilities.
### Features Added:
- Support `2>` operator for stderr redirection (overwrite mode)
- Support `2>>` operator for stderr redirection (append mode)
- Support `2>&1` syntax to redirect stderr to stdout
- Works with both foreground and background commands
### Usage Examples:
```bash
nsh> ls noexists 2> err.log # Redirect stderr to file
nsh> ls noexists 2> err.log & # Background with stderr redirect
nsh> ls noexists 2>> err.log # Append stderr to file
nsh> sh < /dev/ttyS0 > /dev/ttyS0 2> /dev/ttyS1 & # Multiple redirections
nsh> sh < /dev/ttyS0 > /dev/ttyS0 2>&1 & # Merge stderr to stdout
```
### Implementation Details:
- Added stderr file descriptor field to `nsh_vtbl_s` structure
- Extended redirection parsing logic in `nsh_parse.c` to handle stderr
operators
- Implemented `nsh_redirect_stderr()` function in `nsh_fileapps.c`
- Updated console and script handlers to support stderr redirection
## Impact
- **Features**: Adds stderr redirection capability to NSH shell, enhancing
compatibility with standard Unix shell syntax
- **API Changes**: Modified `nsh_vtbl_s` structure to include stderr file
descriptor (internal API only, no user-facing changes)
- **User Experience**: Users can now redirect error messages separately from
standard output, improving script debugging and error logging
- **Build**: No build system changes required
- **Hardware**: No hardware-specific dependencies, works on all platforms
- **Documentation**: No documentation updates required (standard shell
feature)
- **Security**: No security implications
- **Compatibility**: Fully backward compatible - existing NSH commands and
scripts continue to work unchanged
## Testing
### Test Configuration:
- **Platform**: QEMU simulation (sim:nsh)
- **Architecture**: Host architecture via NuttX simulator
- **Config**: sim:nsh
### Runtime Test Results:
**Test 1: Basic stderr redirection (overwrite mode)**
```bash
nsh> ll 2> err.log
nsh> cat err.log
nsh: ll: command not found
```
**Test 2: Stderr append mode**
```bash
nsh> ll 2> err.log
nsh> cat err.log
nsh: ll: command not found
nsh> ls noexists 2>> err.log
nsh> cat err.log
nsh: ll: command not found
nsh: ls: stat failed: 2
```
**Test 3: Multiple append operations**
```bash
nsh> ls noexists 2> err.log
nsh> cat err.log
nsh: ls: stat failed: 2
nsh> ls file1 2>> err.log
nsh> cat err.log
nsh: ls: stat failed: 2
nsh: ls: stat failed: 2
```
**Test 4: Redirect stderr to stdout (2>&1)**
```bash
nsh> ls > log 2>&1
nsh> cat log
/:
bin/
data/
dev/
err.log
etc/
log
proc/
tmp/
```
### Hardware Platforms Tested:
- [x] NuttX simulator (sim:nsh configuration)
- [ ] Physical hardware (not required for shell feature testing)
### Code Style Check:
```bash
$ /home/fpn/disk/github/nuttx/tools/checkpatch.sh -g HEAD~
✔️ All checks pass.
```
## Documentation
N/A - This implements standard Unix shell stderr redirection syntax that is
well-documented in general shell documentation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]