Personal scripts
Thu, 2006-04-20 02:40
Post your personal scripts here! I keep mine in /usr/local/scripts/, which is in my $PATH.
sizeof (obsoleted by fsize).
#!/bin/sh if [ -z $1 ]; then echo "Error: no files or directories specified." exit 1 fi for file; do if [ -d $file ]; then echo $(du -hs $file)$separator else echo $(ls -lh $file|cut -d\ -f5) \($file\)$separator fi; done
reset
#!/bin/sh cd&&clear
Be sure to alias reset=". reset" or the cd won't do anything.
I have exit aliased to reset for /dev/pts/1, so the console on my desktop doesn't close.
swapclear
#!/bin/sh su -c "/sbin/swapoff -a&&/sbin/swapon -a"
swap
#!/bin/sh cp $1 ~/.temp mv $2 $1 mv ~/.temp $2
inject
#!/bin/sh eject -t $1










Why /usr/local/scipts?
Why not /usr/lib/<your name>/scripts?
I dunno -- I'm at odds with wondering why /usr/local exists at all.
Anyway, here's mine:
bounceweb
bounceswap -- I use a swap file instead of a partition
I use this one to switch between PHP 4 and 5. Usually I work on five, but since most hosters out there still have 4 I have to test my stuff on that too, especially my classes which I always write for PHP5 and then backport to 4.
case $1 inphp4)
cp /usr/lib/php4.ini /usr/lib/php.ini
cp /usr/bin/php4 /usr/bin/php
cp /usr/local/apache/conf/httpd-php4.conf /usr/local/apache/conf/httpd.conf
cp /usr/local/apache2/conf/httpd-php4.conf /usr/local/apache2/conf/httpd.conf
/etc/rc.d/rc.httpd stop
sleep 2
/etc/rc.d/rc.httpd start
;;
php5)
cp /usr/lib/php5.ini /usr/lib/php.ini
cp /usr/bin/php5 /usr/bin/php
cp /usr/local/apache/conf/httpd-php5.conf /usr/local/apache/conf/httpd.conf
cp /usr/local/apache2/conf/httpd-php5.conf /usr/local/apache2/conf/httpd.conf
/etc/rc.d/rc.httpd stop
sleep 2
/etc/rc.d/rc.httpd start
;;
esac
Save MBR and partition table of a disk:
dd if=/dev/$1 of=$1.mbr count=1 bs=512sfdisk -d /dev/$1 > $1.sf
Restore MBR and partition table to a disk:
dd if=$1.mbr of=/dev/$1sfdisk /dev/$1 < $1.sf
I've got a script that logs bandwidth spent via ppp network interface whenever I disconnect using a "hang" command I also made:
This is in /etc/ppp/ppplog/ppptraf.sh
It gathers ppp bandwidth information using ifconfig.
#!/bin/bash# Traffic logger for ppp0 interface (both downstream and upstream)
cd /etc/ppp/ppplog/
# Add last traffic data to ppptraf.log in the same directory
ifconfig ppp0 > ./ifconfig.tmp && date >> ppptraf.log && grep -i 'RX Byte' ifconfig.tmp >> ppptraf.log
# Add last traffic data to /mnt/data/ppp-traffic.log to last longer
# when switching between distros given that /mnt/data is your permanent data partition.
# Change the "/mnt/data" to your path. If you don't need this, comment it.
date >> /mnt/data/ppp-traffic.log && grep -i 'RX Byte' ./ifconfig.tmp >> /mnt/data/ppp-traffic.log
# Report last traffic information to the screen and let me know it's done
echo "----------------------------------"
tail --lines=2 /mnt/data/ppp-traffic.log
echo "----------------------------------"
echo "ppp traffic has been logged"
exit
The hang command is simply a script located at /usr/bin/hang that looks like this:
GNU nano 1.2.5 File: /usr/bin/hang#!/bin/bash
# Run connect command and the ppplog script
/etc/ppp/ppplog/ppptraf.sh
killall pppd
echo "You are now disconnected!"
exit
So it basically runs ppptraf.sh before killing pppd so that it logs the bandwidth information before it is lost.
Also, less significant is a hail command which I use instead of having to manually start both network interfaces whenever connecting to the internet (since I use both ppp and dvb interfaces, where ppp is for my EDGE modem and dvb is for my satelite card for satelite downstream connection).
So, the hail command starts both:
#!/bin/bashecho "Hailing internet..."
/usr/bin/hail.sh &
exit
calling hail.sh (because then hail can release terminal for other commands to be issued afterwards).
#!/bin/bash# Run connect commands
/opt/tqrecv/start_satadsl.sh
pppd call edge
(probably not the sexiest solution, but hey..)
So, when I connect to the internet I usually just say "sudo hail" and I'm online. When disconnecting I say "sudo hang" and it logs my traffic from ppp to /mnt/data/ppp-traffic.log for my later inspection and kills ppp. DVB connection doesn't need to be killed however since it doesn't work without ppp anyway, it is just a satelite downstream used through a proxy.
I think this is pretty much the only meaningful thingie coming close to some sort of scripting/programming that I've done, except maybe for the libervis and nuxified site designs.
Cheers
I have quite a few of them. $HOME/bin is my preferred place. I'm not on my box so I can't post any of em. One of them takes a list of mp3/ogg files and burns a cd out of em, another cool one will take a command and log all output of it (stdout and/or stderr) to a file, very useful with startx. Can't really remember any more, I just make and use em without thinking about it. And many things other people would put into scripts are quite trivial for me so scripting it is a bother. And I can conjure up some crazy for and while loops too! I have rewritten quite a few utilities from coreutils and util-linux as shell scripts. Not much work to replace several MBs of bins with a couple KBs of POSIX compliant shell script. I wonder how many people realize how unnessecary the cat utility is...
I have quite a few of them. $HOME/bin is my preferred place. I'm not on my box so I can't post any of em. One of them takes a list of mp3/ogg files and burns a cd out of em, another cool one will take a command and log all output of it (stdout and/or stderr) to a file, very useful with startx. Can't really remember any more, I just make and use em without thinking about it. And many things other people would put into scripts are quite trivial for me so scripting it is a bother. And I can conjure up some crazy for and while loops too! I have rewritten quite a few utilities from coreutils and util-linux as shell scripts. Not much work to replace several MBs of bins with a couple KBs of POSIX compliant shell script. I wonder how many people realize how unnessecary the cat utility is...
Can you post the smaller ones and link to all of them?
some small scripts on my home page:
http://jollybox.de/toolbox/index.en.php
and
#!/bin/bash# any2xvid
output="${1%.*}.avi"
if test "$2" ; then output="$2";fi
echo -en "\033]0;any2xvid: converting $1 to $output\007"
mencoder -o "$output" -ovc xvid -oac mp3lame -srate 48000 -ofps 25 -xvidencopts bitrate=687 "$1"
useful for civilizing stray wmv9's
I updated my sizeof script to handle multiple files/directories.
For some reason I couldn't post them.. so I'll post later when I have them uploaded and linked here.
dos2unix
initwifi.sh
#!/bin/sh if([ "$1" = "1" ]) then essid=belkin54g elif([ "$1" = "2" ]); then essid=USR8054 key=5275-7373-6F elif([ -n "$1" ]) then essid=$1 if([ -n "$2" ]) then key=$2 fi else echo "No preset, or ESSID set. Exiting.." exit 1 fi modules=/opt/wifi echo=/bin/echo sudo=/usr/bin/sudo iwconfig=/sbin/iwconfig grep=/bin/grep dhclient=/sbin/dhclient bash=/bin/bash prevdir=$(pwd) cd $modules $echo -n 'starting wifi .. ' $sudo sh -c "$bash wlan0down &> /opt/wifi/down.log" $sudo sh -c "$bash wlan0up &> /opt/wifi/up.log" until($sudo $iwconfig wlan0 2> /dev/null | $grep $essid > /dev/null) do $sudo $iwconfig wlan0 essid $essid $echo retrying ESSID done if([ -n "$key" ]); then until($sudo $iwconfig wlan0 2> /dev/null | $grep $key > /dev/null) do $sudo $iwconfig wlan0 key open $key $echo retrying WEP done fi sleep 1 $dhclient wlan0 $echo > /etc/resolv.conf $echo "nameserver 208.67.220.220" >> /etc/resolv.conf $echo "nameserver 208.67.222.222" >> /etc/resolv.conf $echo done. cd $PREVDIRmatrix.sh
#!/bin/bash #echo $(sleep 5;echo) | cmatrix -b -s; function printword { for i in $1 do echo -n "$i" sleep .25; done echo -n " " sleep .5 } printword "W a k e" printword "u p," printword "N e o" echomkscrn
np
#!/bin/bash PASS='rofl' HOST='127.0.0.1' PORT=6600 b=$(perl -e ' print chr(2) ') v=$(perl -e ' print chr(22) ') c=$(perl -e ' print chr(3) ') status=$( (echo "password ${PASS}";echo "status";echo "close") | nc ${HOST} ${PORT} ) song=$( echo "$status" | grep ^song: | sed -re 's/song: //' ) bitrate=$( echo "$status" | grep ^bitrate: | sed -re 's/bitrate: //' ) songinfo=$( (echo "password ${PASS}";echo "playlistinfo ${song}";echo "close") | nc ${HOST} ${PORT} ) artist=$( echo "$songinfo" | grep ^Artist: | sed -re 's/^Artist: //' ) title=$( echo "$songinfo" | grep ^Title: | sed -re 's/^Title: //' ) album=$( echo "$songinfo" | grep ^Album: | sed -re 's/^Album: //' ) file=$( echo "$songinfo" | grep -i ^File: | sed -re 's/^File: //i' ) filename=$( basename "${file}" ) filedir=$( dirname "${file}" ) time=$( echo "$status" | grep ^time: | sed -re 's/^time: //' ) ctime=$( echo $time | sed -re 's/:[0-9]+$//' ) ttime=$( echo $time | sed -re 's/^[0-9]+://' ) function mktime { in=$1 min=$(printf "%02.0f" $( echo "${in}/60" | bc -l)); minc=$(printf "%.0f" $( echo "${in}/60" | bc -l)); sec=$(printf "%02.0f" $( echo "${in}-(60*${minc})" | bc -l)); if([[ "$sec" < 0 ]]) then min=$( echo "${min} - 1" | bc -l ) sec=$( echo "60 + ${sec}" | bc -l ) fi printf "%s:%s\n" "$min" "$sec" } currtime=$( mktime $ctime ) totaltime=$( mktime $ttime ) if ([[ -z "$title" ]]) && ([[ -z "$artist" ]]) && ([[ -z "$album" ]]) then printf "np: ${b}%s${b} in ${b}%s${b} [${b}%s${b}/${b}%s${b}|${b}%sKbps${b}]\n" "$filename" "$filedir" "${currtime}" "${totaltime}" "${bitrate}" elif ([[ -z "$artist" ]]) || ([[ -z "$title" ]]) then printf "np: ${b}%s${b} in ${b}%s${b} [${b}%s${b}/${b}%s${b}|${b}%sKbps${b}]\n" "$filename" "$filedir" "${currtime}" "${totaltime}" "${bitrate}" elif ([[ -z "$album" ]]) && ([[ -n "$title" ]]) && ([[ -n "$artist" ]]) then printf "np: ${b}%s${b} by ${b}%s${b} in ${b}%s${b} [${b}%s${b}/${b}%s${b}|${b}%sKbps${b}]\n" "$title" "$artist" "$filedir" "${currtime}" "${totaltime}" "${bitrate}" elif ([[ -n "$title" ]]) && ([[ -n "$artist" ]]) && ([[ -n "$album" ]]) then printf "np: ${b}%s${b} by ${b}%s${b} from ${b}%s${b} [${b}%s${b}/${b}%s${b}|${b}%skbps${b}]\n" "$title" "$artist" "$album" "${currtime}" "${totaltime}" "${bitrate}" fireadnotes
#!/bin/bash for i in nano.save* do (echo $i;echo ---------------;cat "$i";echo ---------------;) | less echo -n "Remove this file? [y/N]: " read ans if([[ "$ans" = "y" ]]) then rm "$i" fi donesys
#!/bin/bash SEP=' ' BOLD=$(perl -e 'print chr(2);') cpu=$(cat /proc/cpuinfo|grep "model name"|sed -re 's/model\s+name\s+:\s+(.*?)\s/\1/'|uniq|head -n 1) upt=$(uptime|sed -re "s/\s*([0-9]+:[0-9]+:[0-9]+)\s+\w+\s+([0-9]+:[0-9]+|[0-9]+\s+\w+,\s+[0-9:]+|[0-9]+\s+\w+),\s+([0-9]+)\susers,\s+load\saverage:\s(.*?)\s*/[Uptime:(${BOLD}\2${BOLD})]${SEP}[Load Avg:(${BOLD}\4${BOLD})]${SEP}[Users:(${BOLD}\3${BOLD})]/") memfree=$(cat /proc/meminfo|grep MemFree|sed -re 's/MemFree:\s+([0-9]+)\s+kB\s*/\1/') memtotal=$(cat /proc/meminfo|grep MemTotal|sed -re 's/MemTotal:\s+([0-9]+)\s+kB\s*/\1/') hdd=$(df -h|grep -Ee '^/dev/(sd|hd)'|sed -re 's/\s+/ /g'|sed -re "s/^\s*(.*?)\s(.*?)\s(.*?)\s(.*?)\s(.*?)\s(.*?)\s*$/${SEP}[${BOLD}\1${BOLD}->${BOLD}\6${BOLD}:(${BOLD}\3${BOLD}\/${BOLD}\2${BOLD})(${BOLD}\5${BOLD})]${SEP}/") mem=$(printf "${SEP}[Mem:(${BOLD}%smB${BOLD}/${BOLD}%smB${BOLD})(${BOLD}%s${BOLD})]" "$(echo "scale=1;(${memtotal}-${memfree})/1024"|bc -l)" "$(echo "scale=1;${memtotal}/1024"|bc -l)" "$(echo "scale=1;((${memtotal}-${memfree})/${memtotal})*100"|bc -l)%") printf "%s%s%s%s%s%s" \ "[Host:(${BOLD}${HOSTNAME}${BOLD})]${SEP}" \ "[OS:(${BOLD}$(uname -o), $(uname -mr)${BOLD})]${SEP}" \ "$(echo "[CPU:(${BOLD}${cpu}${BOLD})]${SEP}"|sed -re 's/\s+/ /g')" \ "${upt}" \ "${mem}" \ "${hdd}"sizes
#!/usr/bin/env perl # Formats output of du. # # example: $ du -sc * | sort -n | perl -e sizes # # output: # # 104.0000 kb rexima-1.4 # 101.3994 mb tremulous-1.1.0-installer.x86.run # 1.1494 mb wrar37b3.exe # 1.1041 gb arch # # written by spyro_boy / use warnings; use strict; while(my $num=<STDIN>) { my $i=0; # add more, I'm too lazy to search up all the acronyms my @units = ("b","kb","mb","gb","tb"); if(my @m=($num =~ m/^([0-9]+)\s+(.+)/)) { my $size=$m[0]; do { $size = $size/1024; $i++; } while($size>1); $size = $size*1024; printf "%10.4f %s %s\n",$size,$units[$i],$m[1]; } }tinyurl
echo "TinyURLing: ${1}" echo -n "TinyURL : " wget -q -O - http://tinyurl.com//create.php?url=${1}|\ grep -iEe "block.*tinyurl"|sed -re 's/.*(tinyurl.com\/\w+).*/\1/g'|head -n 1volume
if [[ -z "$1" ]] then vol=100 else vol=$1 fi vols=($(amixer -c 0 sget PCM|grep -iEe '^\s*Front\s(Left|Right)'|sed -re 's/^\s*Front\s(Left|Right):\s\w+\s[0-9]+\s\[([0-9]+)%\]/\2/')); amixer -c 0 sset PCM $[${vols[0]}+${vol}]%,$[${vols[1]}+${vol}]% > /dev/nullytget
/home/spyroboy/hacks/programming/bash/mkscreenshot.bash
/home/spyroboy/hacks/programming/bash/ripdrm.sh
if([ "$1" != "" ]) then dir=$1 else dir=cdrip_$(date +"%j_%H_%M_%S_%Y"); fi prevdir=$(pwd) mkdir ${dir} cd ${dir} for i in $(seq 1 $(echo QUIT | mplayer -quiet -slave cdda:// 2> /dev/null | grep 'Found.*tracks' | sed -e 's/[^0-9]//g')) do echo -n ripping track $i.. mplayer cdda://${i} -quiet -cache 8888 -ao pcm:file=track_${i}.wav &> /dev/null echo done. done if([ "$2" = "ogg" ]) then oggenc -q 10 track_*.wav fi if([ "$2" = "mp3" ]) then lame --preset insane *.wav fi ## I think this needs to be in a for loop; oh-well cd ${prevdir}/home/spyroboy/hacks/programming/bash/ripcd.bash
ERR... use these at your own damn risk! Some of these are probably disfunctional and incomplete.
*cough* yeah, I write a script for anything.
I have a lot more but they're more or less useless.
This is the script I use to find the sum total size of my server backups:
srvbksize.sh
#!/bin/bash # # Server Backup Size - srvbksize.sh # # Iestyn Pryce # 25/04/07 IFS=' ' PATH=/usr/local/bin:/bin:/usr/bin export PATH UMASK=002 umask $UMASK PROGRAM=srvbksize.sh DIR=~/backups total=0 for f in `ls -al $DIR | grep -v "\-backup-" | fgrep backup- | sed s/' '/#/g |sed s/###/#/g | sed s/##/#/g | cut -d'#' -f 5 ` do total=$(expr $total + $f); done # Make human readable as MB # remainder=`expr $total % 1048576` total=`expr $total / 1048576` echo $total.$remainder MB is the sum total of your server backupsI need to reorganize my scripts directory. I've also updated a lot of them.. mm I'll get around to reposting them[, in a better fashion too].