0

I was asked to contribute to a simple web app built using Smashing (a Sinatra-based framework). I have a little experience with rails, but I am not familiar with this framework (or Sinatra) nor I am sure why it was chosen. I understand it is useful to build dashboards and believe it was chosen because this kind of front-end meets the client's requirements. So far I even struggle to understand where the controller is.

What I have been failing to do all day is something that at least sounds very easy: run a bash script on the server side when a button on the web page is clicked and display a message like "job done" (or "error" if something goes wrong).

So I have a typical button like:

<button id='script_btn' type='button' class='btn btn-danger'>run script</button>

And somewhere in my project folder a file script.sh.

How do I make the button click execute "script.sh" and return a message that the script run successfully (or that errors occurred)?

I keep searching but I seem to find info about this mostly for PHP, Node.js, and maybe something for rails but nothing really in my case. Any help would be much appreciated.

(There is no tag smashing so I'll use dashing instead it seems to be its predecessor)

Tommy
  • 628
  • 11
  • 22

1 Answers1

1

Let's see if this helps, I have used Slim as templating engine. Please let us know if this works

(bash_sinatra.rb)

require 'sinatra' 
require 'slim'

get '/' do 
  slim :index 
end

get '/run_bash' do
  # assuming the file is in root of the app or else we can give the path to bash file.
  @script_result = `bash script.sh` # make sure you have an if condition in script which returns a string stating the status of script.
  slim :run_bash 
end

__END__

@@layout 
doctype html 
html
  head 
    meta charset="utf-8" 
    title Run Bash 
    link rel="stylesheet" media="screen, projection" href="/styles.css" 

  body 
    h1 Tommy's Sinatra App
    == yield 

@@index 
h2 Run bash Script
a href="/run_bash" title='run bash script' class='btn btn-danger' id='script_btn' Run Bash Script

@@run_bash
h2 class='some_class'
  = @script_result

Just for the sake of example here is a one liner bash script

#!/bin/bash

echo "Script was successfully executed."

run the sinatra app on terminal ruby bash_sinatra.rb and the sinatra server will start

Sumeet Masih
  • 597
  • 1
  • 8
  • 22
  • this is new knowledge to me, thanks, sorry to follow this conversation, when you put slim :run_bash, is it command to run_bash or is it call class method that you wrote @@run_bash – widjajayd Jun 26 '17 at 12:53
  • @script_result = `\`somewhere_in_system/script.sh`\` is command to run bash script, just cage file path in back quote. `slim :run_bash` is used to render in page template. – Sumeet Masih Jun 26 '17 at 14:00
  • Thanks for the reply. Forgive the silly question but is all this code in a single file? The top part seem to be like a controller.. my problem with this smashing framework is that so far I can't even understand where the controller is. :/ – Tommy Jun 27 '17 at 00:18
  • yes top one act as controller and all the code is one page, you can separate the views by creating a `views` folder in the root of the app. You can also use `erb` instead of slim. – Sumeet Masih Jun 27 '17 at 06:47