Ruby Programming Tutorial - Lesson 39 - Working with Shell Commands in ruby
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 :)
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..
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.
And here are the dummy modules, which this ruby program has created for us
Leave Ruby Programming Tutorial - Lesson 39 - Working with Shell Commands in ruby to:
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
- Roar!! is what bearshares' new frontend called
- Join bearshares.com and get 50 SP signup bonus, + invite your friends and earn 50 BEARS for each of them
- Bearjobs (jobs.bearshares.com)
- Bearshares now has BearWallet, Advertisement Network and Hyip Investment Program
- bearshares.com reached 1100 members :)
- Inviting everyone to bearshares.com ==> rebirth of paying social network
- Bearshares launched its own explorer
- https://bearshares.com giving 50 SP sign up bonus
- steem 0.20.3 first problem !
- Installed Condenser on https://beryxchange.com
- Ruby Programming for Beginners ==> Part 11 Video
- Ruby Programming for Beginners ==> Part 10 Video
- Ruby Programming for Beginners ==> Part 9 Video
- Ruby Programming for Beginners ==> Part 8 Video
- Ruby Programming for Beginners ==> Part 7 Video
- Ruby Programming for Beginners ==> Part 6 Video
- Ruby Programming for Beginners ==> Part 5 Video
- Ruby Programming for Beginners ==> Part 4 Video
- Ruby Programming for Beginners ==> Part 3 Video
- Ruby Programming for Beginners ==> Part 2 Video