Populate command line from script

0

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.

kainaw

Posted 2016-01-07T19:55:19.183

Reputation: 131

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 – 2016-01-07T20:16:47.393

@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 – 2016-01-07T20:21:15.557

It's tough when manglement sucks ;) – DavidPostill – 2016-01-07T20:39:05.610

You can use "-loops 1" instead of ">/dev/null". – Ayell – 2017-12-24T14:15:53.780

Answers

0

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)

davidgo

Posted 2016-01-07T19:55:19.183

Reputation: 49 152

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 – 2016-01-08T13:31:20.587