LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Passing value from HTML file to bash script (https://www.linuxquestions.org/questions/linux-general-1/passing-value-from-html-file-to-bash-script-4175657149/)

JugaruGabi 07-10-2019 01:46 AM

Passing value from HTML file to bash script
 
I am having the following requirement:

HTML file:
Code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Build report</title>
</head>
<!-- <link href="stylesheets/common.css" type="text/css" rel="stylesheet"> -->

<body>

<h1>Select date range</h1>

<form method="post" action="script.php">

Start Date DD-MMM-YYYY: <br />
<input type="text" name="s_date" size="35" />
<br />

End Date DD-MMM-YYYY: <br />
<input type="text" name="e_date" size="35" />
<br /> <br />

<input type="submit" value="Submit" />
<br />
</form>

</body>

script.php file:
Code:

<?php
$s_date = $_POST['s_date'];
$e_date = $_POST['e_date'];


if(empty($s_date ) || empty($e_date )) {
echo "<h2>You must fill in all fields</h2>\n" ;
die ("Click Back to start again.");
}
echo "<h2>You Entered the following information:</h2>";
echo "<b>Starting date:</b><br><br>";
echo $s_date;
echo "<br><br><b>Ending date:</b><br><br>";
echo $e_date;
?>

<?php
// exec ( "var/www/html/reports/bash.sh \"$s_date\" $e_date" );
$output = "<pre>".shell_exec("/var/html/www/reports/bash.sh $s_date $e_date")."</pre>";
echo $output;
?>

bash.sh file:
Code:

#!/bin/bash

#s_date=01-Jan-2019
#e_date=02-Jan-2019
read -p "Enter starting date for concat: " s_date
read -p "Enter ending date for concat: " e_date

if [ "$1" != "" ] ; then
  s_date=$1
fi
if [ "$2" != "" ] ; then
  e_date=$2
fi

fs=""
i=0
while true ; do
    date=$(date +'%d-%b-%Y' --date "$s_date +$i days")
    f="auto_test_$(date +'%Y-%b-%d' --date "$s_date +$i days").txt"
    ((i+=1))
    if [ -f $f ] ; then # Check the file exists... maybe some days are missing in this interval?
      fs="$fs $f"
    fi
    if [ "$date" == "$e_date" ] ; then
        break
    fi
done
cat $fs > "Period $s_date-$e_date"
echo "File "Period $s_date-$e_date" has been generated successfully!";
echo "See file below: "
echo ""
ls -lah | grep 'Period*'

So, the idea is the following:
the user must complete the starting date and ending date from the browser (html file), the dates are being pulled in the script.php which needs to run the bash.sh with the starting date and ending date as parameters.

If I am doing this from the web browser interface, nothing happens and the bash does not run.
But if I am running the bash.sh file on the server using:
Code:

./bash.sh 01-Jan-2019 02-Jan-2019
it works as expected.

What am I doing wrong?

scasey 07-10-2019 03:29 AM

When the web server is running things, references are relative to the DocumentRoot...so the path to the script is probably something like /reports/bash.sh or maybe just /bash.sh...hard to say without knowing the DocumentRoot.

(And naming a script bash.sh is kinda confusing...just sayin’)

JugaruGabi 07-10-2019 03:42 AM

1. DocumentRoot is /var/www/html/
I have put the all of the paths possible. Did not start the bash file.
I have even left it in the root of the webserver. Just to test.

2. Well, it's just for testing purposes. The bash file which would be used has a totally different name.

scasey 07-10-2019 03:47 AM

Quote:

Originally Posted by JugaruGabi (Post 6013698)
1. DocumentRoot is /var/www/html/
I have put the all of the paths possible. Did not start the bash file.
I have even left it in the root of the webserver. Just to test.

Then the php script should use /bash.sh if the script is in the root of the web server

BTW, looking at the web logs will probably show that the server can’t find the bash script...or maybe the php script. That’s the first place to look when troubleshooting a web server

michaelk 07-10-2019 04:11 AM

php has a date range function so there is no need for separate bash script. Assuming you are running apache by default it will not run scripts from document root although your calling it from php so that might not matter. Check the error logs.

JugaruGabi 07-10-2019 05:24 AM

Yep, main issue was with the path - for some reason the issue got fixed after I have moved the bash script to a different path and changed the path in script.php file to the full path to the bash script.
Thanks for the replies. :)

We can close the topic.


All times are GMT -5. The time now is 04:56 PM.