I'm using the following Bash CGI to upload a file:
#!/bin/bash
echo "Content-Type: text/plain"
echo
if [ "$REQUEST_METHOD" = "POST" ]; then
TMPOUT=hello
cat >$TMPOUT
# Get the line count
LINES=$(wc -l $TMPOUT | cut -d ' ' -f 1)
# Remove the first four lines
tail -$((LINES - 4)) $TMPOUT >$TMPOUT.1
# Remove the last line
head -$((LINES - 5)) $TMPOUT.1 >$TMPOUT
# Copy everything but the new last line to a temporary file
head -$((LINES - 6)) $TMPOUT >$TMPOUT.1
# Copy the new last line but remove trailing \r\n
tail -1 $TMPOUT | tr -d '\r\n' >> $TMPOUT.1
fi
This is for a uClinux/Busybox server. When a file is passed this way, the original $TMPOUT
will contain a four line head and one line tail that need to be removed to end up with the same file. The resulting file's hash is identical to the original.
It works but it seems pretty ugly, creating two files and such. I'm by no means a pro in bash, can this be made prettier?
Keep in mind that the target is a little embedded device and has no Perl/Python or anything on it. It needs to be pure bash.