Automate shell commands with a simple Rakefile

Below are three examples of how to automate Bash commands with Rake.

Installation and set-up

  1. Make sure you have Ruby installed.
  2. Install Rake:
    gem install rake
    
  3. Create a .rake folder in your home directory to store your global Rakefiles, which you can then execute from any folder with the -g option:
    mkdir ~/.rake
    
  4. Create a new file hello.rake in the .rake folder and add the following lines to it:
    task :hello do
                puts "Hello world!"
            end
    
  5. Execute the task to check that it prints "Hello world!" to the terminal:
    rake -g hello
    

Rakefile 1:

The following Rakefile creates a folder and then opens a new file in the Atom text editor within that folder.

  1. Create a new file folder_file_editor.rake in the ~/.rake folder:
    touch ~/.rake/folder_file_editor.rake
    
  2. Add the following to the file:
    task :folder_file_editor, [:dir, :name] do |t, args|
              args.with_defaults(:dir =>'newFolder')  
              args.with_defaults(:name =>'newFile')  
              createM=`mkdir #{args.dir} && cd #{args.dir} && touch #{args.name} && atom #{args.name}`
          end
    
  3. Check it works:
    rake -g folder_file_editor[myFolder,myFile]
    
    This command creates the folder myFolder and opens the file myFile within the Atom editor. If you do not specify parameters for the names of the folder and the file, the command will create the default newFolder folder and newFile file.
    Caution: Do not leave empty spaces between myFile and myFolder in "[myFolder,myFile]" when typing the command.
  4. Append the following useful function to your .bashrc file in your home directory:
    f() {
      rake -g create_folder_file_editor["$1","$2"]
           }
    
  5. Save the file and execute the source command:
    source .bashrc
    
  6. Now you can enter the following into a terminal to execute the Rake task:
    f myFolder myFile
    

Rakefile 2:

The following Rakefile automates the process for generating a CV in HTML, PDF and DOCX from an Asciidoc file, as described in this blog post.

  1. Create a new file generate_CV.rake in your ~/.rake folder:
    touch ~/.rake/generate_CV.rake
    
  2. Add the following to the file:
    task :generate_CV, [:file] do |t, args|
      generate=  
           `asciidoctor -a nofooter -a stylesheet=yourCV.css #{args.file}.adoc;
            wkhtmltopdf #{args.file}.html #{args.file}.pdf;
            pandoc -o yourCV.docx --reference-docx=refCV.docx yourCV.html`
      puts generate
    end
    
  3. Check it works:
    rake -g generate_CV[yourCV]
    
    This command creates the yourCV.html, yourCV.pdf and yourCV.docx files, provided you have yourCV.adoc and yourCV.css saved in the same directory.
  4. Append the following useful function to your .bashrc file in your home directory:
    cv() {
      rake -g generate_CV["$1"]
            }
    
  5. Save the file and execute the source command:
    source .bashrc
    
  6. Now you can enter the following into a terminal to execute the Rake task:
    cv yourCV
    

Rakefile 3:

The following Rakefile automates the tasks for pushing Jekyll files to GitHub described in this blog post.

  1. Create a new file git_commit.rake in your ~/.rake folder:
    touch ~/.rake/git_commit.rake
    
  2. Add the following to the file:
    task :git_commit, [:message] do |t, args|
          args.with_defaults(:message =>'Blog update')
            commit=
                `bundle exec jekyll build
                  git add .
                  git commit -m "#{args.message}"
                  git push origin gh-pages`
              puts commit
      end
    
  3. Check it works:
    rake -g git_commit['my commit message']
    
    This command prints the requests for you to enter your GitHub account and password to the terminal. To avoid having to enter your username and password each time, see this GitHub Help article on connecting to GitHub via SSH.
  4. Append the following useful function to your .bashrc file in your home directory:
    blogit() {
      rake -g git_commit["$1"]
                }
    
  5. Save the file and execute the source command:
    source .bashrc
    
  6. Now you can push to GitHub with the following command:
    blogit "my commit message"