You are not logged in.

#1451 2011-04-23 05:08:08

firecat53
Member
From: Sammamish, Wa
Registered: 2007-05-14
Posts: 1,447
Website

Re: Post your handy self made command line utilities

This script run from cron keeps a git tree (my vimwiki files, in this case) synchronized with other machines. You will need to setup a bare git repo (I setup my repo on my own server) and push whatever files you want to track to it. Then put the script and setup the cron entries on whatever machines you want to keep in sync. Adjust variables to taste. It's not really designed for multiple people making changes, but more for one person using it on multiple machines (home, work, laptop, etc.). This is sort of like a poor-man's dvcs-autosync smile I use keychain for my ssh keys, but you could source ssh-agent too. This is needed to run from cron!

#!/bin/sh
type -P keychain &>/dev/null || { echo "I require keychain but it's not installed.  Aborting." >&2; exit 1; }
eval `keychain --noask --eval --agents ssh id_rsa` || exit 1
local="$HOME/.local/share/vimwiki"
remote="$s""/srv/git/vimwiki"
ssh_name="homeserver"
h=`hostname`
if [ "$h" = "$ssh_name" ]; then
        s=""
else
        s="$ssh_name"":"
fi
status=`ping -qc1 google.com 2> /dev/null`
if [ -n "$status" ]; then
        cd "$local"
        git add .
        git commit -a -m 'Autocommit'
        git pull
        git push
else
        exit 11  # I think this is the cron exit code to try again (for no network connection)
fi

Cron entry. Adjust frequency and retry frequency as desired.

* * * * * ID=vimwiki FREQ=15m/5m /home/firecat53/scripts/vimwiki_sync.sh >& /dev/null

Scott

Last edited by firecat53 (2011-04-23 05:09:17)

Offline

#1452 2011-05-06 15:38:23

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Post your handy self made command line utilities

As requested, lists just the application name of all apps using the network. Can be tweaked to include other info - set the contents of the array to any fields you wish to display and pass them to the print statement:

#!/bin/bash

lsof -nPi | awk '/.*python[2]*/ || /.*java/ {cmd[$2] = $2;next}
                 !/COMMAND/ {cmd[$1]=$1}
                 END {for (item in cmd ) {
                          sub(".*/", "", item)
                         print item}
                     }'

Also could be changed to call lsof from within awk and make it an awk script. Not sure if there is much to be gained from that.

See here: https://bbs.archlinux.org/viewtopic.php?id=118270


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#1453 2011-05-07 01:28:42

Lite
Member
Registered: 2009-07-09
Posts: 6

Re: Post your handy self made command line utilities

This is a simple real-time bandwidth monitoring script my friend wrote this morning: http://d.gfax.ch/scripts/ifpy

Examples:
lite@Hubble:~$ ./ifpy eth0
RX:11.05KB/s TX:135.12KB/s

updates every .5 seconds and averages 3 seconds worth of rates:
lite@Hubble:~$ ./ifpy -i.5 -s3 eth0
RX:11.22KB/s TX:118.49KB/s

Use -h for more output options.

#!/usr/bin/python
import sys,time,collections,os,signal
argc=len(sys.argv)
tmppath="/tmp/"
def printusage():
    print("""ifpy - show interface transfer rates with moving average
        usage: ifpy -[cnmx] [-i interval] [-s seconds] [-a samples] <interface>
        flags:
            -c : turn off color
            -n : adds newline at end of lines
            -m : csv output 
            -i <seconds> : interval between samples/updates (can be <1)
            -s <seconds> : seconds of samples to average
            -a <samples> : number of samples to average
            -x : output one value and exit uses /tmp/ifpytmp to store the previous sample
            -S : output for use in screen hardstatus
            -w <widht> : constant width of <width> digits left of decimal
            default is -i 1 -s 5
        Created by oddbondboris report bugs to obbspam@gmail.com
            """)

def c_c(a,b):
        print()
        sys.exit()
signal.signal(signal.SIGINT, c_c)
def colored(txt,color,screen):
    cnames=[("red"),("green"),("yellow"),("blue"),("magenta"),("cyan")]
    if screen==0:
        ccodes=[("\033[31m"),("\033[32m"),("\033[33m"),("\033[34m"),("\033[35m"),("\033[36m")]
    else:
        ccodes=[("\005{r}"),("\005{g}"),("\005{y}"),("\005{b}"),("\005{m}"),("\005{c}")]

    try:
        if screen==0:
            return "%s%s%s"%(ccodes[cnames.index(color)],txt,"\033[0m")
        else:
            return "%s%s%s"%(ccodes[cnames.index(color)],txt,"")
    except:
        return txt
if argc<2:
    print("error: no interface given")
    printusage()
    sys.exit()
args=["iface","i","a","s","h","c","m","n","x","S","w"]
argvals=["",1.0,5,0,-1,-1,-1,-1,-1,-1,0]
i=0
while i<argc:
    carg=sys.argv[i]
    clen=len(carg)
    if carg[0]=="-":
        argflag=carg[1:2]
        try:
            argindex=args.index(argflag)
        except:
            print("%s is not a valid flag"%argflag)
            printusage()
            sys.exit()
        if argvals[argindex]==-1:
            argval=1
            if clen>2:
                argflag2=carg[2:3]
                try:
                    argindex2=args.index(argflag2)
                except:
                    print("%s is not a valid flag"%argflag2)
                    printusage()
                    sys.exit()
                argvals[argindex2]=1
                if clen>3:
                    argflag3=carg[3:4]
                    try:
                        argindex3=args.index(argflag3)
                    except:
                        print("%s is not a valid flag"%argflag3)
                        printusage()
                        sys.exit()
                    argvals[argindex3]=1

                    
                    
        else:
            if clen==2:
                try:
                    argval=float(sys.argv[i+1])
                    i=i+1
                except ValueError:
                    print("error: -%s requires a numeric flag"%argflag)
                    sys.exit()
                except IndexError:
                    print("error: -%s requires a flag"%argflag)
                    sys.exit()
            else:
                try:
                    argval=float(carg[2:clen])
                except ValueError:
                   print("error: -%s requires a numeric flag"%argflag)
                   sys.exit()
                except IndexError:
                   print("error: -%s requires a flag"%argflag)
                   sys.exit()
        argvals[argindex]=argval
    else:
        argvals[0]=sys.argv[i]
    i=i+1
