# External commands in a controller

**URL:** <https://www.ruby-forum.com/t/external-commands-in-a-controller/192480>\
**Category:** Rails\
**Created:** [July 30, 2010, 11:17pm UTC](https://www.ruby-forum.com/t/external-commands-in-a-controller/192480 "2010-07-30T23:17:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [July 30, 2010, 11:17pm UTC](https://www.ruby-forum.com/t/external-commands-in-a-controller/192480/1 "2010-07-30T23:17:12Z")

</div>

I have various files of extensions .cpp stored on the server. When  
user clicks on the compile button, I should run gcc command passing  
the absolute path of the file to gcc compiler and then show the user  
the output of the file.  
How can i do that???

---

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [July 30, 2010, 11:29pm UTC](https://www.ruby-forum.com/t/external-commands-in-a-controller/192480/2 "2010-07-30T23:29:51Z")

</div>

Jatin K. wrote:

> I have various files of extensions .cpp stored on the server. When  
> user clicks on the compile button, I should run gcc command passing  
> the absolute path of the file to gcc compiler and then show the user  
> the output of the file.  
> How can i do that???

A silly contrived example:

## user\_controller.rb

# GET /users

# GET /users.xml

def index  
@users = User.all  
@out = `ls -l /Users/robertwalker/Desktop`

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @users }
end

```

end

## index.html.erb

List of files on Desktop: \<%= simple\_format(@out) %\>

---

<div class="post-metadata">

**Author:** ![silver-spoon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aeb1de/32.png) [@silver-spoon](https://www.ruby-forum.com/u/silver-spoon)\
**Post date:** [July 31, 2010, 8:54am UTC](https://www.ruby-forum.com/t/external-commands-in-a-controller/192480/3 "2010-07-31T08:54:50Z")

</div>

The above dummy code will only list the files in the directory, which  
is something i don’t really want. I want to individually run all these  
files with gcc compiler and then show the user the output/ errors in  
the process to the user, in front of each file. Now can you help me???
