目标:
- 在当前目录下启动一个静态文件服务器
- 设定一个50000端口为默认静态服务端口
- 如果当前端口已经被占用,需要向上找一个可以使用的端口
写在前面的完整示例:
#!/bin/bash
docfile() {
loop() {
if [ $(lsof -i:$1 | wc -l) -eq 0 ]; then
chrome --new-window http://127.0.0.1:$1;
php -S 127.0.0.1:$1 -t $2;
else
loop $(($1+1)) $2;
fi
}
if [ -z $2 ]; then
loop $1 $(pwd);
else
loop $1 $2;
fi
};
## 示例1 有目标目录
docfile 50000 /Users/rc/Documents/JSDoc/jsdoc.github.io
## 示例2 无目标目录
docfile 50000
命令别名
alias docfile='docfile() { loop() { if [ $(lsof -i:$1 | wc -l) -eq 0 ]; then chrome --new-window http://127.0.0.1:$1; php -S 127.0.0.1:$1 -t $2; else loop $(($1+1)) $2; fi }; if [ -z $2 ]; then loop $1 $(pwd); else loop $1 $2; fi }; docfile 50000 '
结果输出
➜ ~ alias docfile
docfile='docfile() { loop() { if [ $(lsof -i:$1 | wc -l) -eq 0 ]; then chrome --new-window http://127.0.0.1:$1; php -S 127.0.0.1:$1 -t $2; else loop $(($1+1)) $2; fi }; if [ -z $2 ]; then loop $1 $(pwd); else loop $1 $2; fi }; docfile 50000 '
➜ ~ docfile
[Fri Apr 29 12:59:14 2022] PHP 8.1.5 Development Server (http://127.0.0.1:50002) started
设计
1. 查看端口是否被占用
➜ ~ lsof -i:50000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php 63637 rc 5u IPv4 0x2f9d5a5323563b45 0t0 TCP localhost:50000 (LISTEN)
➜ ~
2. 如果端口不可用则更换一个端口
3. 默认以当前目录为静态服务器目录, 也可指定文件目录
-z 判断当前值是否为null
if [ -z $2 ]; then
loop $1 $(pwd);
else
loop $1 $2;
fi
启动静态服务
查看所有命令
➜ ~ php --help
Usage: php [options] [-f] <file> [--] [args...]
php [options] -r <code> [--] [args...]
php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
php [options] -S <addr>:<port> [-t docroot] [router]
php [options] -- [args...]
php [options] -a
-a Run as interactive shell
-c <path>|<file> Look for php.ini file in this directory
-n No configuration (ini) files will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse and execute <file>.
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-r <code> Run PHP <code> without using script tags <?..?>
-B <begin_code> Run PHP <begin_code> before processing input lines
-R <code> Run PHP <code> for every input line
-F <file> Parse and execute <file> for every input line
-E <end_code> Run PHP <end_code> after processing all input lines
-H Hide any passed arguments from external tools.
-S <addr>:<port> Run with built-in web server.
-t <docroot> Specify document root <docroot> for built-in web server.
-s Output HTML syntax highlighted source.
-v Version number
-w Output source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin
--ini Show configuration file names
--rf <name> Show information about function <name>.
--rc <name> Show information about class <name>.
--re <name> Show information about extension <name>.
--rz <name> Show information about Zend extension <name>.
--ri <name> Show configuration for extension <name>.
完整示例
php -S 127.0.0.1:8081 -t /Users/shang/Downloads/opencv-4.5.5
示例解读
将 静态目录
/Users/shang/Downloads/opencv-4.5.5
挂载到
127.0.0.1:8081
示例展示
使用docfile直接启动静态服务
➜ jsdoc.github.io git:(master) ✗ pwd
/Users/rc/Documents/JSDoc/jsdoc.github.io
➜ jsdoc.github.io git:(master) ✗ docfile
[Fri Apr 29 13:40:50 2022] PHP 8.1.5 Development Server (http://127.0.0.1:50006) started
当前目标下再次启动静态服务
➜ jsdoc.github.io git:(master) ✗ docfile
[Fri Apr 29 13:42:30 2022] PHP 8.1.5 Development Server (http://127.0.0.1:50007) started
端口自动增加1
版权声明:本文为swe_ling原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。