if argvals[0]=="":
    print("need an interface")
    sys.exit()
interface=argvals[0]
interval=argvals[1]
avg=int(argvals[2])
seconds=argvals[3]
if argvals[4]==1:
    printusage()
    sys.exit()

running=1
sol="\r"
rxstart=""
rxformat="RX:%.2fKB/s"
rxcolor='green'
seperator=" "
txstart=""
txformat="TX:%.2fKB/s"
txcolor='red'
eol="    "
tmp=0
screen=0
flen=int(argvals[10]+6)
if argvals[5]==1:
    rxcolor=''
    txcolor=''
if argvals[6]==1:
    rxformat="%f"
    txformat="%f"
    rxcolor=''
    txcolor=''
    seperator=","
    flen=0
    sol=""
    eol="\n"
if argvals[7]==1:
    sol=""
    eol="\n"
if argvals[8]==1:
    rxstart="D"
    txstart="U"
#    rxstart=u'\u2193'
#    txstart=u'\u2191'
    rxformat=":%.2fK"
    rxcolor='green'
    seperator=" "
    txformat=":%.2fK"
    txcolor='red'
    tmp=1
    oneshot=1
    flen=17
    eol="\n"
else:
    oneshot=0
if argvals[9]==1:
#    rxstart=u'\u2193'
#    txstart=u'\u2191'
#    rxstart="\\/"
#    txstart="/\\"
    rxstart="D"
    txstart="U"
    rxformat=":%.2fK"
    rxcolor='green'
    seperator=" "
    txformat=":%.2fK"
    txcolor='red'
    eol="\n"
    screen=1


if seconds!=0:
    avg=round(seconds/interval)
avg=avg+1
ifpath="/sys/class/net/%s/statistics/"%interface
try:
    f=open("%s../uevent"%ifpath,'r')
    f.close()
except:
    print("can't find interface %s "%interface)
    printusage()
    sys.exit()
txfile=open("%stx_bytes"%ifpath,'r')
rxfile=open("%srx_bytes"%ifpath,'r')
rxscale=1000
txscale=1000
txbuf=collections.deque('',avg)
rxbuf=collections.deque('',avg)
if tmp==0:
    nti=time.time()
    txbuf.appendleft((int(txfile.read()),nti))
    rxbuf.appendleft((int(rxfile.read()),nti))
else:
    try:
        tfile=open("%s/ifpytmp"%tmppath,'r+')
    except IOError as e:
        if e.code()==13:
            print ("bad temp permissions")
        else:
            print ("error %s"%e.code())
        sys.exit()
    else:
        tf=tfile.read().split(",")
        if len(tf)==1:
            nti=time.time()
            txbuf.appendleft((int(txfile.read()),nti))
            rxbuf.appendleft((int(rxfile.read()),nti))
        elif len(tf)==3:
            nti=int(tf[0])
            txbuf.appendleft((int(tf[1]),nti))
            rxbuf.appendleft((int(tf[2]),nti))




while running>0:
    if tmp==0:
        time.sleep(interval)
    rxfile.seek(0)
    txfile.seek(0)
    nti=time.time()
    newtx=int(txfile.read())
    newrx=int(rxfile.read())
    txbuf.appendleft((newtx,nti))
    rxbuf.appendleft((newrx,nti))
    oldtx=txbuf[-1]
    oldrx=rxbuf[-1]
    txavg=(newtx-oldtx[0])/(nti-oldtx[1])/txscale
    rxavg=(newrx-oldrx[0])/(nti-oldrx[1])/rxscale
    #sys.stdout.write((u"%s%s%s%s%s"%(sol,colored(u"%s%s"%(rxstart,rxformat%rxavg),rxcolor,screen),seperator,colored(u"%s%s"%(txstart,txformat%txavg),txcolor,screen),eol)).ljust(flen))
    sys.stdout.write(("%s%s%s%s%s"%(sol,colored(("%s%s"%(rxstart,rxformat%rxavg)).ljust(flen),rxcolor,screen),seperator,colored(("%s%s"%(txstart,txformat%txavg)).ljust(flen),txcolor,screen),eol)).ljust(flen))
    sys.stdout.flush()
    running=running-oneshot


if tmp==1:
    tfile.seek(0)
    nti=time.time()
    tfile.write("%i,%i,%i"%(nti,newtx,newrx))

Last edited by Lite (2011-07-17 23:24:53)

Offline

#1454 2011-05-10 20:12:49

usr
Member
Registered: 2006-07-26
Posts: 20

Re: Post your handy self made command line utilities

Here's a nice one I just put together for watching flash movies with mplayer. I used to like grabbing them from /tmp/Flash* but they now delete them immediately (http://www.elfsternberg.com/2010/11/15/ … ash-files/) so you can't do that any more.

