I have a PHP script that goes through a lot of complication and, in the end, produces a command for the user to run. Right now, it prints the command and then quits. The user has to copy the command and paste it to run it. Because the PHP command is being run on the command line, it would be easier for the user if the PHP script was able to place the command on the command line. The user can just press enter. (Yes - it is a requirement that a human runs the command or I'd just execute the command from inside the script.)

I have tried using xclip to copy the command to the clipboard. It hangs. I've used exec and system to run it. The command to be run is in $command and I used:

exec("echo \"$command\" | xclip");

With both exec and system, it hangs until I ctrl-C and there's nothing in the clipboard.

UPDATE I found that if I pipe xclip to /dev/null in the command, it does not hang. I can then add it to the user's clipboard and he/she can run it using ctrl+shift+v

exec("echo \"$command\" | xclip -sel clip -i > /dev/null");

I've tried wrapping the whole thing in complicated scripts on the command line, tailing off the last line of output, and trying to paste that into the command line. I can get it to print the command, but not on the command line. It always prints the command and then the command line comes up below it.

I am using BASH for the command line. So, any solution must work in the bash shell - assuming that there is a solution.

  • Can't your PHP script just prompt the user "To execute this {command} please press enter", wait for an enter key, then actually execute the command? – DavidPostill Jan 7 '16 at 20:16
  • @DavidPostill That was my suggestion. It didn't fly with the brass that are (ignorantly) making the decisions. I said that I would see if it is possible to do what they want. My next plan of action is to write a fake bash shell inside the PHP so it looks like you are running everything in bash - but then I will risk running the command inside itself over and over and over.... – kainaw Jan 7 '16 at 20:21
  • It's tough when manglement sucks ;) – DavidPostill Jan 7 '16 at 20:39
  • You can use "-loops 1" instead of ">/dev/null". – Ayell Dec 24 '17 at 14:15

Not exactly pressing enter, but not much harder - the command

history -s "command to put in history"

Will add the given command line into the bash history. You can then simply use [up arrow][enter] to retrieve and run the injected command.

(I'm not sure if you could exec this from PHP directly, or if you would need to wrap the PHP script into a bash script, but this should be doable)

  • This works on the command line, but (so far) not from inside a PHP script. I did get me thinking about how to push an "up arrow" into STDIN on exit - which has me thinking about how to push entire strings into STDIN on exit. – kainaw Jan 8 '16 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.