Skip to content

Instantly share code, notes, and snippets.

@CMAD
Created September 10, 2012 20:45
Show Gist options
  • Save CMAD/3693719 to your computer and use it in GitHub Desktop.
Save CMAD/3693719 to your computer and use it in GitHub Desktop.
script trivia
#!/bin/bash
# micro fileserver
echo "exportfs: server initiated" | logger
if [ "$#" -ne 1 ]; then
echo "Usage: exportfs EXPORT_BASE"
exit
fi
EXPORT_BASE=$(echo $1 | sed 's@/$@@')
CR=`printf '\r'`
fetch() {
local obj=$1
echo "exportfs: command $obj received" | logger
# if it's a directory show it's contents
if [ -d "$obj" ]; then
find "$obj" -maxdepth 1 \( -type d -printf "%p/\n" , -type f -print \)
return
fi
#if it's a file show it's contents too
if [ -f "$obj" ]; then
cat "$obj"
else
return
fi
}
main_loop() {
trap=40
while read -t 15 -n 4096 input; do
trap=$(expr ${trap} - 1)
echo "exportfs: recevied $input as raw materials" | logger
obj=$(/bin/readlink -e "${input%$CR}" | /bin/sed 's@//@/@g;s@/$@@;s@^$@/@')
case "$obj" in
"$EXPORT_BASE"*) fetch "$obj";;
*) echo "exportfs: invalid command received" | logger; exit 1;;
esac
if [ "$trap" -lt 0 ]; then
echo "exportfs: trap for number of operations exceded" | logger
exit 1
fi
done
echo "exportfs: timeout trap exeeded" | logger
exit 1
}
main_loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment