I’m releasing version 0.6 of Nix Monitor today. It’s gotten a bit bigger and harder to configure, so I wrote a kickin configurator script using Zenity for all my prompts. Check it out at http://kde-look.org/content/show.php/Nix+Monitor?content=67399
Posted November 13th, 2007 - PermalinkI was curious how many lines were in the new OurUNO rewrite, so I decided to write a little script to find out. Well, that all got out of hand and I kept adding things and masks and depth recursion limiting and I managed to stop myself before I added color to the script, so I did okay I guess.
Anyway, here it is. I’m sure there is some really clever way to do an equivalent one-liner of this, but hey, that’s life.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/bin/sh function printUsage { echo "Usage: countLines directory [options]" echo echo "Options:" echo " -m=XX --mask=XX - The mask may be any grep style regular expression." echo " -d=XX --depth=XX - The maximum depth of recursion. Defaults to 20." echo exit } if [ $# -le 0 ]; then printUsage exit fi if [ "$1" == "-v" ]; then printUsage exit fi TOTALCOUNT=0 FILEMASK='' MAXDEPTH=20 # Drag out our options... for i in $@; do if [ `echo $i | sed 's/^\(-m=\).*$/\1/'` == "-m=" ]; then FILEMASK=`echo $i | sed 's/^-m=\(.*\)$/\1/'` elif [ `echo $i | sed 's/^\(--mask=\).*$/\1/'` == "--mask=" ]; then FILEMASK=`echo $i | sed 's/^--mask=\(.*\)$/\1/'` elif [ "$i" == "$1" ]; then continue; elif [ `echo $i | sed 's/^\(-d=\)[0-9]*$/\1/'` == "-d=" ]; then MAXDEPTH=`echo $i | sed 's/^-d=\(.*\)$/\1/'` elif [ `echo $i | sed 's/^\(--depth=\)[0-9]*$/\1/'` == "--depth=" ]; then MAXDEPTH=`echo $i | sed 's/^--depth=\(.*\)$/\1/'` else printUsage exit fi done CURDEPTH=0 function wcDir { FILES=`ls -l $1 | grep ^- | awk '{print $8}' | grep -e "$FILEMASK"` for i in $FILES; do LINES=`wc -l $1/$i | awk '{print $1}'` TOTALCOUNT=$(($LINES + $TOTALCOUNT)) done } function recurseDir { COUNT=`ls -l $1 | grep ^d | awk '{print $8}' | wc -l` CURDEPTH=$(($CURDEPTH + 1)) if [ $COUNT != 0 ] && [ $CURDEPTH -lt $MAXDEPTH ]; then for i in `ls -l $1 | grep ^d | awk '{print $8}'`; do recurseDir $1/$i done fi wcDir $1 CURDEPTH=$(($CURDEPTH - 1)) } recurseDir $1 echo $TOTALCOUNT |
Bonus! Here’s a tip for posting scripts on the interwebs. Replace your tabs with spaces before copying them into your posts with:
$ cat scriptOrCodeSource | sed ’s/\t/ /g’
I’ve been doing a lot more bash stuff recently for work and with a book I’m reading. One thing I found handy is this one-liner script to do something every N seconds.
while :; do ps aux | grep ssh | grep -v grep; echo "----------[$(date)]------------";sleep 1; done
You can replace the sleep 1 with sleep N for a larger interval, and the ps aux | grep ssh | grep -v grep is just what I wanted done every second. The echo “———-[$(date)]————” is a nice way to separate and mark the timing.
An example run:
root 3610 0.0 0.0 59516 568 ? Ss Jul30 0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid jmhobbs 3963 0.0 0.0 46636 432 ? Ss Jul30 0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc ----------[Wed Aug 1 14:21:07 CDT 2007]------------ root 3610 0.0 0.0 59516 568 ? Ss Jul30 0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid jmhobbs 3963 0.0 0.0 46636 432 ? Ss Jul30 0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc ----------[Wed Aug 1 14:21:08 CDT 2007]------------ root 3610 0.0 0.0 59516 568 ? Ss Jul30 0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid jmhobbs 3963 0.0 0.0 46636 432 ? Ss Jul30 0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc jmhobbs 2404 0.0 0.1 49480 1004 ? Ss 14:21 0:00 /usr/bin/ssh -f -N -i /var/auth/tunnel_grandisland -L 36200:127.0.0.1:3306 -l grandisland statserver ----------[Wed Aug 1 14:21:09 CDT 2007]------------ root 3610 0.0 0.0 59516 568 ? Ss Jul30 0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid jmhobbs 3963 0.0 0.0 46636 432 ? Ss Jul30 0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc ----------[Wed Aug 1 14:21:10 CDT 2007]------------ root 3610 0.0 0.0 59516 568 ? Ss Jul30 0:00 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid jmhobbs 3963 0.0 0.0 46636 432 ? Ss Jul30 0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc ----------[Wed Aug 1 14:21:11 CDT 2007]------------
Update: (2008-09-23)
Silly me, I just inelegantly rewrote watch
watch -n 1 'ps aux | grep ssh | grep -v grep'
The -n is for specifying seconds between refresh.
Every 1.0s: ps aux | grep ssh | grep -v grep Tue Sep 23 15:22:15 2008 root 3510 0.0 0.0 6016 620 ? Ss Sep05 0:02 /usr/sbin/sshd -o PidFile=/var/run/sshd.init.pid jmhobbs 12606 0.0 0.0 5452 456 ? Ss Sep18 0:00 /usr/bin/ssh-agent /bin/bash /etc/X11/xinit/xinitrc
I had some m4a encoded files I wanted in mp3 (don’t judge me) so I cooked up a modified script from one found here, that does the trick.
1 2 3 4 5 6 7 8 9 10 11 12 | #!/bin/bash # # m4a2mp3 # for i in *.m4a; do mplayer -vc null -vo null -ao pcm:fast "$i" -ao pcm:file="${i%.m4a}.wav" done for i in *.wav; do lame -h -V2 --vbr-new "$i" "$(i%.wav}.mp3" done rm *.wav |
So my little freak-out post about my hard drive just proved itself true. WDE, my 320GB drive full of movies, music, books, and (ironically) backups just gave up the ghost. It actually froze up my machine in it’s dying gasps.
I know it wasn’t full of content, but that’s a big hit. It’s also the newest drive in my machine, lasting one day under 8 months. Thats silly. Plus I voided the warranty on it, so no replacement for me. Oh, bonus note, I bought 100 DVD blanks yesterday to start backing things up on from it.
This little fiasco is going to implement some new procedures. I’m making weekly tree listing of my media folders so I can know what I lose from here on out, and I’m going to back important stuff up to disc. This really sucks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #!/bin/bash DATE=`eval date +%F` tree /media/hdb1/Music > MUSIC.$DATE MUSIC=`eval diff MUSIC.$DATE MUSIC.CURRENT` if [ "$MUSIC" = "" ]; then echo "No Change" > MUSIC.$DATE else cp MUSIC.$DATE MUSIC.CURRENT fi tree /media/hdb1/Torrents > TORRENTS.$DATE TORRENTS=`eval diff TORRENTS.$DATE TORRENTS.CURRENT` if [ "$TORRENTS" = "" ]; then echo "No Change" > TORRENTS.$DATE else cp TORRENTS.$DATE TORRENTS.CURRENT fi |
As much as all of this sucks, I really enjoyed learning that much bash scripting.