How do I use pipes with a system() call using ssh? Parsing issue?

조회 수: 3 (최근 30일)
Folks,
I am having difficulty, I believe with parsing, of a string that I am using to make a system call on my Windows 10 system.
I can run the exact following command from my Windows 10 command prompt:
ssh user@domain /bin/ps -aef | /bin/grep SearchString | /bin/awk "{print $2}"
It will send the command with all the pipes correctly to the domain computer and return the result.
When I do this, it does not work:
[a, b] = system('ssh user@domain /bin/ps -afe | /bin/grep SearchString | /bin/awk "{print $2}"')
It returns a = 255 (error)
If I reduce this to
[a, b] = system('ssh user@domain /bin/ps -afe | /bin/grep SearchString')
it still fails.
If I further reduce this to
[a, b] = system('ssh user@domain /bin/ps -afe')
it works.
So I expect this has something to do with parsing the pipe symbol. Any ideas on how to address this? I tried replacing the pipe with '|' and \|, but that did not fix the issue.
Thank you,
Kris

채택된 답변

Kristoffer Walker
Kristoffer Walker 2019년 11월 12일
Folks,
I figured out my issue. This works.
system('ssh user@domain "/bin/ps -afe | /bin/grep SearchString | /bin/awk ''{print $2}''"')
Be careful reading the above line. The last series of quotes contain both single and double quotes.
Kris

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 11월 12일
[a, b] = system('ssh user@domain ''/bin/ps -afe | /bin/grep SearchString | /bin/awk "{print $2}"''')
Otherwise the | is going to be interpreted locally.
MATLAB does not exactly just submit the command to the local command shell: it does some parsing of it even on windows. If you look at the details, the processing of & for background execution is documented as taking place at parsing time, and also some details about blocking (I think it is) on windows systems are at the parsing level.
You should thus expect that an unquoted unescaped | will be able interpreted as something to be run on the local system: the unquoted unescaped | would mark the end of the ssh command

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by