BobZach
BobZach

Reputation: 1

How to insert form value into shell_exec

I have a script I am trying to execute in PHP. The script searches a txt file for whatever is queried. It then posts the results.

  <?php
   $output = shell_exec('/var/www/html/scripts/query.sh');
   echo $output;
   ?>
   <?php
   $output = shell_exec('/var/www/html/scripts/query.sh [email protected]');
   echo $output;
   ?>

What I want to do is have a form I or anyone can type in a query and it inputs it at the end of query.sh where [email protected] is. Can anyone help me accomplish this? I am new to PHP so try not to flame me to bad. Thank you for your help.

   #!/bin/bash
 dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

if [ "$1" != "" ]; then
  letter1=$(echo ${1,,}|cut -b1)
  if [[ $letter1 == [a-zA-Z0-9] ]]; then
      if [ -f "$dir/data/$letter1" ]; then
          grep -ai "^$1" "$dir/data/$letter1"
      else
          letter2=$(echo ${1,,}|cut -b2)
          if [[ $letter2 == [a-zA-Z0-9] ]]; then
              if [ -f "$dir/data/$letter1/$letter2" ]; then
                  grep -ai "^$1" "$dir/data/$letter1/$letter2"
              else
                  letter3=$(echo ${1,,}|cut -b3)
                  if [[ $letter3 == [a-zA-Z0-9] ]]; then
                      if [ -f "$dir/data/$letter1/$letter2/$letter3" ]; then
                          grep -ai "^$1" "$dir/data/$letter1/$letter2/$letter3"
                      fi
                  else
                      if [ -f "$dir/data/$letter1/$letter2/symbols" ];  then
                          grep -ai "^$1" "$dir/data/$letter1/$letter2 /symbols"
                      fi
                  fi
              fi
          else
              if [ -f "$dir/data/$letter1/symbols" ]; then
                  grep -ai "^$1" "$dir/data/$letter1/symbols"
              fi
          fi
      fi
  else
      if [ -f "$dir/data/symbols" ]; then
          grep -ai "^$1" "$dir/data/symbols"
      fi
  fi
  else
  echo "[*] Example: ./query [email protected]"
 fi

Upvotes: 0

Views: 421

Answers (1)

user8795381
user8795381

Reputation:

so your code should look pretty similar to this.

<?php
       if(isset($_POST['arg']){
       $your_input = $_POST['arg'];
       $output = shell_exec("/var/www/html/scripts/query.sh {$your_input}");
       echo $output;
       }
 ?>


<form action="" method="POST">
<input type="text" name="arg">
<input type="submit" value="Submit">
</form>

Try this, it should work fine.

Upvotes: 1

Related Questions