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

4 ビュー (過去 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 Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by