2

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.

Rohan Zakie
  • 319
  • 5
  • 14
  • what do you mean by `front end in PHP`? PHP is a server side scripting language. – bansi Oct 08 '13 at 08: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 08 '13 at 09: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 08 '13 at 09:39

3 Answers3

1

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.

vp_arth
  • 14,461
  • 4
  • 37
  • 66
  • i guess this can work, need littel more detail if you can please – Rohan Zakie Oct 08 '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 08 '13 at 11:01
1

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.

nietonfir
  • 4,797
  • 6
  • 31
  • 43
1

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");

?>
  • 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 08 '13 at 09: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 08 '13 at 09: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 08 '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 08 '13 at 10:29
  • i want to round the problem sort of like this http://stackoverflow.com/questions/13903506/pass-php-variables-to-a-bash-script-and-then-launch-it – Rohan Zakie Oct 08 '13 at 11:05
  • also this kinda thing http://askubuntu.com/questions/340428/running-a-php-script-in-apache-webserver – Rohan Zakie Oct 08 '13 at 11:32