Last active
December 19, 2015 16:18
-
-
Save mamchenkov/5982496 to your computer and use it in GitHub Desktop.
Use the same script for different purposes, based on the name of the file being called (symlinks). Install: chmod +x symlink.php && ln -s another-symlink.php b.php Run: ./symlink.php --with A params && ./another-symlink.php --with B params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* Use the same script for different purposes, based on the | |
* name of the file being called (symlinks). | |
* | |
* Install: chmod +x symlink.php && ln -s another-symlink.php b.php | |
* Run: ./symlink.php --with A params && ./another-symlink.php --with B params | |
*/ | |
// Map physical file names to expected functionality | |
$executables = array( | |
'symlink.php' => 'do_programA', | |
'another-symlink.php' => 'do_programB', | |
); | |
// Get filename that is being used | |
$file = basename($argv[0]); | |
// Only act on known stuff | |
if (!empty($executables[$file])) { | |
call_user_func($executables[$file]); | |
} | |
else { | |
fwrite(STDERR, "Don't know what to do. Sorry. Use only: " . implode(' or ', array_keys($executables)) . ".\n"); | |
exit(1); | |
} | |
/** | |
* Functionality A | |
* | |
* @return void | |
*/ | |
function do_programA() { | |
print "I am doing program A " . merge_params() . "\n"; | |
} | |
/** | |
* Functionality B | |
* | |
* @return void | |
*/ | |
function do_programB() { | |
print "I am doing program B " . merge_params() . "\n"; | |
} | |
/** | |
* Example for using parameters passed to the filename | |
* | |
* @return string | |
*/ | |
function merge_params() { | |
global $argv; | |
return implode(' ', array_slice($argv, 1)); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment