3

So I'm working with a designer on a website in PHP/MySQL and there are a few scripts that he would like to have to make life easier for him. He is pretty comfortable using the command line for stuff like git, SASS, node, etc. I would like him to be able to run my scripts like he would run a program, instead of running it through PHP.

So instead of this:

php /path/to/file/create_module.php module_name

I would like to do this:

myscript create_module module_name

Is it possible to do this with PHP on an Apache server? I know I will most likely have to modify the server to interpret it properly, which is fine. I just don't even know where to begin, and couldn't find what I needed on Google.

1
  • What is myscript supposed to do? Commented Dec 21, 2012 at 17:13

4 Answers 4

3

Your best bet would to be to create an alias.

So an alias of myscript would actually point to the command: php /path/to/file/create_module.php and then any extra arguments will be passed as typed.

In command line, do the following:

cd /etc/
nano bash.bashrc

At the very bottom of the file, add this line of text:

alias "myscript=php /path/to/file/create_module.php"

BASHRC is a script that is run on user login, so the alias will be recreated every time the user logs into the system.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for, thanks! Just a note, if you are using CentOS then the bash file is located at /etc/bashrc instead of /etc/bash.bashrc
2

I am not sure what you are looking for myscript to do, but to run a php script via the command line without specifying the php binary, just add a a shebang, like

#!/bin/env php
<?php

// The above expects env is in /bin

$foo = "bar";

Or the full path if you like

#!/usr/bin/php

2 Comments

Don't forget to make the file executable
also you have to make sure your script is path - independent.
1

Another option: Shell script wrapper

create a new text file in /bin or another directory in your PATH, name it how you would like to invoke your script and give it this content

#!/bin/bash
cd /path/to/your/php/scripts/folder
php script.php $*

don't forget to

chmod a+x /path/to/bash/script

The advantage of this is that your PHP script is run in the right directory where it may expect other resources to be that it depends on.

Comments

0

On A Linux Solution:

/usr/bin/php /path/to/file.php

On a Windows Solution:

C:\Path\To\PHPExe C:\Path\To\phpfile.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.