For Programmers: Free Programming Magazines  


Home > Archive > Ruby > August 2005 > Re: shell command with envvars - howto?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: shell command with envvars - howto?
Reyn Vlietstra

2005-08-30, 7:58 am

Hi,
We havent implemented && or exit status yet(shouldnt be too hard using
open4), but have a look at ruSH.

You can pipe commands to each other.

http://reyn.co.za/rushtest2.png
http://rubyforge.org/projects/rush/


On 8/30/05, Wybo Dekker <wybo@servalys.nl> wrote:
>
> I want to make a system calling method that returns output, success rate,
> and exit status of a system command, like the sys method in the example
> program below. My system call fails, however, when I put a definition of
> an
> environment variable in front of the system command.
>
> It works only if I put &&'s between the envvar definition(s) and the
> command, as illustrated in the program. But that means that I'll have to
> interpret the argument of my method to insert those &&'s.
>
> Is there a better way?
>
> #!/usr/bin/env ruby
>
> # return output, success rate, and exit status of a system command
> def sys(command)
> [%x{#{command} 2>&1}, $?.success?, $?.exitstatus]
> end
>
> def pr(command,output,success,status)
> puts "command: #{command}"
> puts " output: #{output}"
> puts "success: #{success ? 'succeeded' : 'failed'}"
> puts " status: #{status}",''
> end
>
> command = "echo hello"; pr(command,*sys(command))
> command = "xxxx hello"; pr(command,*sys(command))
> command = "x=hello echo $x"; pr(command,*sys(command)) # FAILS!
> command = "x=hello && echo $x"; pr(command,*sys(command))
>
> Outputs:
>
> command: echo hello
> output: hello
> success: succeeded
> status: 0
>
> command: xxxx hello
> output: sh: xxxx: command not found
> success: failed
> status: 127
>
> command: x=hello echo $x
> output:
> success: succeeded
> status: 0
>
> command: x=hello && echo $x
> output: hello
> success: succeeded
> status: 0
>
>
>
> --
> Wybo
>
>



--
Reyn Vlietstra

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com