bash

Bash Notificaiton

Stephen
I haven’t updated the blog in ages and I think it’s about time that I wrote a post. I’m forever running commands that take forever to complete, I forget about them and only release they have complete once I cycle through all the windows in my tmux session. To overcome this, I wrote a super simple function that sends a desktop notification once the command completes. It also returns the exit code of the command rather than the exit code of notify-send.

Updated Fun with Bash prompts

Stephen
After working with my prompt for a few months I thought it only fixing to update my post with the changes I have made. The update configuration is below: function _git_prompt() { local git_status="`git status -unormal 2>&1`" if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then if [[ "$git_status" =~ nothing\ to\ commit ]]; then local ansi=32 elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then local ansi=31 else local ansi=33 fi echo -n '\[\e[0;33;'"$ansi"'m\]'"$(__git_ps1)"'\[\e[0m\]' fi } function _prompt_command() { PS1="`if [ \$?

Fun with Bash prompts

Stephen
After reading Oliver’s blog about cool bash prompts i thought id give it a go. I wanted a way to also get feedback about my current status of my git branch. After hacking around for ten minutes i came up with function _git_prompt() { local git_status="`git status -unormal 2>&1`" if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then if [[ "$git_status" =~ nothing\ to\ commit ]]; then local ansi=32 elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then local ansi=34 else local ansi=33 fi echo -n '\[\e[0;33;'"$ansi"'m\]'"$(__git_ps1)"'\[\e[0m\]' fi } function _prompt_command() { PS1="`if [ \$?

Find your ip with Bash

Stephen
I only wanted to run a script if the machine had a specific network access so i constructed this below. How to find the machines ip address ifconfig eth0 | grep "inet addr" | awk -F: '{ print $2 }' | awk '{ print $1 }' Then find out if its on a network we care about ifconfig eth0 | grep "inet addr" | awk -F: '{ print $2 }' | awk '{ print $1 }' | grep -qE '172\.

Reading Stdin Bash style

Stephen
Have you ever had the need to read standard input in a bash script, for example cat /var/log/message | mycoolbashscript.sh Well i did when wrapping msmtp for use with mutt see the example bellow #!/bin/bash all=`cat "/dev/stdin"` if `/sbin/ifconfig eth0 | /bin/grep "inet addr" | /usr/bin/awk -F: '{ print $2 }'' | /usr/bin/awk '{ print $1 }'' | /bin/grep -qE '172\.31|32\.[0-9]{1,3}\.[0-9]{1,3}'`; then echo "$all" | tsocks msmtp -a account $* else echo "$all" | msmtp -a account $* fi I then setup mutt to use this as the sendmail binary set sendmail="~/utils/sendmail.

Bash and substring removal

Stephen
Deletes shortest match of $substring from back of $string. ${string%”$substring”} Example find /var/files -name "*.mp3" | while read string; do mv$ $string ${string%"mp3"}wav done Will strip mp3 of the end of the string and add wav