[SOLVED] Run bash script with HTML5 button - won't work
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Your script might be fine. The problem looks to be with the HTML. The button element needs to be part of a form element. The form element should then have an 'action' attribute containing the URL pointing to your PHP script. That URL can be a relative path of course.
Can you show more of the HTML document? In particular, please show the whole form element and its attributes.
Does a click on the button open the new URL?
If so I see 2 possible reasons for the failure:
1. if the shell script does not reside in the same directory as the calling php script you have call it with its entire path. Maybe it would be OK if you set the entire include path but I am not sure about that.set_include_path('include) will not help much I think.
2. the shell script is not executable for the web user?
I want to suggest to you, pizzipie, that tell us what was wrong and what is your whole working situation now. Making a button execute a shell script is something I may want to do eventually, and I decided to read this thread to get a bit more informed about the subject and its possible problems. But, with everything said in this thread until now, I am basically in the same point I was before starting it.
Make sure you set a strategy for button debouncing.
I mean not to execute more than one database dump when multi clicking the button by accident or not.
I have marked this thread as UNSOLVED because of your input and in addition I have further problems which I hope you can help me with..
Re: Buttons I have 18 <input> statements in the form which contain the type="button" which all work fine.
Example: <input type="button" class="hov" id="btn2g" value="Dump-Databases"
I did have problems with which directory I was in which prevented the script to work:
I changed gzip -fc > DB-Web/mydb-bakup-folder to what you see above in the code.
This allowed the script to run.
However, this created another problem.
When the script runs the files are created and stored in the proper folder 'mydb-bakup-folder' BUT there is no output other than /home/rick/DB-Web which is the PWD. I want to see what files are being processed. I guess this is the old bug-a-boo of server-client processing.
To solve this I created a launcher which calls my bash script and does show me the output BUT only in a tiny new window which is very hard to see. Is there any way to show the output in a normal display window? Or at least format the 'tiny new window' See my latest thread.
Hi I've created an index button that calls a php script that in turn calls a bash script. The bash script won't work'.
"Won't work" is not a useful problem description. When describing problems please tell us at least what you are expecting it to do, and how that differs from what it is actually doing.
The first thing I see is that your button only sets the window.location.href attribute (i.e. loads the new page), it does not submit any other form elements and in particular is not equivalent to an HTTP POST request which puts variable values into the PHP environment.
Having then loaded the page (presumably the PHP script posted) from the new location, it is working exactly as indicated, printing the working directory (and discarding the output of the shell_exec(...) operation, whatever it may be).
Quote:
Originally Posted by pizzipie
Re: Buttons I have 18 <input> statements in the form which contain the type="button" which all work fine.
Example: <input type="button" class="hov" id="btn2g" value="Dump-Databases"
It would be helpful if you would show us the complete <input...> tag, otherwise we cannot know whether it still includes the onclick handler which loads a different page, or whether it submits the parent form.
Quote:
Originally Posted by pizzipie
This allowed the script to run.
However, this created another problem.
When the script runs the files are created and stored in the proper folder 'mydb-bakup-folder' BUT there is no output other than /home/rick/DB-Web which is the PWD. I want to see what files are being processed. I guess this is the old bug-a-boo of server-client processing.
Client-server processing is not a bug-a-boo, it is the way things work. As we have noted in previous threads, it is important to understand how that works in order to understand what is happening when things don't work.
But "this allowed the script to run" tells us nothing useful - it was running before as far as we can tell. Please be complete in your descriptions of what the script is expected to do, and what it is actually doing - how do you know it is running, and how can those here know what is happening?
Please remember that we cannot see the whole picture of what is happening,, only what you tell us.
If this is the script posted above, then it is doing exactly what you told it to do:
Code:
echo getcwd();
shell_exec("mydb-mysqldump.sh");
Which is, echo the working directory and discard the output of the shell_exec(...) function, whatever it may be.
What were you expecting it to do?
Quote:
Originally Posted by pizzipie
To solve this I created a launcher which calls my bash script and does show me the output BUT only in a tiny new window which is very hard to see. Is there any way to show the output in a normal display window? Or at least format the 'tiny new window' See my latest thread.
Please do not ask members to go in search of other threads - if you refer to them please link to them.
Also, please do not open new threads if this is essentially the same problem, which appears likely.
First it was an HTML5 button problem, now in the new thread it is a terminal window size problem, but you still have not told us what it is you are actually trying to accomplish, or anything about the actual script running in the shell_exec(...) function mentioned here, or its execution context, which I suspect is the real problem.
This is beginning to look like another X-Y Problem.
To get help, please describe for us what you are actually trying to accomplish, and in what context (web server, desktop application, terminal, bash script, PHP code, etc.). Demystify it for us, please.
Last edited by astrogeek; 09-29-2017 at 12:40 AM.
Reason: typo
In #10 picture, how did you make the little black window on the right appear? Did you:
1. Did you doubleclick its icon in the desktop?
2. Did you doubleclick its icon in a file manager window?
3. Did you type its full path in the terminal window in the left?
4. Did you type part of its path preceeding it with "./" in the terminal window in the left?
If you did 3 or 4, the script output would be shown the in left window, except if the little window is somehow invoked by that command, but this will be a different problem.
Which is, echo the working directory and discard the output of the shell_exec(...) function, whatever it may be.
What were you expecting it to do?
The attachment shows the output from invoking mydb-mysqldump.shon the command line. This is what I expect.
When I invoke it with the launcher I get that little black background screen.
The third thing I tried was to invoke it thru php. This led to your statement above. I don't understand why you say the code tells it to discard the output. However, the script works in that all the files are 'Dumped'.
It isn't about breaking any strict protocol. It is all about providing a complete and useful problem description so that ohters can understand the problem and offer helpful advice without heading down blind alleys.
Quote:
Originally Posted by pizzipie
The attachment shows the output from invoking mydb-mysqldump.shon the command line. This is what I expect.
Then you should have said this in post #1, not post #12, so that we would know what you were expecting and how the actual result differed from the expected result.
And the answer was indeed guessed here:
Quote:
If this is the script posted above, then it is doing exactly what you told it to do:
Code:
echo getcwd();
shell_exec("mydb-mysqldump.sh");
Which is, echo the working directory and discard the output of the shell_exec(...) function, whatever it may be.
Your code tells it to discard the output of whatever runs in the shell_exec(...) function.
Quote:
Originally Posted by pizzipie
The third thing I tried was to invoke it thru php. This led to your statement above. I don't understand why you say the code tells it to discard the output. However, the script works in that all the files are 'Dumped'.
Then please consider why I may have said that and see the PHP manual page for shell_exec(...) to understand how to display the output instead of discarding it.
Remember, the better and more precise the question, the better and more useful the answers.
As this was the third thing you tried I suspect we may still be working on part Z or an X-Y-Z problem. If this does not provide the answer you ultimately need then please pause and reconsider what your original goal was, and what did not work. Then try to explain just that in simplest, complete terms. This will help others more quickly understand the problem and provide you with the best answers.
And remember that ultimately, programming is not about trying things until something happens to work. Programming is all about fully understanding the task at hand and crafting a solution which best meets the clear requirements of that task. When things do not work it may be because the code is incorrectly implemented, or it may be because the requirements were not correctly understood. Knowing which is key to success!
Last edited by astrogeek; 09-29-2017 at 01:41 PM.
Reason: typos
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.