It's not very pretty. I can't work out how to get it to run the mplayer's sequentially so all flash videos will start together. pause the ones you don't need. For some reason after the first mplayer quits the for loop will also quit without starting the next one so they are started in the background to avoid this.

Just save it somewhere and run it. It may take a while before anything happens - lsof seems to take varying amounts of time.

#!/bin/bash

lsof -bw | grep Flash | while read line
do
  echo $line
  DIR=$(echo $line | cut -f2 -d \ ) #needs this space

  FILE=$(echo $line | cut -f4 -d \ )

  FILE=$(echo $FILE | sed -e 's/u//g' ) 

  cd /proc/$DIR/fd/
  
  mplayer $FILE &
  echo next file
done  # < /tmp/flash
echo Done

Do you ever get that "Oops" feeling?

Offline

#1455 2011-05-11 05:15:40

vadmium
Member
Registered: 2010-11-02
Posts: 63

Re: Post your handy self made command line utilities

Here’s an initcpio hook I made to run a Linux file system from a subdirectory rather than the root directory of a partition. It’s not precisely a command-line utility but I thought I’d post it anyway. The key to it is the “mount --bind” command. With this I can have many OSes installed without needing a separate partition for each of them.

/lib/initcpio/hooks/dir
run_hook ()
{
    echo "Root FS is in: $dir"
    
    dir_pre_mount_handler="$mount_handler"
    mount_handler=dir_mount_handler
}

dir_mount_handler () {
    $dir_pre_mount_handler "$1"
    mount --bind "$1/$dir" "$1"
}
/lib/initcpio/install/dir
install ()
{
    MODULES=""
    BINARIES=""
    FILES=""
    SCRIPT="dir"
}

help ()
{
cat<<HELP
  Mounts a subdirectory of the filesystem as the actual root filesystem. Use
  kernel parameter "dir" to specify the subdirectory. A leading slash (/) is
  allowed but not necessary.
  
  Example:
  dir=os/arch
HELP
}

To use it I added “dir” to HOOKS in /etc/mkinitcpio.conf, and added a dir= option to the kernel command line in /boot/grub/menu.lst. The path is relative to the file system of the “root” device. For example I have:

kernel /vmlinuz26 root=/dev/disk/by-label/cross dir=os/arch ro

It’s also useful to mount the whole partition somewhere as well using /etc/fstab, although this means all the files under the mounted OS directory will be visible under both mount points. I haven't had any big problems with this; although some programs (find and du) complain about a circular file system loop.

Offline

#1456 2011-05-11 09:19:36

quigybo
Member
Registered: 2009-01-15
Posts: 220

Re: Post your handy self made command line utilities

@usr: see this thread

Offline

#1457 2011-05-21 22:19:56

hauzer
Member
From: Belgrade, Serbia
Registered: 2010-11-17
Posts: 279
Website

Re: Post your handy self made command line utilities

Well, not exactly command line, but here it is anyway. Not really usefull because there are tons of stuff like this, just did it for fun (TM). smile It's a clock written in bash using urxvt. Use your WM's trickery to position it somewhere on the screen. It doesn't reserve space.

urxvt-clock

#!/bin/bash

# Set up the variables
#
# Format as used by 'date'
if [ ! -n "${URXVT_CLOCK_FMT}" ]; then
    export URXVT_CLOCK_FMT='%a %b %d %H:%M:%S'
fi
# Update interval, useful if one wants to, for example, remove seconds from the clock
if [ ! -n "${URXVT_CLOCK_SLEEP}" ]; then
    export URXVT_CLOCK_SLEEP=1
fi
if [ ! -n "${URXVT_CLOCK_BG_COLOR}" ]; then
    URXVT_CLOCK_BG_COLOR=black
fi
if [ ! -n "${URXVT_CLOCK_FG_COLOR}" ]; then
    URXVT_CLOCK_FG_COLOR=white
fi
if [ ! -n "${URXVT_CLOCK_FONT}" ]; then
    URXVT_CLOCK_FONT=xft:monospace:bold:size=9
fi

