Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I want to know how PHP can run bash scripts.

I mean, I want to pass some variables from a PHP script to a bash script which will run when the submit button is clicked.

Also how can I take output from terminal to php page?

The purpose of this question is to have understanding of making front end in PHP and back end with bash, I want to run command of terminal from PHP page by giving some variables.

Please put me in right direction, I haven't slept for 4 days now, just reading stuff for it, so far I am confused.

share|improve this question

marked as duplicate by Zuul, devnull, fedorqui, Lorenzo Donati, Radu Murzea Oct 8 '13 at 11:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
what do you mean by front end in PHP? PHP is a server side scripting language. –  bansi Oct 8 '13 at 8:34
    
i mean i have to setup 2 application every time when restarting system,i have to give these two apps on terminal some arguments like #dvblast -f xxxxx -s xxxxx -c /path to config file i want to make some php page where i can just input -f -s -c values and submit, then php will run this command on terminal, and the process will start. let me know if you need more detail, its a IPTV headend kindna thing –  Rohan Zakie Oct 8 '13 at 9:14
    
@bansi It doesn't stop many websites from being written in PHP, you know. Rohan just wants something else to do the processing? –  icedwater Oct 8 '13 at 9:39

3 Answers 3

up vote 1 down vote accepted

Running shell script from php code can be done as follows:
shell_exec("script_path -with arguments");
The command shell_exec returning whatever the script prints out.
here is an example:

<?php

$dir_listing=shell_exec("/bin/ls -1 /path");

?>
share|improve this answer
    
thanks michael , but i am in the middle of learning phase , can you explain this argument shell_exec("/bin/ls -1 /path"); i want to know about -1 /path what is it for, -1 is for depth i guess but what is the path too? i really appreciate your help man –  Rohan Zakie Oct 8 '13 at 9:19
    
You might notice that -1 is after /bin/ls - it is an argument to that program. ls --help|less will be useful here. As for /path Michael is just using it as a placeholder for your actual path... –  icedwater Oct 8 '13 at 9:43
    
is there a way i can pass variable like $var1 for -f and $var2 for -s i know may be its simple like that but i just divid into the opensource world and also want to contribute like you guys. –  Rohan Zakie Oct 8 '13 at 10:26
1  
-f fff is only string. you can manipulate it in PHP as a string before exec it... see my answer update also –  vp_arth Oct 8 '13 at 10:29
    
i want to round the problem sort of like this stackoverflow.com/questions/13903506/… –  Rohan Zakie Oct 8 '13 at 11:05

You might want to take a look at the Symfony2 Console Component. It is designed to facilitate the execution of shell commands and returning their output.

share|improve this answer

Try this:

$file = "/etc/hosts";
echo `cat $file`;

Added: If you want to use it like this:

  $f = $_POST['f'];
  $s = $_POST['s'];
  $path = $basepath.'/'.$_POST['path'];
  $res = `dvblast -f $f -s $s -c $path`;

be sure that your input is sanitized. May be shell injection here.

share|improve this answer
    
i guess this can work, need littel more detail if you can please –  Rohan Zakie Oct 8 '13 at 10:36
    
things are coming together now, once i get some hook over the problem , then i am good to go and can develop things. but i need one more favor from you man, this part can you just make some demo script example here that do this , this way i will grab the starting point immediately . an example php side , where a form takes a path of directory, and when hit submit it show the ls -al of that directory, i just need to learn a very basic example to of php taking values in form and submit button to execute bash script by sending those values to bash or,even php running the command from form values –  Rohan Zakie Oct 8 '13 at 11:01
    
accept/unaccept... wrong answer? –  vp_arth Oct 11 '13 at 6:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.