OS independent scripts, system calls on Windows and Linux 
Author Message
 OS independent scripts, system calls on Windows and Linux

Hi,

I need to script some tools (Xerces, Saxon, Dot, etc).
Since it should be OS independent, and must run unchanged
on at least Linux and any Windows, and should be
very simple, elegant, and quick to write, I choose Ruby over .bat
or .sh scripts.

I'll chain about seven actions, and need to know
if there were errors at any point, or if all went well.

I'm not that experienced yet, so I might miss some obvious
solutions, but AFAICS, the backtick system call
mechanism returns stdout/sterr, but is not OS independent.
If that's so, it should be made OS independent IMHO;
That would be the simplest solution for me.
I don't want to write all the cmd.exe stuff for various Windowses,
each time I write a little script.

The system() method seems to be OS independent, but doesn't
return anything, IIRC. (again: I didn't yet test or read
enough; please correct my assumptions)

Perhaps something like:

commands = # ...
commands.each do |cmd|
   returned = system cmd
   # system (or 'rsys') would return
   #  sterr, or stdout, or nil
   if returned
     case ret
       when Stderr then raise returned
       when Stdout then puts  returned
     end
   end
end

Or is there already a way to write an equiv of the above
which is as short, or shorter?

Ideally there would be
   commands.run
which would do what the above pseudo code outlines.

Tobi

--
http://www.*-*-*.com/



Tue, 09 Nov 2004 19:43:32 GMT  
 OS independent scripts, system calls on Windows and Linux
sorry, typo:

 > commands = # ...
 > commands.each do |cmd|
 >   returned = system cmd
 >   # system (or 'rsys') would return
 >   #  sterr, or stdout, or nil
 >   if returned
 >     case ret

       case returned

 >       when Stderr then raise returned
 >       when Stdout then puts  returned
 >     end
 >   end
 > end

--
http://www.pinkjuice.com/



Tue, 09 Nov 2004 20:09:30 GMT  
 OS independent scripts, system calls on Windows and Linux
Hi,

At Fri, 24 May 2002 20:38:59 +0900,

Quote:

> I'm not that experienced yet, so I might miss some obvious
> solutions, but AFAICS, the backtick system call
> mechanism returns stdout/sterr, but is not OS independent.

The backtick returns stdout only and is almostly system
independent.

Quote:
> The system() method seems to be OS independent, but doesn't
> return anything, IIRC. (again: I didn't yet test or read
> enough; please correct my assumptions)

Correct.  It's OS independent to the same extent as the
backtick.

Quote:
> Perhaps something like:

> commands = # ...
> commands.each do |cmd|

     system(cmd) or raise(cmd)

Quote:
> end

--
Nobu Nakada


Tue, 09 Nov 2004 19:59:07 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

> The backtick returns stdout only and is almostly system
> independent.

Then I might think about an RCR for a feature which saves me from
writing stuff like

