Sunday, September 28, 2008

"renamescore"

(Yes, yet another shell script)
I wrote a little bash script to replace all the underscores in a filename with spaces, in the current directory.
Here it is:


#!/bin/bash
#this script renames all filenames containing _ with [space], in the current directory
#copy to /usr/local/bin and set permission as +x

total=`ls | grep .*_.* | wc -l`
count=0
while [ $count -lt $total ]
do

filename=`ls | grep .*_.* | head -1`
newfilename=`ls | grep .*_.* | head -1 | tr '_' ' '`
mv "$filename" "$newfilename"
echo "renamed $filename to $newfilename"
count=`expr $count + 1`
done

Friday, September 12, 2008

Alternate approach to 'keeponline'

Instead of running the 'keeponline' script (described here) as a cron job, I decided to make it run as a standalone script. (Because the cron job kept spamming my syslog file)

The new /usr/local/bin/keeponline.sh looks like this:

#!/bin/bash
echo "Going Online..."
/usr/local/bin/online.sh > /dev/null
while true; do
ping -c 1 -W 120 www.google.com > /dev/null

if [ "$?" = "0" ]; then
echo "You are connected"
else
/usr/local/bin/online.sh > /dev/null
echo "Disconnect detected at `date`, reconnecting" >> /home/akudewan/logs/keeponline.log
fi
sleep 10m
done


Next I make another script, keeponline-launch inside /etc/network/if-up.d/

#!/bin/bash

# Not for loopback!
[ "$IFACE" != "lo" ] || exit 0

sh /usr/local/bin/keeponline.sh &

exit 0


This will basically launch the keeponline script when the network interface comes up :)

(Thanks to Vivaldi Gloria from ubuntuforums)