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

조회 수: 5 (최근 30일)
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
Walter Roberson
Walter Roberson 2019년 7월 24일
Double quotes within single quotes are just characters not string forming operators.
Mert Turkol
Mert Turkol 2019년 7월 24일
편집: Mert Turkol 2019년 7월 24일
Ah, you're right. Pretty straightforward, but I did not think about it at first.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by