Perplexing output from the system() command

12 ビュー (過去 30 日間)
the cyclist
the cyclist 2017 年 1 月 19 日
コメント済み: Jess Crawshaw 2020 年 4 月 28 日
I am running R2106b on a Macbook Pro OS X Yosemite.
In a Terminal window (using bash shell), if I type
which which
I get the expected output
/usr/bin/which
and similarly
which psql
yields
/usr/local/bin/psql
All is well and good so far. But if I issue these same command from within MATLAB, using the system() command, I get output that perplexes me. As expected, when I type
[status cmdout] = system('which which')
I get
status =
0
cmdout =
'/usr/bin/which
'
But from
[status cmdout] = system('which psql')
I get
status =
1
cmdout =
0×0 empty char array
Finally, if I point directly to the known directory (which in my real application I would not know) ...
[status cmdout] = system('which /usr/local/bin/psql')
then the command is found
status =
0
cmdout =
'/usr/local/bin/psql
'
Does anyone have some insight into what is happening here? I am not a Unix expert by any means. But the whole thing seems to be a bit of a shell game. ;-)
(I can add the why I am doing this if needed, but I won't burden folks just yet.)
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 6 月 23 日
The causes are described below: when you start MATLAB from the icon, that is not a login shell so your shell setup files are not run and so your PATH environment variable does not include everything you might be expecting.
Jess Crawshaw
Jess Crawshaw 2020 年 4 月 28 日
Your answer below completely fixed my problems, thanks for this

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 25 日
psql is not on your PATH environment variable inside of MATLAB. When you enter the terminal then that would be a login shell and your bash profile would be executed, but when you start MATLAB from an icon then that is not a login shell and your bash profile would not be executed.
  2 件のコメント
the cyclist
the cyclist 2017 年 1 月 25 日
編集済み: the cyclist 2017 年 1 月 25 日
Thanks.
For the sake of others who may read this ...
The specific solution for me was to add /usr/local/bin to my MATLAB PATH environment variable. I did this by adding the line
setenv('PATH', [getenv('PATH') ':/usr/local/bin'])
to my startup file.
Walter Roberson
Walter Roberson 2017 年 9 月 10 日
/etc/profile and ~/.bashrc and ~/.bash_profile and ~/.bash_login and ~/.profile are only executed for interactive shells.
/etc/bashrc (Red Hat) or /etc/bash.bashrc (Ubuntu) are executed for non-interactive shells.

サインインしてコメントする。

その他の回答 (1 件)

Nikunj Kothari
Nikunj Kothari 2017 年 1 月 25 日
Are you getting the same output if instead of using system(), you use the exclamation point? The following documentation link elaborates on how to properly run external commands and scripts:

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by