[ http://ruby-talk.com/9739 ]
..
  if ENV.has_key?("OS") && ENV["OS"] =~ /nt/i
                puts "It's either NT or 2K, use 'cmd.exe /c '" if $DEBUG
                #it's either NT or Win2K
                command_shell = "cmd.exe /c "
..

What do people think?

I'd like to have a way more platform independent system call mechanism,
in Ruby itself or in the std lib, which returns either nil, an Error
object of some sort, or a Stdout object of some kind, so I can simply
write something like

commands = Commands.new ['this -x -y','that -a -b']
commands.each do |cmd|
   returned = cmd.issue # or ".run", ".enter", ...
   case returned
     when Stderr then raise returned
     when Stdout then puts  returned
   end
end

Would others find that useful too?

Tobi

--
http://www.pinkjuice.com/



Tue, 09 Nov 2004 20:30:19 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:
> Then I might think about an RCR for a feature which saves me from
> writing stuff like

> [ http://ruby-talk.com/9739 ]

I'd vote for it. I currently require the calls as in
http://ruby-talk.com/10006 for every script I write. BTW, the post says if
Ruby is rebuilt with MSVC, these calls aren't required, but I've not found
that to be the case with pragprog 1.6.6-0.

Chris



Tue, 09 Nov 2004 20:35:33 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

> I'd like to have a way more platform independent system call mechanism,
> in Ruby itself or in the std lib, which returns either nil, an Error
> object of some sort, or a Stdout object of some kind, so I can simply
> write something like

Check IO.popen (builtin), and open3.rb (standard library). They both do
what you want, with IO.popen providin stdin & stdout to a separate process,
and open3.rb providing stderr as well.

 -- Nikodemus



Tue, 09 Nov 2004 20:46:00 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

> Check IO.popen (builtin), and open3.rb (standard library). They both do
> what you want, with IO.popen providin stdin & stdout to a separate process,
> and open3.rb providing stderr as well.

Thanks! Two questions:

1.

How would I write

commands = Commands.new ['this -x -y','that -a -b']
commands.each do |cmd|
   returned = cmd.issue # or ".run", ".enter", ...
   case returned
     when Stderr then raise returned
     when Stdout then puts  returned
   end
end

then?

2. Will it run unchanged on Win and Lin, etc?

TIA,

Tobi

--
http://www.pinkjuice.com/



Tue, 09 Nov 2004 20:56:23 GMT  
 OS independent scripts, system calls on Windows and Linux
Hi,

At Fri, 24 May 2002 21:26:54 +0900,

Quote:

> Then I might think about an RCR for a feature which saves me from
> writing stuff like

> [ http://ruby-talk.com/9739 ]
> ...
>   if ENV.has_key?("OS") && ENV["OS"] =~ /nt/i
>                 puts "It's either NT or 2K, use 'cmd.exe /c '" if $DEBUG
>                 #it's either NT or Win2K
>                 command_shell = "cmd.exe /c "
> ...

> What do people think?

Do you want to run internal commands of cmd.exe?  It's
definitely system dependent.  For external commands,
mingw32/mswin32 ruby runs a child process directly instead.

Quote:
> I'd like to have a way more platform independent system call
> mechanism, in Ruby itself or in the std lib, which returns either nil,
> an Error object of some sort, or a Stdout object of some kind, so I
> can simply write something like

> commands = Commands.new ['this -x -y','that -a -b']
> commands.each do |cmd|
>    returned = cmd.issue # or ".run", ".enter", ...
>    case returned
>      when Stderr then raise returned
>      when Stdout then puts  returned
>    end
> end

I'm not sure what you mean by Stderr and Stdout.  Something
other than streams?

--
Nobu Nakada



Tue, 09 Nov 2004 21:06:47 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

> commands = Commands.new ['this -x -y','that -a -b']
> commands.each do |cmd|
>    returned = cmd.issue # or ".run", ".enter", ...
>    case returned
>      when Stderr then raise returned
>      when Stdout then puts  returned
>    end
> end

Something like:

require 'open3'

commands.each do |cmd|
   Open3.popen3(cmd) do |in,out,err|
        out = out.readlines
        err = err.readlines
        puts out unless out.empty?
        raise err unless err.empty?
   end
end

Completely untested, though.

Quote:
> 2. Will it run unchanged on Win and Lin, etc?

No idea whatsoever. ;) Try and let us know.

 -- Nikodemus



Tue, 09 Nov 2004 21:06:48 GMT  
 OS independent scripts, system calls on Windows and Linux
Hi,

At Fri, 24 May 2002 21:51:48 +0900,

Quote:

> 2. Will it run unchanged on Win and Lin, etc?

Open3.popen3 doesn't work on mingw32/mswin32.

--
Nobu Nakada



Tue, 09 Nov 2004 21:12:01 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

> Do you want to run internal commands of cmd.exe?  It's
> definitely system dependent.  For external commands,
> mingw32/mswin32 ruby runs a child process directly instead.

I think you don't see my task or my problem; let me try to explain on a
higher level.

I'm using the PragProg installer on Win, and the ruby-talk .tar.gz on Lin.

I need to use some tools for some files, and issue commands like

dot -Tsvg -o links.svg links.dot
java com.icl.saxon.StyleSheet -o links.dot svg-links.xml links2dot.xslt
domcount -n svg-links.xml
etc...

I run those from the commandline ("DOS prompt" on Windows). Some are
long, and I have around seven for that little project, so I put them in
.bat files; but because the same thing will have to run on other
Windowses, Linux, and perhaps many more platforms like Mac OS X, I
choose Ruby as portable scripting language. People will have to be able
run the same script without editing it.

I need a very simple way to issue the list of commands, in a very simple
and highly platform independent way.

 > I'm not sure what you mean by Stderr and Stdout.

I need to get the stdoutput, and the strerr output. Xerces' domcount for
example says something like "12ms, 25 elements" on success (that's what
should bet printed, befor the next command is issued), and spits out
error messages if the file was invalid XML (that's what should get
printed befor everything stops because of an error that one command/tool
returned.).

So, like in my pseudo code example, I want to get each command executed,
but see each one's output on the console, and possibly have the whole
shebang come to a halt with an error massage if one tool raised one.

Tobi

--
http://www.pinkjuice.com/



Tue, 09 Nov 2004 21:22:26 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

> commands.each do |cmd|
>    Open3.popen3(cmd) do |in,out,err|
>            out = out.readlines
>            err = err.readlines
>            puts out unless out.empty?
>            raise err unless err.empty?
>    end
> end

Two fixes and it even works:

sub(/in/, "stdin")                   # reserved word, silly me
sub(/err unless/, "err.join unless") # raise won't take an array

 -- Nikodemus



Tue, 09 Nov 2004 21:27:39 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

> Open3.popen3 doesn't work on mingw32/mswin32.

Thanks for the info.

All these incompatibility issues are why I want to talk about an RCR.

If there are workarounds reqired to get platform independent scripts,
like OS- / version- / type of compilation -  sniffing, then I think it
would be awesome to not require the Ruby user to do that each time, but
put it into the stdlib or Ruby itself.

I watn a system command mechanism that's *very* OS/build/version/...
independent, and is *very* safe, stable, simple, quick&easy, yet
flexible to use.

And yes, I can imagine that there are a lot of annoyances in OSes
(...like M$Windows) etc. when trying to implement that :)

Tobi

--
http://www.pinkjuice.com/



Tue, 09 Nov 2004 21:32:50 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:

>>commands = Commands.new ['this -x -y','that -a -b']
>>commands.each do |cmd|
>>   returned = cmd.issue # or ".run", ".enter", ...
>>   case returned
>>     when Stderr then raise returned
>>     when Stdout then puts  returned
>>   end
>>end

> I'm not sure what you mean by Stderr and Stdout.  Something
> other than streams?

returned = cmd.issue

returned would hold an object of class Stderr (or equiv) if there's an
error output from the command, an object of class Stdout (or equiv) if
there's anything from the command's stdout, or it would hold nil if
there's none of those.
The pseudo code was merely for demonstration, so I chose a way I found
descriptive.

Tobi

--
http://www.pinkjuice.com/



Tue, 09 Nov 2004 21:43:21 GMT  
 OS independent scripts, system calls on Windows and Linux

Quote:
> All these incompatibility issues are why I want to talk about an RCR.

Seems maybe this is just a bug report:

  On mswin32 1.6.6, the system call does not work with shell commands.

Here's the evidence. cl/util/win is a unit of mine that contains the methods
listed in http://ruby-talk.com/10006. Without it, system call does not work
on 'dir', but the backticks work. Requiring it fixes the system call for
'dir'.

For non cmd.exe calls, system and `` work fine as-is.

C:\temp\rcr>copy con test.bat
echo Howdy
^Z
        1 file(s) copied.

C:\temp\rcr>irb

irb(main):006:0> VERSION
"1.6.6"
irb(main):007:0> RUBY_PLATFORM
"i586-mswin32"

irb(main):001:0> `test.bat`
"\nC:\\temp\\rcr>echo Howdy \nHowdy\n"
irb(main):002:0> `dir`
" Volume in drive C is MS-DOS_6\n Volume Serial Number is BCBF-0F2A\n\n
Director
y of C:\\temp\\rcr\n\n05/24/2002  08:36a      <DIR>          .\n05/24/2002
08:3
6a      <DIR>          ..\n05/24/2002  08:36a                  12 test.bat\n
           1 File(s)             12 bytes\n               2 Dir(s)
123,353,0
88 bytes free\n"
irb(main):003:0> system('dir')
false
irb(main):004:0> system('test.bat')

C:\temp\rcr>echo Howdy
Howdy
true
irb(main):008:0> require 'cl/util/win'
true
irb(main):009:0> system('dir')
 Volume in drive C is MS-DOS_6
 Volume Serial Number is BCBF-0F2A

 Directory of C:\temp\rcr

05/24/2002  08:36a      <DIR>          .
05/24/2002  08:36a      <DIR>          ..
05/24/2002  08:36a                  12 test.bat
               1 File(s)             12 bytes
               2 Dir(s)     123,353,088 bytes free
0

Chris



Tue, 09 Nov 2004 21:43:23 GMT  
 
 [ 48 post ]  Go to page: [1] [2] [3] [4]

 Relevant Pages 

1. how to run script in windows via C system call w/o popup dos shell

2. Using os.system and os.popen from pythonwin under Windows NT

3. accessing calling scripts from called scripts at run time

4. a Perl script's interaction with Ruby's system call

5. ada making linux script calls

6. Linux OS-call bind to Ada package spec

7. Linux OS/Interrupts and BIOS calls

8. Multiple os.system calls

9. Window NT: need to allow client to make system call via CGI script

10. strange problem with os.system() call

11. grabbing return codes from os.system() call

12. problem with os.system calls under win32

 

 
Powered by phpBB® Forum Software