MathGroup Archive 2004

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

Search the Archive

Re: how to run a shell command in mathematica?

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

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

Run["xterm&"]

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.";

UnixLinkQ[fn_String]:=If[Not[$OperatingSystem\[Equal]"Unix"],False,
                StringMatchQ[First[Join[ReadList["!ls -l "<>fn,String],{"
"}]],"*->*"]
        ]

SetAttributes[UnixLinkQ,Listable]



deUnixLink[tlink_String]:=Module[
      {llwords=ReadList["!ls -l "<>tlink,Word],newtlink},
      If[Length[llwords]\[GreaterEqual]3 &&
          llwords\[LeftDoubleBracket]-3\[RightDoubleBracket]==tlink&&
          llwords\[LeftDoubleBracket]-2\[RightDoubleBracket]=="->",
        ReadList["!rm "<>tlink,Record];
        newtlink=OpenWrite[tlink];
        WriteString[newtlink,"<<deUnixLinked>>  "];
        Write[newtlink,llwords];
        Close[newtlink]
        ]
      ]/;UnixLinkQ[tlink]



reUnixLink[tlink_String]:=Module[
      {llwords=
          If[Not[UnixLinkQ[tlink]]&&
              Read[tlink,Word]\[Equal]"<<deUnixLinked>>",
            Read[tlink,Expression]
            ]},
      If[Length[llwords]\[GreaterEqual]3&&
          llwords\[LeftDoubleBracket]-3\[RightDoubleBracket]==tlink&&
          llwords\[LeftDoubleBracket]-2\[RightDoubleBracket]=="->",
        DeleteFile[tlink];
        ReadList[
          "!ln -s "<>llwords\[LeftDoubleBracket]-1\[RightDoubleBracket]<>"
"<>
            llwords\[LeftDoubleBracket]-3\[RightDoubleBracket]];
        tlink
        ]
      ]/;$OperatingSystem\[Equal]"Unix"




"Yasvir Tesiram" <tesiramy at omrf.ouhsc.edu> wrote in message
news:bvq74q$5jj$1 at smc.vnet.net...
> 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 ustc.edu 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?