MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: how to run a shell command in mathematica?

  • To: mathgroup at
  • Subject: [mg46071] Re: how to run a shell command in mathematica?
  • From: "John Jowett" <John.Jowett at>
  • Date: Thu, 5 Feb 2004 04:02:51 -0500 (EST)
  • Organization: CERN
  • References: <> <bvq74q$5jj$>
  • Sender: owner-wri-mathgroup at

As I have mentioned in previous posts to this group, execution of command
lines in Unix, Linux or Windows is often best done *without*  the Run[]
function (by the way, I would be interested to know if anyone sees
disadvantages of the following).

In this case you could try

ReadList["!chmod a+x some-file", Record]

(note the exclamation mark) which will return the console output as a list
of strings, avoiding the need for redirection and temporary files.  An
equivalent method is

Import["!chmod a+x some-file","Lines"]

Other options of ReadList or Import allow you to get the output in other
forms, depending on what you want to do with it.   Simply applying TableForm
as in

Import["ls -l", "Lines"]//TableForm

will display the output in a readable fashion in the notebook interface.

Run[] does have its uses for non-console commands.  E.g. to launch a new
terminal window you need to do


and ReadList is not appropriate.

As a slightly more complicated example, here are some functions from my
personal utilities package that extend Mathematica's system interface by
providing functions for dealing with Unix links.  I use them for
de-activating links prior to, e.g., archiving or copying files with certain
programs.   Select[FileNames[],UnixLinkQ]  will give a list of files that
are links and so on.

UnixLinkQ::usage:="UnixLinkQ[filename] returns True if a file exists with
the \
name filename and is a Unix link.";

deUnixLink::usage:="deUnixLink[linkname] if linkname is a Unix link, \
de-activates it, replacing it with a file that can be re-activated by \
reUnixLink and returns the filename.  Otherwise returns Null.";

reUnixLink::usage:="deUnixLink[linkname] if linkname is a Unix link that has
been de-activated by deUnixLink then it will be re-activated.  Otherwise \
returns Null.";

                StringMatchQ[First[Join[ReadList["!ls -l "<>fn,String],{"


      {llwords=ReadList["!ls -l "<>tlink,Word],newtlink},
      If[Length[llwords]\[GreaterEqual]3 &&
        ReadList["!rm "<>tlink,Record];
        WriteString[newtlink,"<<deUnixLinked>>  "];

          "!ln -s "<>llwords\[LeftDoubleBracket]-1\[RightDoubleBracket]<>"

"Yasvir Tesiram" <tesiramy at> wrote in message
news:bvq74q$5jj$1 at
> First thing that springs to mind is;
> Run["chmod a+x some-file"]
> A couple of other things spring to mind;
> 1. Make sure that SetDirectory has been used prior to Run or specify the
> full path to the file.
> 2. The command above will return the status code of the operation, 0 for
> success. If you get anything else then something went awry.
> 3. You can Run["ls -al"] to check the permissions. But note, that you will
> not get anything in the Notebook interface. You will need to redirect the
> output of "ls -al", i.e. Run["ls -al > tmp.out"] or similar, then read it
> into the Mathematica notebook however you please.
> Cheers
> Yas
> On Tue, 3 Feb 2004 sunyb at wrote:
> > Hi,
> > I want to change some file's mode such as "chmod a+x some-file" in
> > mathematica, what should i do?
> >
> > thx!
> >

  • Prev by Date: Re: MathML Namespace + Mathematica
  • Next by Date: Re: clarification. Re: one liner for a function?
  • Previous by thread: Re: how to run a shell command in mathematica?
  • Next by thread: Re: Re: how to run a shell command in mathematica?