# The length of the whole string reproduced by 'date ${fmt}' so we can set the geometry accordingly
URXVT_CLOCK_DATE_LEN=`echo -n \`date "+\${URXVT_CLOCK_FMT}"\``; URXVT_CLOCK_DATE_LEN=${#URXVT_CLOCK_DATE_LEN}

urxvt -depth 32 -geometry $((${URXVT_CLOCK_DATE_LEN}+1))x1 -bg ${URXVT_CLOCK_BG_COLOR} -fg ${URXVT_CLOCK_FG_COLOR} +sb +bc +uc -sl 0 -bl -title urxvt-clock -name urxvt-clock -font ${URXVT_CLOCK_FONT} -e urxvt-clock.sh

urxvt-clock.sh

#!/bin/bash

# The escape sequence is used to remove the console cursor
# Without the echo in the end, printed dates would stack up one besides the other
while true ; do
    echo -ne "\033[?25l"`date +"${URXVT_CLOCK_FMT}"`
    sleep ${URXVT_CLOCK_SLEEP}
    echo
done

OpenBox configuration:

  <!-- urxvt-clock -->
  <application name="urxvt-clock"
               class="URxvt"
               type="normal">
    <decor>no</decor>
    <shade>no</shade>
    <position force="yes">
        <x>0.1</x>
        <y>-0.1</y>
    </position>
    <focus>no</focus>
    <desktop>all</desktop>
    <layer>above</layer>
    <iconic>no</iconic>
    <skip_pager>yes</skip_pager>
    <skip_taskbar>yes</skip_taskbar>
    <fullscreen>no</fullscreen>
    <maximized>false</maximized>
  </application>

Note that urxvt tries to undecorate the window regardless of WM.

Last edited by hauzer (2011-05-21 22:24:24)


Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
What profit hath a man of all his labour which he taketh under the sun?
All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

Offline

#1458 2011-05-22 18:56:18

jac
Member
From: /home/jac
Registered: 2009-05-19
Posts: 431
Website

Re: Post your handy self made command line utilities

hauzer, I like the idea. This makes me want to get real transparency working again.

Consider using "-z" (empty) instead of "! -n" (not not empty). Or, possibly better, have you heard of bash's default substitution syntax? "${var-default}" evaluates to the value of var if it has something in it, otherwise it is "default". Since you're using bash anyway, you should consider using $() for command execution, they nest without needing any trickery. Any reason why you chose a while true loop over watch (with -t and -n)?

Edit: Take a look at this page on parameter substitution if you'd like to learn more about some of the neat substitution stuff.

Last edited by jac (2011-05-22 19:01:30)

Offline

#1459 2011-05-22 19:59:02

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Post your handy self made command line utilities

ripcd.sh

#!/bin/zsh

device=$1
track=1
shift

while [[ $1 != "" ]]; do
    if [[ $track -gt 9 ]]; then
        filename="$track. $1.wav"
    else
        filename="0$track. $1.wav"
    fi

    cdda2wav -D $device -t $track $filename
    flac $filename

    track=$[$track + 1]
    shift
done

rm *.inf *.wav

Call it by `ripcd.sh /dev/whatever "name 1" "name 2" ... "name n"` and it will rip them all to FLAC. I've been considering adding some tagging functionality to it (it could do album name, track numbering, and track names).

Last edited by Barrucadu (2011-05-22 20:01:38)

Offline

#1460 2011-05-23 03:10:00

hauzer
Member
From: Belgrade, Serbia
Registered: 2010-11-17
Posts: 279
Website

Re: Post your handy self made command line utilities

jac wrote:

hauzer, I like the idea. This makes me want to get real transparency working again.

Consider using "-z" (empty) instead of "! -n" (not not empty). Or, possibly better, have you heard of bash's default substitution syntax? "${var-default}" evaluates to the value of var if it has something in it, otherwise it is "default". Since you're using bash anyway, you should consider using $() for command execution, they nest without needing any trickery. Any reason why you chose a while true loop over watch (with -t and -n)?

Edit: Take a look at this page on parameter substitution if you'd like to learn more about some of the neat substitution stuff.

Hey, thanks for all the tips! big_smile I'm quite a bash newbie, just knowing things I need in a specific situation. The variable substitution is great! I thought that bash couldn't ever possibly not be ugly. smile Of course, I never knew of $(), makes things easy. The reason I'm not using watch is because it can't interpret escape sequences. Look under BUGS in the manpage.

urxvt-clock

#!/bin/bash

# Set up the variables
#
# Format as used by 'date'
export URXVT_CLOCK_FMT=${URXVT_CLOCK_FMT-'%a %b %d %H:%M:%S'}
# Update interval, useful if one wants to for example, remove seconds from the clock
export URXVT_CLOCK_SLEEP=${URXVT_CLOCK_SLEEP-1}
URXVT_CLOCK_BG_COLOR=${URXVT_CLOCK_BG_COLOR-black}
URXVT_CLOCK_FG_COLOR=${URXVT_CLOCK_FG_COLOR-white}
URXVT_CLOCK_FONT=${URXVT_CLOCK_FONT-xft:monospace:bold:size=9}
# The length of the whole string reproduced by 'date ${fmt}' so we can set the geometry accordingly
URXVT_CLOCK_DATE_LEN=$(echo -n $(date "+${URXVT_CLOCK_FMT}")) ; URXVT_CLOCK_DATE_LEN=${#URXVT_CLOCK_DATE_LEN}

urxvt -depth 32 -geometry $((${URXVT_CLOCK_DATE_LEN}+1))x1 -bg ${URXVT_CLOCK_BG_COLOR} \
      -fg ${URXVT_CLOCK_FG_COLOR} +sb +bc +uc -sl 0 -bl -title urxvt-clock -name urxvt-clock \
      -font ${URXVT_CLOCK_FONT} -e urxvt-clock.sh

urxvt-clock.sh

#!/bin/bash

# The escape sequence is used to remove the console cursor
# Without the echo in the end, printed dates would stack up one besides the other
while true ; do
    echo -ne "\033[?25l"$(date +"${URXVT_CLOCK_FMT}")
    sleep ${URXVT_CLOCK_SLEEP}
    echo
done

Last edited by hauzer (2011-05-23 03:10:36)


Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
What profit hath a man of all his labour which he taketh under the sun?
All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

Offline

#1461 2011-05-25 07:18:24

dmz
Member
Registered: 2008-08-27
Posts: 866
Website

Re: Post your handy self made command line utilities

If you hide the cursor, make sure you show it again, or else people like me will be very angry.

I do not really understand why you want to spam the terminal with the dates... why not just show it once and let that single instance update itself as time passes?

perl -e '$SIG{INT} = sub { printf("\e[?25h"); exit }; printf("\e[?25l"); while(1) { printf("%s\r", scalar localtime()); }'

Offline

#1462 2011-05-25 07:23:12

karol
Archivist
Registered: 2009-05-06
Posts: 25,430

Re: Post your handy self made command line utilities

A thread about a bash clock https://bbs.archlinux.org/viewtopic.php?id=105693

Last edited by karol (2011-05-25 07:23:40)

Offline

#1463 2011-05-26 23:53:55

cyrusza
Member
From: Italy
Registered: 2011-05-18
Posts: 29
Website

Re: Post your handy self made command line utilities

The description is in the code, i wrote it to right-click&copy some links and create a download list.

#!/bin/bash
# AUTHOR: cyrus
# TITLE: clipredir
# DATE: 8 dec 2010
# DESCRIPTION:
#   Check for clipboard change and append data into the provided file.
#   Must be interrupted with CTRL+C. Default interpoll time is 0,05 s.
#   Require "xsel".
# USAGE: clipredir <path-to-file>
 
if [ -z "$1" -o -n "$2" ]; then
    echo "usage: clipredir <path-to-file>"
    exit 1
fi
 
oldclip=`xsel -b -o`
while true; do
    sleep 0.05    # interpoll time
    newclip=`xsel -b -o`
    if [ "$oldclip" !=  "$newclip" ]; then
        xsel -b -o  >> $1
        echo >> $1
        oldclip=$newclip
    fi
done

Last edited by cyrusza (2011-05-26 23:55:43)

Offline

#1464 2011-05-27 22:08:56

cyrusza
Member
From: Italy
Registered: 2011-05-18
Posts: 29
Website

Re: Post your handy self made command line utilities

Another little script i use in my Debian server as a weekly cron job. It downloads a rich adblock list and changes it in a DNSmasq compatible format. Remember to add the following configuration line into your "dnsmasq.conf"

conf-file=/etc/adblock-dnsmasq.conf
#|/bin/bash
# mvps adlist downloader for DNSmasq

# Download the adblock list
wget -q -O /tmp/adlist-tmp  http://winhelp2002.mvps.org/hosts.txt 

# Skip the first 1352 bytes and format the list for DNSmasq
dd ibs=1352 skip=1 if=/tmp/adlist-tmp | awk '/127.0.0.1/ { sub("\r$",""); if($1 == "127.0.0.1") print "address=/"$2"/"$1 }' > /etc/adblock-dnsmasq.conf

# Restart DNSmasq
/etc/init.d/dnsmasq restart

Offline

#1465 2011-05-29 13:37:49

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 785
Website

Re: Post your handy self made command line utilities

BASH function for looking up syscall numbers. Argument is partial or complete syscall name. No argument returns all syscall numbers. Adjust as required for 32 bit systems.

syscall() {
    grep "#define __NR_$1" /usr/include/asm/unistd_64.h | \
        sed -e 's/.*__NR_//' | column -t
}

Edit: Remove unnecessary sed expression.

Last edited by mikesd (2011-05-30 00:08:52)

Offline

#1466 2011-05-29 16:46:01

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,095
Website

Re: Post your handy self made command line utilities

Neat! I did something similar with errno's:

#!/bin/bash
#
# tool to convert symbols like EPERM to their numeric values and vice versa
#

shopt -s extglob

base=/usr/include/asm-generic/errno-base.h
upper=/usr/include/asm-generic/errno.h

exec 0< <(cat "$base" "$upper")

numeric_to_symbol() {
  awk '/^#define/ && $3 == '"$1"' { printf "%s = '$1'\n",$2; exit 1; }'
}

symbol_to_numeric() {
  awk '/^#define/ && $2 == "'"$1"'" { printf "'$1' = %s\n",$3; exit 1; }'
}

perror() {
  printf '#include <errno.h>
    #include <string.h>
    int main(void) { errno = %s; perror("message"); return 0; }
    ' "$1" | gcc -x c - -o /tmp/a.out; [[ -x /tmp/a.out ]] && /tmp/a.out; rm -f /tmp/a.out
}

[[ $1 ]] || { printf 'usage: %s [symbol|errno]\n' "${0##*/}"; exit 1; }>&2

declare -u arg=$1

# if its numeric, try to get the symbol
case "$arg" in
  +([[:digit:]])) numeric_to_symbol "$arg" ;;
  +([[:upper:]])) symbol_to_numeric "$arg" ;;
  *) printf 'unknown input: %s\n'>&2; exit 1 ;;
