Well, there are many possible answers:
- Not many people familiar with Linux shell scripting
- Don't want to learn another language
- PHP have a lot of libraries
- Who knows?
- Unix/Linux like Operating System.
- You must have php-cli.
- Ubuntu: apt-get install php5-cli
- Centos: yum install php-cli
- FreeBSD assuming that you have ports tree installed:
# cd /usr/ports/lang/php5-cli
# make config
# make install
- At command line: php your_scripts.php $arg1 $arg2 ...
- At the very first line of your php scripts insert this line if you are using Ubuntu, no space:
#!/usr/bin/php5
<?php
If you are using other OS, replace /usr/bin/php5 with your PHP executable file. Execute your script:
./path/to/your_scripts.php $arg1 $arg2 ...
Insert the following lines into a test file:
cat > test.phpPaste from command line (ctrl+shift+v):
done with ctrl+c.#!/usr/bin/php5
<?php
foreach($argv as $key => $value){}
echo "\n" . $key . ": " . $value;
Now, you can execute the test.php file using any of the methods above.
- php test.php param1 param2
- ./test.php param1 param2
Note: In order to use this method, you must have executable permission on your php script file:
chmod 0755 test.php.
0: test.phpRemember, argument [0] is always your php script file.
1: param1
2: param2
Resources:
- http://php.net/manual/en/features.commandline.php
No comments:
Post a Comment