LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-10-2019, 01:46 AM   #1
JugaruGabi
LQ Newbie
 
Registered: Jun 2019
Posts: 15

Rep: Reputation: Disabled
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?
 
Old 07-10-2019, 03:29 AM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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’)
 
Old 07-10-2019, 03:42 AM   #3
JugaruGabi
LQ Newbie
 
Registered: Jun 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-10-2019, 03:47 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by JugaruGabi View Post
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

Last edited by scasey; 07-10-2019 at 03:56 AM.
 
Old 07-10-2019, 04:11 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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.

Last edited by michaelk; 07-10-2019 at 04:13 AM.
 
Old 07-10-2019, 05:24 AM   #6
JugaruGabi
LQ Newbie
 
Registered: Jun 2019
Posts: 15

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assigning a value to a variable also creates an empty file named as the value in bash StorageDon Linux - Newbie 3 11-01-2016 08:52 PM
strange value assignments variable = value, value?? ostrow30 Programming 2 07-24-2011 07:59 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
Passing a value from a shell script to a php file kushalkoolwal Programming 4 07-07-2010 06:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration