Nix Monitor V0.6

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 - Permalink
Categories: BASH - Linux - Nix - Programming - Projects
No Comments »
 
Recursive Word Count With Bash

I 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’

Update (11/08/07)So that doesn’t work as advertised. I think it’s doing some double counting or something. I’ll post the rewrite when I finish it.
Posted November 3rd, 2007 - Permalink
Categories: BASH - Linux - Programming
No Comments »
 
Bash Line For Constant Monitoring

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

Posted August 1st, 2007 - Permalink
Categories: BASH - Linux - Programming
No Comments »
 
m4a2mp3

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

Update (06/19/07)
Just a quick note, you can use this to convert any supported audio file by replacing all the m4a with the correct extension. E.G. For flac replace m4a with fla. A nice little vim command for that would be :%s/m4a/fla/g
Update (03/18/08)
I posted a version to go from anything (that mplayer can play) to mp3 without editing the script. Check it out here.
Posted June 4th, 2007 - Permalink
Categories: BASH - Computers - Linux - Music - Programming
1 Comment »
 
Really, really, bad.

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.

Update (05/24/07)
Here’s a little sample of the script I wrote for my tree listings.

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.

Another Update (05/24/07)
So, I dug back into the machine and tried the old “hit it with your hand” method, and lo and behold, the drive works again. Dunno why or how, but it’s alive again, and all of that data is coming off of there. (Eeep! 195.21 GB -> 42 DVD’s)
Posted May 24th, 2007 - Permalink
Categories: BASH - Dangit - Programming
No Comments »
 
More Posts
« Older
Newer »