bash
Updated bash PS1
Made a minor tweak to my .bashrc after browsing dotfiles.org for some ideas. One neat trick I gleaned was detecting when the exit code of the last command ($?) was non-zero and altering the prompt. This will be useful for quickly seeing at a glance if some enormous load of output from make was successful or not.
Here are the bits from my updated .bashrc:
-
# define useful aliases for color codes
-
sh_norm="\[\033[0m\]"
-
sh_black="\[\033[0;30m\]"
-
sh_darkgray="\[\033[1;30m\]"
-
sh_blue="\[\033[0;34m\]"
-
sh_light_blue="\[\033[1;34m\]"
-
sh_green="\[\033[0;32m\]"
-
sh_light_green="\[\033[1;32m\]"
-
sh_cyan="\[\033[0;36m\]"
-
sh_light_cyan="\[\033[1;36m\]"
-
sh_red="\[\033[0;31m\]"
-
sh_light_red="\[\033[1;31m\]"
-
sh_purple="\[\033[0;35m\]"
-
sh_light_purple="\[\033[1;35m\]"
-
sh_brown="\[\033[0;33m\]"
-
sh_yellow="\[\033[1;33m\]"
-
sh_light_gray="\[\033[0;37m\]"
-
sh_white="\[\033[1;37m\]"
-
-
case `hostname` in
-
"livehost"|"production_server"|"sauron")
-
HOSTCOLOUR=${sh_red}
-
;;
-
"staging-node") HOSTCOLOUR=${sh_yellow} ;;
-
*) HOSTCOLOUR=${sh_green} ;;
-
esac
-
-
export PROMPT_COMMAND=‘if [ $? -ne 0 ]; then ERROR_FLAG=1; else ERROR_FLAG=; fi; ‘
-
export PS1=${sh_white}‘\u@’${HOSTCOLOUR}‘\h‘${sh_norm}‘ \w\n‘${sh_norm}‘${ERROR_FLAG:+’${sh_light_red}‘}\$${ERROR_FLAG:+’${sh_norm}‘} ‘
I’m also using the hostname to decide what colour the host appears in the prompt. My home directory, and thus .bashrc, is mounted on most hosts I log in to, and this serves as a reminder if I’m logged in to a production host. Green is the default, and it’s overridden for various special hosts.
Transcoding HTTP mp3 streaming proxy in bash
Here’s how to make a proxy for streaming mp3s. It transcodes on-the-fly to 64kpbs MP3 using lame. When transcoding is finished, it calls the ./posthandler.sh script, which can either just delete the file, or potentially archive it so you don’t need to transcode it again.
-
#!/bin/bash
-
read method url version
-
-
method="${method%$CR}"
-
url="${url%$CR}"
-
version="${version%$CR}"
-
-
echo -ne "HTTP/1.0 200 OK\r\nContent-type: audio/mpeg\r\n\r\n"
-
-
BR=64 #birate to transcode to.
-
PIPE="/tmp/$$.pipe"
-
mkfifo "$PIPE"
-
-
OUTFILE="./tmp.$$.$BR.mp3"
-
rm $OUTFILE
-
url=`echo "$url" | sed ‘s/\///’`
-
echo "** GET $url" >&2
-
-
nohup lynx –source "$url" \
-
| (lame –preset cbr $BR –mp3input – - 2>/dev/null \
-
&& (echo "** Finished transcoding $url" >&2 ; \
-
./posthandler.sh "$OUTFILE"&))\
-
| tee -i "$PIPE" > $OUTFILE &
-
-
cat < $PIPE
-
rm $PIPE
One interesting limitation seems to be the buffer size of a fifo pipe in linux. Even though the transcoding step is pretty quick, if a client is connected the transcoding only manages to fill the pipe a couple of hundred k ahead of what is being read.
The -i flag to `tee` means it ignores interrupts, and will finish transcoding the file and call the posthandler even if the client disconnects.
Run is like this:
while [ 1 ]; do nc -vlp 8080 -c './transstreamer.sh' ; done
Then hit up a url of your choice using your awesome new proxy:
mpg321 "http://localhost:8080/http://freedownloads.last.fm/download/105468518/Letters%2BFrom%2BThe%2BBoatman.mp3"
Not the most scalable solution, but a mildly amusing quick hack.
About Me
Tags
Recent Posts
- Rewriting Playdar: C++ to Erlang, massive savings
- Erlang talk at London Hackspace
- Anti-RDBMS: A list of distributed key-value stores
- How we use IRC at Last.fm
- Getting to know ejabberd and writing modules
- ssh hack: connect directly to machine via a firewall box
- A Million-user Comet Application with Mochiweb, Part 3
- A Million-user Comet Application with Mochiweb, Part 2
- A Million-user Comet Application with Mochiweb, Part 1
- On bulk loading data into Mnesia
