UNIX bash shell in an m file?

2 views (last 30 days)
alexander
alexander on 14 Feb 2012
Edited: Matt J on 12 Oct 2013
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')

Accepted Answer

Walter Roberson
Walter Roberson on 14 Feb 2012
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 Comments
alexander
alexander on 14 Feb 2012
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 on 14 Feb 2012
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

Sign in to comment.

More Answers (1)

Kaustubha Govind
Kaustubha Govind on 14 Feb 2012
Yes, I think that is exactly how you would do it. Are you running into any trouble doing that?
  2 Comments
alexander
alexander on 14 Feb 2012
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 on 14 Feb 2012
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.

Sign in to comment.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!