Bilal Haider Qureshi avatar

Ruby Programming Tutorial - Lesson 39 - Working with Shell Commands in ruby

bilal-haider

Published: 26 Mar 2018 › Updated: 26 Mar 2018Ruby Programming Tutorial - Lesson 39 - Working with Shell Commands in ruby

Ruby Programming Tutorial - Lesson 39 - Working with Shell Commands in ruby

plush-linux-penguin.jpg

Common Shell Commands in Ruby

For people who use linux a lot like myself they know very well,about how useful shell commands are, they can help us, to do a lot of amazing things in a short amount of time.

Few basic shell commands which every linux user performs are

  • cd (Change Directory)

    • Which is used to change directory, cd home to change your working directory to home
    • cd .. to go back one step in your directory tree
  • mkdir 'data' (make directory called data)

    • Which is used to create folders or directories
  • pwd (print working directory)

    • Which is used to get the current working directory

These few shell commands can help ruby programmers do some amazing things while writing ruby programs, when using them combined with Filing in ruby, we can create whole project structures in seconds, which can take several minutes when creating them manually.

Ruby helps execute few basic shell commands, via its Shell Class

lets see how can we execute them in our Ruby programs and create whole project structures :)

Screenshot from 2018-03-27 01-29-33.png

require 'shell'

_myshell = Shell.new()
## Current working directory
puts _myshell.pwd
## Current working directory
puts _myshell.dir

## Create a new directory called data
_myshell.mkdir 'data'

## Change directory
_myshell.cd 'data'

## Current working Directory
puts _myshell.pwd

## Go one step back in Directories
_myshell.cd '..'
## Check to see if directory exists
puts _myshell.exists?('data')

Ruby Project Structure

These few simple shell commands can help us create amazing things, lets take a look at this Ruby gem file structure. When you want to create a ruby gem, which you want people to make use of, you need to create it in a way that it follows this file structure, where you have one directory for putting your binary files, one directory for library files so on and so forth.

It can be very cumbersome to create such a structure manually, so we create ruby program, using in it Shell commands and Filing altogether to create such a project structure for us.

We can fill these files with some boiler plate code as well..
Screenshot from 2018-03-26 23-31-05.png

Take a look at this code down below, where we are creating a project structure for a dummy project .. you are not limited to a project structure which other people are using, you can be as creative as you can with your project file's structures .. I am just giving you some examples here, from which you can take inspiration.

require 'shell'

sh = Shell.new()
sh.mkdir "src" unless sh.exists?("src")
# make the 'src' directory if it doesn't already exist
sh.cd("src")
for dir in ["bin", "lib", "test"]
  if !sh.exists?(dir)
    sh.mkdir dir # make dir if it doesn't already exist
    sh.cd(dir) do
      # change to the `dir` directory
      f = sh.open("#{dir}.rb", "w") # open a new file in write mode
      f.print "## This is Test ruby program \n"            # write to the file
      f.print "module #{dir} \n"
      f.print "\t ## code goes here \n"
      f.print "end"
      f.close                     # close the file handler
    end
    print sh.pwd                  # output the process working directory
  end
end

Filing and Shell commands when combined can do amazing things :)

Notice that here, we are creating a folder called "src" and inside that folder we are creating three more folders, which are carrying one file each in them, which has a dummy module.

Screenshot from 2018-03-27 01-50-39.png

And here are the dummy modules, which this ruby program has created for us
Screenshot from 2018-03-27 01-54-26.png

Leave Ruby Programming Tutorial - Lesson 39 - Working with Shell Commands in ruby to:

Written by

I am a programmer :)

Read more #ruby posts


Best Posts From Bilal Haider Qureshi

We have not curated any of bilal-haider's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Bilal Haider Qureshi