To run a shell command with PHP, use shell_exec. It will run as the web user (say, www for Apache), so that user will need to have appropriate permissions. You could, of course, use sudo to get around that - but that is extremely insecure and a very, very bad idea.
So if you had, e.g. a shell script called shellscript.sh sitting on yoru server, and you've written it to do whatever you need it to do, you could run it in php with:
However - I'm not sure I correctly understand what you're trying to do, but it seems to me this is a hacky, very inefficient way to go about things, and it's very likely there is a much smoother solution. Even so, good luck.