MathGroup Archive 2004

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

Search the Archive

Re: Re: how to run a shell command in mathematica?

  • To: mathgroup at
  • Subject: [mg46099] Re: [mg46071] Re: how to run a shell command in mathematica?
  • From: Yasvir Tesiram <yat at>
  • Date: Fri, 6 Feb 2004 04:15:21 -0500 (EST)
  • References: <> <bvq74q$5jj$> <>
  • Sender: owner-wri-mathgroup at

I normally would avoid a reply to such threads but it seems as though 
as what you have proposed below is overly buttered.  If you use the 
notebook interface, sure, you need some way of getting all that output 
back into Mathematica.  Sounds like a a round about way of getting from 
a to b when you can simply write an entire shell script in Mathematica 
and push it through to the command line. It's very simple unstructured 
communication. Why complicate matters? Or is Unix system admin supposed 
to be shrouded in complexity? If you want to really do it properly with 
Mathematica, use the proper tool, Mathlink. But by the time you finish 
writing that C program, there are probably a handful of Unix and 
Mathematica programs that could have combined to do the job for you in 
less time without having to look at any output at all.
Your functions below are more for sanity checking than anything else 
and its good practice. So triffic (terrific). The basic mechanism of 
unstructured communication remains the same whether it's Joe Bloggs 
backyard Unix box or Arthur Marthers Windows desktop. However, Joe 
Blogss output probably comes out on a terminal window or maybe being 
tricky, he has it redirected to a console but he has to decipher it and 
Arthur Marthers gets sent to George Porge for deciphering. In short, 
keep it simple, or use mathlink.

All the best.

On Feb 5, 2004, at 3:02 AM, John Jowett wrote:

> 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> 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: InputAutoReplacements With "-marks
  • Next by Date: Mathematical Statistics
  • 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?