UNIX bash shell in an m file?

조회 수: 4 (최근 30일)
alexander
alexander 2012년 2월 14일
편집: Matt J 2013년 10월 12일
Hello,
I need to invoke a bash file from matlab which is easy enough to do. However i would like instead to write the bash code in an m file and execute the bash commands. The bash code would invoke a standalone application and pipe some things to it.
Can i simply have all bash commands in a string and use
system(string)
so it would look something like
system('path/ish -file placeholder -mode text <<XXX...
a...
b...
3...
ect ...
XXX')

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 14일
You would have to emit newline characters in the string you submit to system(), as MATLAB is not able to construct multi-line quoted strings the way that a shell is.
If you want bash specifically, you need to invoke it by name. The shell used by system() is the $SHELL of the environment, which is not certain to be bash. On the other hand, you should be able to take advantage of the #! magic number that is recognized by unix exec*() calls, by coding the first line of path/ish as
#!bash
and then no matter which shell is your default shell, bash would be invoked to handle path/ish
sysstr = sprintf('path/ish -file placeholder -mode text <<XXX\na...\nb...\n3...\nect ...\nXXX');
system(sysstr);
  댓글 수: 4
alexander
alexander 2012년 2월 14일
ish is of type executable (application/x-executable) and i cannot edit its source code. However i have found that when i enter unix('/bin/bash','-echo') into the command window i can access the terminal through matlab. When i attempt to call ish or bps though it gives me an error message:
bps: /usr/local/MATLAB/R2011b/sys/os/glnx86/libgfortran.so.3: version `GFORTRAN_1.4' not found (required by bps)
any ideas how to fix this? Thanks in advance
Walter Roberson
Walter Roberson 2012년 2월 14일
Ah, I was thinking that "ish" was a script.
You have a bit of a problem in that here documents ("<<") are processed by whatever shell system() is using. Some shells have different meanings for here documents. In particular, the way to end a here document is different in csh than it is in bash, Borne shell, Korn shell, or the Open Group Single Unix Standard "sh" shell (sometimes referred to as the POSIX shell.) If you can promise that the user shell will _not_ be csh then the syntax you are using is okay and it will not matter which of those other shells I mentioned is in use. If you want to be certain, though, then you would be better in writing the here document to a file in MATLAB and then redirecting input from that file.
The GFORTRAN_1.4 problem is a gfortran bug in release 4.5, and appears to have been fixed in 4.6 if I read the reports correctly. Please check which gfortran you have installed and see if you have the latest stable version. If you do, then please have a look at the workaround described at
http://judsonsnotes.com/notes/index.php?option=com_content&view=article&id=611:matlab-running-external-programs&catid=57:programming&Itemid=81

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

추가 답변 (1개)

Kaustubha Govind
Kaustubha Govind 2012년 2월 14일
Yes, I think that is exactly how you would do it. Are you running into any trouble doing that?
  댓글 수: 2
alexander
alexander 2012년 2월 14일
well the problem is that i have a floating license which is currently in use so i cannot try it yet. I was just checking with the community to see if this was a reasonable thing to try. I am concerned that the pipeline will cause an error.
Kaustubha Govind
Kaustubha Govind 2012년 2월 14일
The command would execute exactly as it would in a shell - so I would first test that line in the shell to make sure it's error free.

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

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by