[LinuxSIG] bash_httpd.sh

Craig Miller cvmiller at gmail.com
Sat Mar 2 14:19:48 PST 2024


Ron,

This is my complaint. It looks like bash code, but it doesn't run. I 
followed the instructions, made it executable, and then tried to run, 
and I get this:

$ ./bash_web.sh
./bash_web.sh: line 6: HTTP: command not found
./bash_web.sh: line 9: syntax error near unexpected token `)'
./bash_web.sh: line 9: `'{print $1}') path=$(echo "$request" | awk '{print $2}')'

OK, so the script is a little munged from the copy and paste. So I spend 15 minute cleaning that up and I get this:

$ curl localhost:8080/
curl: (52) Empty reply from server

So I stand by what I said, AI isn't good at bash programming. You want a job that won't fall prey to AI, use bash ;-)

Craig....

On 3/2/24 12:23, Ron / BCLUG wrote:
> #!/bin/bash
>
> # Define the port on which the server will listen PORT=8080
>
> # Function to handle incoming requests handle_request() { # Read the
> HTTP request from the client read -r request
>
> # Extract the request method and path method=$(echo "$request" | awk
> '{print $1}') path=$(echo "$request" | awk '{print $2}')
>
> # Serve a simple response if [ "$method" = "GET" ]; then if [ "$path"
> = "/" ]; then echo -e "HTTP/1.1 200 OK\r\nContent-Type:
> text/html\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>" 
> else echo -e "HTTP/1.1 404 Not Found\r\nContent-Type:
> text/plain\r\n\r\n404 Not Found" fi else echo -e "HTTP/1.1 501 Not
> Implemented\r\nContent-Type: text/plain\r\n\r\n501 Not Implemented" fi }
>
> # Main loop to listen for incoming connections main() { echo
> "Starting server on port $PORT..." mkfifo fifo trap "rm -f fifo"
> EXIT
>
> # Continuously listen for connections while true; do # Wait for a
> client to connect cat fifo | nc -l -p "$PORT" > >(handle_request) done }
>
> main 
-- 
IPv6 is the future, the future is here
ipv6hawaii.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://vicpimakers.ca/pipermail/linuxsig_vicpimakers.ca/attachments/20240302/eaa07fdb/attachment.htm>


More information about the LinuxSIG mailing list