#!/bin/bash abspath=$(readlink -f "$0") # Set variable abspath to the absolute path where this script is located dir=$(dirname "$0") # Set variable dir to source directory (the directory used when executing this script, for example "." if the script was executed with ./) name=$(basename "$0") # Set variable name to name of this script while getopts "hd:" flag; do case $flag in h) # Handle the -h flag echo "Usage: nc -lk 1900 -e \"$dir/$name -d \" note: netcat (nc) is not necessary, however this script ($abspath) does not have any networking, meaning you will need netcat or something similar to make the server accessible over a computer network options: -h Display this help screen -d specify root directory for server (where index and any other files should be located)" exit # Display script help information ;; d) # Handle the -d flag with an argument rootdir=$OPTARG # Sets the specified path as root directory for server (where index and any other files will be located) ;; \?) # Handle invalid options echo "Internal server error: invalid option. See $dir/$name -h for help." # Display error exit ;; esac done if [[ -z $rootdir ]] # Check if variable rootdir has a length of zero then echo "Internal server error: no root directory specified. See $dir/$name -h for help." # Display error exit else : fi if [[ $rootdir == */ ]] # Check if variable rootdir ends in / then rootdir=${rootdir::-1} # Remove last character from variable rootdir else : fi read input # Set variable input to requested path if [[ $input == /* ]] # Check if variable input begins with a / then : else input=/$input # Add / to beginning of variable input fi if [[ $input == */ ]] # Check if requested path is a directory then path=$rootdir$input/index # Set variable path to index file contained by requested directory else path=$rootdir$input # Set variable path to requested file fi if [[ -a $path ]] # Check if requested file exists then cat $path # Serve requested file else echo document not found # Display error fi exit