dongtang1909 2016-06-04 08:02
浏览 19

如何用php文件创建/注册命令(没有扩展名)?

Well i want create command and register it on Linux/Mac/Windows For example, i want use $ ownco create project hello

But ownc is php script like this(ownco.php or ownco):

<?php
if (count($argv) > 3) {
    echo 'Hi';
} else {
    echo 'Ups!';
}

I dont want use php ownco ... Thanks!

  • 写回答

1条回答 默认 最新

  • doushou6480 2016-06-04 08:24
    关注

    Add a shebang at the first line of your file, something like:

    #!/usr/bin/php
    

    Make the file executable:

    chmod +x ./ownco.php
    

    Now you can run the script from the command line without specifying the interpreter:

    % ./path/to/ownco.php
    Ups!
    

    But to avoid having to write the full path to ownco.php you will need to keep it in a directory that are specified in your $PATH variable, this usually looks something like this:

    % echo "$PATH"
    /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
    

    You can see each directory is seperated by :. You can either move your script to one of these directories or add a new directory to the $PATH variable. Usually this is done my adding a bit of code to ~/.bashrc:

    export PATH="$PATH:/my/new/place/to/keep/executables"
    

    Some people like to create a bin directory in their home directory, and keep small script there.

    % cat ~/.bashrc
    ...
    ...
    export PATH="$PATH:$HOME/bin"
    ...
    ...
    
    
    % cat > ~/bin/script1 <<EOL
    #!/usr/bin/php
    <?php
    echo("my first script
    ");
    ?>
    EOL
    % chmod +x ~/bin/script1
    % script1
    my first script
    

    Notes:

    The shebang can be specified on the first line of executable files and start with #!.

    I choose to use /usr/bin/php as the executable. This is the path for where PHP is stored.

    Some people like to use /usr/bin/env php which will run the version of PHP which are specified in your $PATH variable. See more

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上