用shell很久了,写了很多脚本,自我感觉写的还可以了,但是有一天当我发现能用shell去实现一个web服务器,一个是我觉得惊奇,一个是让我明白我需要学的东西还很多,下面就和大家分享一下如何利用几个命令就能模拟出来一个web服务器的运行,命令很简单也很简短,但绝对是一个比较新颖的突破,废话不多,直接贴命令。
1、创建一个示例页面Index.html
[root@localhost]# cat index.html
hello world
2、写一个server脚本webserver.sh
[root@locaolhost]# cat webserver.sh
#!/bin/bash
while true
do { echo -e 'HTTP/1.1 200 OK\r\n'; cat index.html; } | nc -l 8080
done
3、这个时候你可以试着去访问 这台机器的8080端口,会返回一个hello world
赶紧去试试吧^_^