How to pipe/redirect output of a command in Windows Subsystem for Linux (WSL) to another using a system() call?

45 views (last 30 days)
GOAL: I would like to use a system() cal to WSL to execute bash commands in the Linux subsystem by utilizing pipes so that I can direct the output of one bash command to another. An example would be piping the output of a command like 'ls' to 'tail' so that I can capture the last line obtained from 'ls' under WSL. ('ls' is just an example here for the sake of simplicity; the focus is not on obtaining the same output it provides by directly using the MATLAB built-in functions.)
STEPS I tried:
>> system('wsl ls /mnt/c/ | tail -n 1')
'tail' is not recognized as an internal or external command,
operable program or batch file.
>> system('wsl "ls /mnt/c/ | tail -n 1"')
/bin/bash: ls /mnt/c/ | tail -n 1: No such file or directory
>> %another example, with 'wc' to count lines from a file using '<'
system('wsl wc -l </mnt/c/test.dat')
The system cannot find the path specified.
>> system('wsl "wc -l </mnt/c/test.dat"')
/bin/bash: wc -l </mnt/c/test.dat: No such file or directory
It appears to me that either the system call to wsl to execute bash commands do not recognize pipes or I/O redirection operators, or I am not using the correct syntax to make it work.
In case this may be compatibility issue regarding Windows updates on the WSL system, here's system specific information
Windows: Version 1903 (OS Build 18362.239)
WSL Distribution: Legacy,, running GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Any help would be appreciated.
  4 Comments

Sign in to comment.

Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!