esac

# if successful (awk returns error) print the error message
(( $? )) && perror "$arg"

Accepts a numerical code and resolves the ECODE, or takes an ECODE and returns the digit. In both cases, you also get the human readable error string from strerror or perror.

Last edited by falconindy (2011-05-29 16:47:22)

Offline

#1467 2011-05-30 18:55:43

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Post your handy self made command line utilities

Basically the first Bash script I ever wrote (besides simple launchers). I have this launched from my /etc/rc.local.shutdown script. I also use it from /usr/bin with dmenu to quickly purge those files. If you have any tips or anything let me know~ I am sure there are 300 things wrong with it. tongue

# Script to check and remove trash files if they exist
#
#!/bin/bash

user="username"

filecount=`ls -A /home/${user}/.local/share/Trash/files/ | wc -l`

echo "Checking for trash files."
if [ $filecount -gt 0 ];
  then
    rm -rf /home/${user}/.local/share/Trash/files/*
    echo "$filecount trash files deleted."
  else
    echo "Trash files didn't exist."
fi

filecount=`ls -A /home/${user}/.local/share/Trash/info/ | wc -l`

echo "Checking for info files"
if [ $filecount -gt 0 ];
  then
    rm -rf /home/${user}/.local/share/Trash/info/*
    echo "$filecount info files deleted."
  else
    echo "Info files didn't exist."
fi

Last edited by Google (2011-05-30 19:07:01)

Offline

#1468 2011-06-03 01:09:52

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 5,363

Re: Post your handy self made command line utilities

#!/bin/bash
MPDHOST="localhost"
mpc -q -h $MPDHOST clear ; mpc -h $MPDHOST findadd album "$(mpc -h $MPDHOST list album | shuf -n 1)"; mpc -q -h $MPDHOST play

Play a random album in mpd, using mpc. Unfortunately, if you have two albums with identical names, only one of them will ever get played using this script. Haven't tested that, but logic suggests that would be the case.

Last edited by WorMzy (2011-06-03 01:10:07)


Sakura:-
Mobo: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Offline

#1469 2011-06-12 20:53:30

bloom
Member
Registered: 2010-08-18
Posts: 749
Website

Re: Post your handy self made command line utilities

A small script to change your screen brightness.
(The first path must be adapted.)

#! /bin/sh

SYSFS_BACKLIGHT=/sys/class/backlight/apple_backlight
BRIGHTNESS_INTERFACE=$SYSFS_BACKLIGHT/brightness
ACTUAL_BRIGHTNESS_VALUE=$SYSFS_BACKLIGHT/actual_brightness

if [ $# -gt 1 ]
then
    echo "usage: $0 [[+|-]value]"
fi

if [ $# -eq 0 ]
then
    cat $ACTUAL_BRIGHTNESS_VALUE
else
    case $1 in
        -*|+*)
            actual_value=$(cat $ACTUAL_BRIGHTNESS_VALUE)
            new_value=$(($actual_value + $1))
            echo $new_value > $BRIGHTNESS_INTERFACE;;
        *)
            echo $1 > $BRIGHTNESS_INTERFACE;;
    esac
fi

Last edited by bloom (2011-06-13 07:53:06)


gh · da · ds · cr · ab · fkr

Offline

#1470 2011-06-13 02:39:09

thetrivialstuff
Member
Registered: 2006-05-10
Posts: 191

Re: Post your handy self made command line utilities

Three utilities along the same line:

md5all - recursively get the md5sums of every file a directory, sorted in a consistent order:

#!/bin/bash

if [ -n "$1" ]; then
    cd "$1"
fi

#force standard sorting order
LANG=C

find -type f -print0 | xargs -0 md5sum | sort -k2

Note: you can "md5all | md5sum" to get a single md5 for the whole directory tree. If it comes out the same on both machines, the directories are identical. Very quick way to check this across several machines.

md5listcompare.pl - compares two outputs from md5all and tells you which files are in both:

#!/usr/bin/perl

#compares two lists of files with md5sums, finding pairs of files which are
#in both lists (like a remote version of duff.pl, as the lists can be generated
#separately).
#
#usage: md5listcompare.pl <see if files in this list> <are in here>

($list1,$list2) = @ARGV;

%list1 = &makehash($list1);
%list2 = &makehash($list2);

foreach (keys %list1) {
    if ($list2{$_}) {
        push @matched , "$_\t$list1{$_}\tis\t$list2{$_}\there";
    }
    else {
        push @unmatched , "$_\t$list1{$_}";
    }
}

print "====== Matched files ====== ($list1)\t($list2)\n\n";
#the sort sorts on the second field (tab-separated)
print join "\n" , (sort { (split "\t" , $a)[1] cmp (split "\t" , $b)[1] }  @matched);
print "\n\n\n====== Unmatched files ====== in ($list1)\t but not in ($list2)\n\n";
print join "\n" , (sort { (split "\t" , $a)[1] cmp (split "\t" , $b)[1] }  @unmatched);
print "\n\n";


#takes: filename of a list of md5sum outputs
#returns: a hash of md5sum -> file
sub makehash {
    my %hash;

    $listfilename = shift @_;
    $listfilename =~ s/'/\\'/g;
    $list = `cat \$'$listfilename'`;

    @lines = split /\n/ , $list;

    foreach (@lines) {
        @line = split /  / , $_ , 2;
        $hash{$line[0]} = $line[1];
    }

    return %hash;
}

And duff.pl, a slightly modified reimplementation of the "duff" duplicate file finder. The original duff was meant for cleaning out duplicates; this one is more for making sure you've got a copies (i.e. "is it safe to delete this crap off my flash drive, or did I modify some documents on here and not copy them back to my desktop?"):

#!/usr/bin/perl

#a (reduced) re-implementation of duff, by Camilla Berglund
#(http://duff.sourceforge.net/).
#
#usage: [-q] duff.pl <dir1> <dir2>
#
#By Felix (http://skeena.net).
#
#This program is intended for re-integrating a copied batch of files back into
#its original archive. Somewhat like a synchronization program, but
#this works when filenames and directory locations have changed drastically.
#
#Example usage: I grab a select few utilities out of my software archive, dump
#them on my USB stick, then a month later want to delete them from there --
#am I *sure* none of them got accidentally moved instead of copied?
#
#The -q (quick) switch checks only the first MB of each file instead of
#md5'ing the whole thing. This is often sufficient for large, likely-to-be-unique
#files such as movies and CD images.

#check for the -q switch:
if ($ARGV[0] eq "-q") {
    $use_whole_file = 0;
    shift @ARGV;
}
else {
    $use_whole_file = 1;
}

($dir1,$dir2) = @ARGV;

@list1 = &makelist($dir1);
@list2 = &makelist($dir2);

%sizehash1 = &makehash(@list1);
%sizehash2 = &makehash(@list2);


#for each key (filesize) in listing 1, see if there's a file in
#listing 2 with the same size.
#
#for each entry, perform the cartesian product of comparisons; e.g.
#if in listing 1 there is:
#143329=>file1<tab>file2
#and in listing 2 there is:
#143329=>fileA<tab>fileB<tab>fileC
#
#then the comparisons to be done are:
#file1 with fileA, fileB, fileC
#file2 with fileA, fileB, fileC
$count = 0;
for $key (keys %sizehash1) {
    if (defined $sizehash2{$key}) {

        @group1 = split /\t/ , $sizehash1{$key};
        @group2 = split /\t/ , $sizehash2{$key};
        
        #get md5sums of all the files
        my %group1;
        foreach (@group1) {
            $group1{$_} = &md5sum($_);
        }
        my %group2;
        foreach (@group2) {
            $group2{$_} = &md5sum($_);
        }

        #compare md5sums
        foreach $one (@group1) {
            foreach $two (@group2) {
                if ($group1{$one} eq $group2{$two}) {
                    print "#duplicates; size = $key bytes , md5sum of ".
                        ($use_whole_file ? "entire file" : "first MB")." = {$group1{$one}}:\n".
                        "".$one."\n".
                        "# ==> ".$two."\n";
                    $count++;
                    #limit to one match, to keep the matched count accurate (otherwise,
                    #if there are two matches for one file in side1, the number of matches
                    #for side1 will increase by two)
                    last;
                }
            }
        }
    } #if
} #main for

print "#($count files in dir1 have matches in dir2)\n";


#takes: the path to a file
#returns: the md5sum of the first SAMPLE MB of the file
sub md5sum {
    $file = shift @_;
    $file =~ s/'/\\'/g;

    if ($use_whole_file) {
        return substr(`md5sum \$'$file'`,0,32);
    } else { #use first MB
        return substr(`dd if=\$'$file' bs=1M count=1 status=noxfer 2>/dev/null | md5sum`,0,32);
    }
}

#takes: the path to a directory
#returns: a list of directory entries formatted like
#filesize<tab>filename
sub makelist {
    $dir = shift @_;
    $dir =~ s/'/\\'/g;

    $list = `find \$'$dir' -type f -print0 | xargs -0 ls -l --time-style=long-iso`;

    $list =~ s/^([^ ]* +){4}\s*//mg;
    $list =~ s/^(\d+) [^ ]+ [^ ]+ /\1\t/mg;

    @list = split /\n/ , $list;

    return @list;
}

