Skip to content

Instantly share code, notes, and snippets.

@MikeRuby
Forked from JosephPecoraro/shell-execution.rb
Created June 26, 2014 18:28
Show Gist options
  • Select an option

  • Save MikeRuby/1892b66442e6153b3a45 to your computer and use it in GitHub Desktop.

Select an option

Save MikeRuby/1892b66442e6153b3a45 to your computer and use it in GitHub Desktop.

Revisions

  1. @JosephPecoraro JosephPecoraro revised this gist Mar 1, 2011. 1 changed file with 58 additions and 58 deletions.
    116 changes: 58 additions & 58 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,58 +1,58 @@
    # Ways to execute a shell script in Ruby
    # Example Script - Joseph Pecoraro

    cmd = "echo 'hi'" # Sample string that can be used

    # 1. Kernel#` - commonly called backticks - `cmd`
    # This is like many other languages, including bash, PHP, and Perl
    # Returns the result of the shell command
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111

    value = `echo 'hi'` # or uglier but valid => Kernel.`("echo 'hi'")
    value = `#{cmd}` # or uglier but valid => Kernel.`("#{cmd}")


    # 2. Built-in syntax, %x( cmd )
    # Following the ``x'' character is a delimiter, which can be any character.
    # If the delimiter is one of the characters ``('', ``['', ``{'', or ``<'',
    # the literal consists of the characters up to the matching closing delimiter,
    # taking account of nested delimiter pairs. For all other delimiters, the
    # literal comprises the characters up to the next occurrence of the
    # delimiter character. String interpolation #{ ... } is allowed.
    # Returns the result of the shell command, just like the backticks
    # Docs: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html

    value = %x( echo 'hi' )
    value = %x[ #{cmd} ]


    # 3. Kernel#system
    # Executes the given command in a subshell
    # Return: true if the command was found and ran successfully, false otherwise
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M002992

    wasGood = system( "echo 'hi'" )
    wasGood = system( cmd )


    # 4. Kernel#exec
    # Replaces the current process by running the given external command.
    # Return: none, the current process is replaced and never continues
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M002992

    exec( "echo 'hi'" )
    exec( cmd ) # Note: this will never be reached beacuse of the line above


    # Extra Advice
    # $? which is the same as $CHILD_STATUS
    # Accesses the status of the last system executed command if
    # you use the backticks, system() or %x{}.
    # You can then access the ``exitstatus'' and ``pid'' properties

    $?.exitstatus

    # More Reading
    # http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands
    # http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html
    # Ways to execute a shell script in Ruby
    # Example Script - Joseph Pecoraro

    cmd = "echo 'hi'" # Sample string that can be used

    # 1. Kernel#` - commonly called backticks - `cmd`
    # This is like many other languages, including bash, PHP, and Perl
    # Returns the result of the shell command
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111

    value = `echo 'hi'` # or uglier but valid => Kernel.`("echo 'hi'")
    value = `#{cmd}` # or uglier but valid => Kernel.`("#{cmd}")


    # 2. Built-in syntax, %x( cmd )
    # Following the ``x'' character is a delimiter, which can be any character.
    # If the delimiter is one of the characters ``('', ``['', ``{'', or ``<'',
    # the literal consists of the characters up to the matching closing delimiter,
    # taking account of nested delimiter pairs. For all other delimiters, the
    # literal comprises the characters up to the next occurrence of the
    # delimiter character. String interpolation #{ ... } is allowed.
    # Returns the result of the shell command, just like the backticks
    # Docs: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html

    value = %x( echo 'hi' )
    value = %x[ #{cmd} ]


    # 3. Kernel#system
    # Executes the given command in a subshell
    # Return: true if the command was found and ran successfully, false otherwise
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M002992

    wasGood = system( "echo 'hi'" )
    wasGood = system( cmd )


    # 4. Kernel#exec
    # Replaces the current process by running the given external command.
    # Return: none, the current process is replaced and never continues
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M002992

    exec( "echo 'hi'" )
    exec( cmd ) # Note: this will never be reached beacuse of the line above


    # Extra Advice
    # $? which is the same as $CHILD_STATUS (if you require 'english')
    # Accesses the status of the last system executed command if
    # you use the backticks, system() or %x{}.
    # You can then access the ``exitstatus'' and ``pid'' properties

    $?.exitstatus

    # More Reading
    # http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands
    # http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html

  2. @JosephPecoraro JosephPecoraro renamed this gist Dec 6, 2008. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @JosephPecoraro JosephPecoraro revised this gist Aug 5, 2008. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rbx
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ exec( cmd ) # Note: this will never be reached beacuse of the line above
    # Extra Advice
    # $? which is the same as $CHILD_STATUS
    # Accesses the status of the last system executed command if
    # you use the backticks, system() or %{}.
    # you use the backticks, system() or %x{}.
    # You can then access the ``exitstatus'' and ``pid'' properties

    $?.exitstatus
  4. @JosephPecoraro JosephPecoraro revised this gist Aug 5, 2008. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions gistfile1.rbx
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Ways to execute a shell script
    # Ways to execute a shell script in Ruby
    # Example Script - Joseph Pecoraro

    cmd = "echo 'hi'" # Sample string that can be used

    @@ -7,8 +8,8 @@ cmd = "echo 'hi'" # Sample string that can be used
    # Returns the result of the shell command
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111

    value = `echo 'hi'`
    value = `#{cmd}`
    value = `echo 'hi'` # or uglier but valid => Kernel.`("echo 'hi'")
    value = `#{cmd}` # or uglier but valid => Kernel.`("#{cmd}")


    # 2. Built-in syntax, %x( cmd )
    @@ -26,7 +27,7 @@ value = %x[ #{cmd} ]


    # 3. Kernel#system
    # Executes the given command in a subshell,
    # Executes the given command in a subshell
    # Return: true if the command was found and ran successfully, false otherwise
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M002992

  5. @JosephPecoraro JosephPecoraro created this gist Aug 5, 2008.
    57 changes: 57 additions & 0 deletions gistfile1.rbx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    # Ways to execute a shell script

    cmd = "echo 'hi'" # Sample string that can be used

    # 1. Kernel#` - commonly called backticks - `cmd`
    # This is like many other languages, including bash, PHP, and Perl
    # Returns the result of the shell command
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111

    value = `echo 'hi'`
    value = `#{cmd}`


    # 2. Built-in syntax, %x( cmd )
    # Following the ``x'' character is a delimiter, which can be any character.
    # If the delimiter is one of the characters ``('', ``['', ``{'', or ``<'',
    # the literal consists of the characters up to the matching closing delimiter,
    # taking account of nested delimiter pairs. For all other delimiters, the
    # literal comprises the characters up to the next occurrence of the
    # delimiter character. String interpolation #{ ... } is allowed.
    # Returns the result of the shell command, just like the backticks
    # Docs: http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html

    value = %x( echo 'hi' )
    value = %x[ #{cmd} ]


    # 3. Kernel#system
    # Executes the given command in a subshell,
    # Return: true if the command was found and ran successfully, false otherwise
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M002992

    wasGood = system( "echo 'hi'" )
    wasGood = system( cmd )


    # 4. Kernel#exec
    # Replaces the current process by running the given external command.
    # Return: none, the current process is replaced and never continues
    # Docs: http://ruby-doc.org/core/classes/Kernel.html#M002992

    exec( "echo 'hi'" )
    exec( cmd ) # Note: this will never be reached beacuse of the line above


    # Extra Advice
    # $? which is the same as $CHILD_STATUS
    # Accesses the status of the last system executed command if
    # you use the backticks, system() or %{}.
    # You can then access the ``exitstatus'' and ``pid'' properties

    $?.exitstatus

    # More Reading
    # http://www.elctech.com/blog/i-m-in-ur-commandline-executin-ma-commands
    # http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html