#takes: a list as formatted by makelist
#returns: a hash of entries like
#size => name1<tab>name2<tab>name3
sub makehash {
    my %sizehash;
    foreach (@_) {
        @line = split /\t/ , $_;
        if (defined $sizehash{$line[0]}) {
            $sizehash{$line[0]} .= "\t".$line[1];
        }
        else {
            $sizehash{$line[0]} = $line[1];
        }
    }

    return %sizehash;
}

Notes:
- It prints a count of matched files at the end. If the count is the same as the number of files in your "is it safe to delete this?" dir, you can safely delete the whole thing.
- If you don't have a complete match, you can subtract the files that are safe to delete by piping to "grep -v '^#'" to get the list and then sending that list to some delete command (it's not safe on files with newlines in their names, but that doesn't happen very often).

~Felix.

Last edited by thetrivialstuff (2011-06-13 03:16:58)

Offline

#1471 2011-06-16 13:55:48

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: Post your handy self made command line utilities

I'm quite sure there are tons of it like it, but this is my 'XKCD latest comic retriever'

Yeah, it's in python2 because that's what I use at work.

Yeah, part of my work involves retrieving latest XKCD smile

""" Retrieve latest XKCD in a nicely
named image

"""

import os
import json
import urllib

XKCD_JSON_URL = "http://xkcd.com/info.0.json"

def get_latest_xkcd(output_path):
    """ Fetch json information from xckd, then
    create an img with the correct name in
    output_path.

    Return the full path of the newly retrieved image

    """
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    url_obj = urllib.urlopen(XKCD_JSON_URL)
    data = url_obj.read()
    url_obj.close()
    xkcd_info = json.loads(data)
    img_url = xkcd_info["img"]
    title   = xkcd_info["safe_title"]

    output_img = os.path.join(output_path, title + ".png")
    urllib.urlretrieve(img_url, output_img)

    return output_img



def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("output_path", nargs="?")
    args = parser.parse_args()
    if not args.output_path:
        output_path = os.getcwd()
    output_img = get_latest_xkcd(output_path)
    print "Latest XKCD comic retrieved in", output_img


if __name__ == "__main__":
    main()

Offline

#1472 2011-06-19 17:37:10

nixfag
Member
From: Romania
Registered: 2011-03-03
Posts: 73

Re: Post your handy self made command line utilities

.zshrc function for setting tty colours:

ttycolours()
{
   if [[ $TERM == 'linux' && $# == 1 ]] ; then
    [[ -z $1 ]] && echo 'What colorscheme?'
    if [[ ! -f /home/derp/ttycolours/${1} ]] ; then
        echo "Can't find colorscheme $1"
        echo "Available colorschemes:"
        colorschemes=($(echo /home/derp/ttycolours/*))
        for colorscheme in ${colorschemes[@]} ; do
            echo ${colorscheme##*/}
        done | tr '\n' ' '
    elif [[ -f /home/derp/ttycolours/${1} ]] ; then
        . /home/derp/ttycolours/${1}
        echo -en "\e]P0${color0}"  #black
        echo -en "\e]P8${color8}"  #darkgrey
        echo -en "\e]P1${color1}"  #darkred
        echo -en "\e]P9${color9}"  #red
        echo -en "\e]P2${color2}"  #darkgreen
        echo -en "\e]PA${color10}" #green
        echo -en "\e]P3${color3}"  #brown
        echo -en "\e]PB${color11}" #yellow
        echo -en "\e]P4${color4}"  #darkblue
        echo -en "\e]PC${color12}" #blue
        echo -en "\e]P5${color5}"  #darkmagenta
        echo -en "\e]PD${color13}" #magenta
        echo -en "\e]P6${color6}"  #darkcyan
        echo -en "\e]PE${color14}" #cyan
        echo -en "\e]P7${color7}"  #lightgrey
        echo -en "\e]PF${color15}" #white
        clear # bring us back to default input colours
    fi
   fi
}

Offline

#1473 2011-06-19 17:42:15

dmz
Member
Registered: 2008-08-27
Posts: 866
Website

Re: Post your handy self made command line utilities

Offline

#1474 2011-06-19 17:45:10

nixfag
Member
From: Romania
Registered: 2011-03-03
Posts: 73

Re: Post your handy self made command line utilities

@dmz I know about that,I just like to have my own smile.It's fun to code shell scripts,easy as a pie.

Offline

#1475 2011-06-21 02:23:07

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: Post your handy self made command line utilities

Quick script I wrote earlier to handle Minecraft servers, though really it could be trivially adapted to manage any sort of servers/daemons:

#!/bin/zsh

# Settings for Minecraft instances
MINEDIR=/srv/minecraft
JAVA=/opt/java/jre/bin/java
MAXMEM=900M
MINMEM=256M

# Check if a world is running
function is_up ()
{
    world=$1
    sudo -uminecraft screen -ls | grep -q minecraft$world
}

# Check if a world exists
function is_world ()
{
    world=$1
    [[ -d $MINEDIR/$world ]]
}

# Confirm a user action
function confirm ()
{
    prompt=$1
    echo -n "$prompt [y/N]: "
    read ans
    [[ $ans == "y" ]] || [[ $ans == "Y" ]]
}

# Get the action
action=$1

if [[ $action == "list" ]]; then
    # List all worlds and their status
    pushd $MINEDIR
    for file in *; do
    if [[ -d $file ]]; then
        if is_up $file; then
        # World is up
        echo "[UP]     $file"
        else
        echo "[DOWN]   $file"
        fi
    fi
    done
    popd

elif [[ $action == "help" ]] || [[ $2 == "" ]]; then
    # Show help
    echo " USAGE: minecraft <action> <world1> ... <worldn>"
    echo
    echo " Actions:"
    echo "     list   - List all worlds, indicating which are up."
    echo "     start  - Start all named world."
    echo "     stop   - Stop all named worlds."
    echo "     make   - Create all named worlds."
    echo "     delete - Delete all named worlds (after confirmation)."
    echo "     help   - Show this text."
    echo
    echo " ~ Michael Walker <mike@barrucadu.co.uk>"

else
    # Get the first world
    shift
    world=$1

    while [[ $world != "" ]]; do
    if [[ $action == "start" ]]; then
            # Start the world (if not started already)
        if is_up $world; then
        echo "$world is already up!"
        else
        if is_world $world; then
            # World exists, start it
            pushd $MINEDIR/$world
            sudo -uminecraft screen -A -m -d -S minecraft$world $JAVA -Xmx$MAXMEM -Xms$MINMEM -jar $MINEDIR/minecraft-server.jar nogui minecraft$world
            echo "Started $world."
            popd
        else
            # World does not exist, complain
            echo "$world does not exist!"
        fi
        fi

    elif [[ $action == "stop" ]]; then
        # Stop the world (if up)
        if is_up $world; then
        sudo -uminecraft kill `pgrep -fl minecraft$world | sed 's: .*::'`
        echo "Stopped $world."
        else
        echo "$world is not up!"
        fi

    elif [[ $action == "make" ]]; then
        # Make the world (if nonexistant)
        if is_world $world; then
        echo "$world already exists!"
        else
        sudo -uminecraft mkdir $MINEDIR/$world
        sudo -uminecraft cp $MINEDIR/server.properties $MINEDIR/ops.txt $MINEDIR/$world
        echo "Created $world."
        fi

    elif [[ $action == "delete" ]]; then
        # Delete the world (after confirmation)
        if is_world $world; then
        if is_up $world; then
            echo "$world is currently up!"
        else
            if confirm "Do you really want to delete $world?"; then
            sudo -uminecraft rm -r $MINEDIR/$world
            fi
        fi
        else
        echo "$world does not exist!"
        fi
    fi
    
        # Get the next world
    shift
    world=$1
    done
fi

There is the following directory hierarchy in /srv/minecraft:

/srv/minecraft
├── allops
├── minecraft-server.jar
├── ops.txt
├── server.properties
└── smp

minecraft-server.jar is the actual server, ops.txt and server.properties are the default files for new servers, and the other directories (here "allops" and "smp") are specific worlds.

Here's an example of it being used a little:

barrucadu on carcosa in /srv/minecraft 
 >>>  minecraft list
[DOWN]   allops
[DOWN]   smp

barrucadu on carcosa in /srv/minecraft 
 >>>  minecraft make foo
Created foo.

barrucadu on carcosa in /srv/minecraft 
 >>>  minecraft start smp
Started smp.

barrucadu on carcosa in /srv/minecraft 
 >>>  minecraft list
[DOWN]   allops
[DOWN]   foo
[UP]     smp

barrucadu on carcosa in /srv/minecraft 
 >>>  minecraft delete foo smp
Do you really want to delete foo? [y/N]: y
smp is currently up!

barrucadu on carcosa in /srv/minecraft 
 >>>  minecraft list
[DOWN]   allops
[UP]     smp

Basically, it's a tool to start/stop Minecraft server instances for different worlds.

Last edited by Barrucadu (2011-06-21 02:24:29)

Offline

Board footer

Powered by FluxBB