You are not logged in.

#1076 2010-05-30 14:17:11

simlan
Member
From: Germany
Registered: 2010-05-30
Posts: 20

Re: Post your handy self made command line utilities

I call it pyquery. It parses bad HTML and lets me specify  which elements I want to have extracted (id /classname tag etc...) The Code is not perfect in style..."quick and dirty" wink . It requires Beautifullsoup http://www.crummy.com/software/BeautifulSoup/. I normally use it for  screenscraping like  that:

curl www.somesite.com | pyquery -t div -c someclass | grep .... #U got the point

This is the Code for pyquery (when running use --help for description of options or read it in the source wink )

#!/usr/bin/env python

from optparse import OptionParser
import sys
from BeautifulSoup import BeautifulSoup,ResultSet

#Pyquery handy commandlinetool for querying (bad) Html
#Reads HTML from stdin and spits out matches


def main():
    #parse options
    parser = OptionParser()
    parser.add_option("-t", "--tag", dest="tag",
                  help="selects all Elements with the given TAGNAME", metavar="TAGNAME")
    parser.add_option("-i", "--id",
                   dest="id", 
                  help="Selects all elements with the given IDVALUE",metavar="IDVALUE")
    parser.add_option("-c", "--class",
                   dest="class_", 
                  help="Selects all elements with the given CLASSNAME",metavar="CLASSNAME")
    parser.add_option("-n","--number",dest="index",
                       help="If you have multiple matches you can specify the match by its NUMBER in the result. Lets assume you have 3 matches and want the second put -n 1 ,because the index is zero based.")

    parser.add_option("-x", "--textonly",
                   dest="textonly",action="store_true",default=False,
                  help="Prints out the result without any Htmltags")


    (options, args) = parser.parse_args()
    #read stdin and put to Bsoup
    text=sys.stdin.readlines()
    html="".join(text)
    soup=BeautifulSoup(html)
    soup.pretify
    #Initialize Options and result variable
    attribs={"class":options.class_ , "id":options.id}
    result=None
    #The parsing of the html with attribs defined above
    resultset=soup.findAll(name=options.tag, attrs=attribs, recursive=True, text=None, limit=None)                                                                 #todo ^ text
    if options.index :
    #check if index number given
        resultset=resultset[int(options.index)]
    if options.textonly:
    #transform type resultset to string via iteration
        if isinstance(resultset,ResultSet) :
            result=""
            for i in resultset:
                result=result+" "+i.text
        else:
            result=resultset.text
    if not result:
    #This is a little messy
    # I have to iterate here to get rid of the [] that python normaly prints for lists 
        for i in resultset:
            print i
            print " "
    else:
        print result
        sys.exit(1)
        
if __name__=="__main__":
    main()

http://github.com/simlan123/pyquery

Last edited by simlan (2010-05-30 15:19:40)

Offline

#1077 2010-05-30 14:24:03

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

I'm controlling firefox remotely using http://github.com/trapd00r/firefox-remote .

This:

#!/bin/sh
rm /tmp/*.png &> /dev/null
FNAME=$(fxr pic|awk '{print $4}'|perl -pe 's/\/.+\///')
scp -P 11313 /tmp/$FNAME scp1@192.168.1.33:http/psy/fxr && echo "http://psy.trapd00r.se/fxr/$FNAME"|xclip

allows me to do some magic, like so: http://psy.trapd00r.se/fxr/Arch_Linux_F … lities.png

Offline

#1078 2010-05-30 15:24:12

simlan
Member
From: Germany
Registered: 2010-05-30
Posts: 20

Re: Post your handy self made command line utilities

@dmz: cool one wink

Last edited by simlan (2010-05-30 15:27:50)

Offline

#1079 2010-05-31 09:46:43

hume's doona
Member
Registered: 2009-12-11
Posts: 206

Re: Post your handy self made command line utilities

A simple one to check optus prepaid mobile broadband credit using gammu, it just sends a text sms and receives the reply:

balance.sh

#! /bin/bash
echo 'balance' | gammu sendsms TEXT 9999
sleep 10
gammu getallsms

of course it just implements gammu and has to be run as root [also can't be connected to the internet at the same time], but it saves a bit of typing

Offline

#1080 2010-05-31 16:33:05

TaylanUB
Member
Registered: 2009-09-16
Posts: 150

Re: Post your handy self made command line utilities

dmz wrote:

I'm controlling firefox remotely using http://github.com/trapd00r/firefox-remote .

This:

#!/bin/sh
rm /tmp/*.png &> /dev/null
FNAME=$(fxr pic|awk '{print $4}'|perl -pe 's/\/.+\///')
scp -P 11313 /tmp/$FNAME scp1@192.168.1.33:http/psy/fxr && echo "http://psy.trapd00r.se/fxr/$FNAME"|xclip

allows me to do some magic, like so: http://psy.trapd00r.se/fxr/Arch_Linux_F … lities.png

Please specify the exact shell (#!/bin/bash in our case) when using shell-specific (non-POSIX) features.
( '&>' is a bashism.)

Sorry for off-topic. big_smile


Did i ever share shellplayer?
It originally started because, when i did `mplayer -loop 0`, i couldn't make it stop when the current song finishes, and i was too lazy to figure out how to set up MPlayer for that. =P I realized i could easily add more funny things to it, and it grew to ~200 lines.
Some parts await a rewrite, and i think of implementing more stuff, like a config of some sort, and more mature playlisting. This is 99% for fun though; i'm sure MPlayer could do all of these plus a *lot* more, through a FIFO or something. (Not even considering MPD and the like...)

#!/bin/sh
#
# shellplayer.sh: POSIX shell driven music player wrapper.
#                 Supplies functionality for add song, loop current, stop looping, etc..
#
# This script depends on tools provided by 'moreutils'.
#
# TODO
# Provide usage info.
# Implement uniqing of playlist.
# Implement configuration stuff.
#

. "$HOME"/bin/common_stuff.sh

ed="${VISUAL:-${EDITOR:-vi}}"

cache="${XDG_CACHE_HOME:-$HOME/.cache}"/shellplayer

touch_dirs "$cache" || exit 2

current="$cache"/current
playlist="$cache"/playlist
delay="$cache"/delay
pid="$cache"/pid

touch_files "$current" "$playlist" "$delay" "$pid" || exit 2

looper="$cache"/looper

grep -q . "$delay" ||
    echo 0 > "$delay"

clean_exit ()
{
    test $# -gt 1 && return 255
    test_ints "${1:-1}" || return 255
    > "$current"
    > "$pid"
    rm -f "$looper"
    exit $1
}

# Here you can change the media player to use
play ()
{
    trap '"$0" kill' TERM
    mplayer "$@" < /dev/null > /dev/null 2>&1 &
    echo $! > "$pid"
    wait $!
    local s=$?
    > "$pid"
    if [ "$exitonerror" ] # Maybe make an option to enable this
    then
        test $s -eq 0 || {
            echo >&2 "$z: media player exited with exit status $s"
            clean_exit 3
        }
    fi
}

test "$1" || {
    echo >&2 "$z: no command given"
    exit 1
}

cmd="$1"; shift

# For better readability
is_running(){ grep -q . "$current"; }
pl_line1empty(){ ! sed -n 1p < "$playlist" | grep -q .; }
pl_empty(){ ! grep -q . < "$playlist"; }

case "$cmd" in
    a|ad|add)
        for arg
        do
            echo "$arg" >> "$playlist"
        done
        ;;
    l|ls|lst|list)
        cat -n "$playlist"
        ;;
    e|ed|edit)
        "$ed" "$playlist"
        ;;
    sh|shuf|shuffle)
        if pl_empty
        then
            echo >&2 "$z: playlist is empty"
            exit 1
        fi
        wasempty=
        pl_line1empty && wasempty=yeah
        sed '/^$/d' < "$playlist" | shuf | sponge "$playlist"
        if [ "$wasempty" ]; then
            echo | cat - "$playlist" | sponge "$playlist"
        fi
        ;;
    cl|clr|clear)
        > "$playlist"
        ;;
    ln|lo|lon|lop|lopon|loop|loopon)
        >> "$looper" || {
            echo >&2 "$z: could not create file: $looper"
            exit 2
        }
        ;;
    lf|lof|lopof|loopoff|nl|nol|nolo|nolop|noloop)
        rm -f "$looper" || {
            echo >&2 "$z: could not remove file: $looper"
            exit 2
        }
        ;;
    d|delay)
        case $# in
            0)
                echo "$z: delay is $(cat "$delay")"
                exit
                ;;
            1)
                if test_ints "$1" && [ $1 -ge 0 ]
                then
                    echo $1 > "$delay"
                else
                    echo >&2 "$z: expecting unsigned integer"
                    exit 1
                fi
                ;;
            *)
                echo >&2 "$z: too many arguments"
                exit 1
                ;;
        esac
        ;;
    c|cur|current)
        if is_running
        then
            echo "$z: playing '$(cat "$current")'"
        else
            echo "$z: not playing anything"
        fi
        ;;
    C|Cur|Current)
        echo "$(cat "$current")"
        ;;
    r|run)
        if is_running
        then
            echo >&2 "$z: already running"
            exit 1
        fi
        while true
        do
            if [ -f "$looper" ]
            then
                if grep -q '^next$' < "$looper" || ! is_running
                then
                    sed '/^next$/d' < "$looper" | sponge "$looper"
                    sed -n 1p < "$playlist" > "$current"
                    sed 1d < "$playlist" | sponge "$playlist"
                fi
            else
                pl_line1empty && clean_exit
                sed -n 1p < "$playlist" > "$current"
                sed 1d < "$playlist" | sponge "$playlist"
            fi
            play "$(cat "$current")"
            pl_line1empty && { test -f "$looper" || clean_exit; }
            sleep $(cat "$delay")
        done &
        ;;
    n|ne|nx|nxt|next)
        is_running || {
            echo >&2 "$z: currently not running"
            exit 1
        }
        test -f "$looper" || {
            echo >&2 "$z: not looping"
            exit 1
        }
        echo >> "$looper" 'next'
        ;;
    sk|skip)
        is_running || {
            echo >&2 "$z: currently not running"
            exit 1
        }
        kill $(cat "$pid")
        ;;
    s|st|stop)
        rm -f "$looper" || {
            echo >&2 "$z: could not remove file: $looper"
            exit 2
        }
        pl_line1empty && exit
        echo | cat - "$playlist" | sponge "$playlist"
        ;;
    cnt|cont|continue)
        if is_running
        then
            pl_line1empty || exit
            sed 1d < "$playlist" | sponge "$playlist"
        else
            echo >&2 "$z: currently not running"
            exit 1
        fi
        ;;
    k|kill)
        is_running || {
            echo >&2 "$z: i'm dead already!"
            exit 1
        }
        kill -KILL $(pidof -x "$z" -o %PPID)
        kill $(cat "$pid") 2> /dev/null
        clean_exit
        ;;
    *)
        echo >&2 "$z: unrecognized command: $cmd"
        exit 1
        ;;
esac

Here's the current version of common_stuff.sh:
(Edit: Obsolete and breakage-prone.)

#!/usr/bin/printf This file is meant to be sourced!\n\c
#
# common_stuff.sh: Some useful functions and stuff for shell scripts.
#                  It's all POSIX.
#

z="$(basename "$0")"

validate_var_names ()
{
    for var
    do
        if
            test "$(echo "$var" | wc -l)" -ne 1 ||
            echo "$var" | grep -q '\(^[0-9]\|[^a-zA-Z0-9_]\)'
        then
            return 1
        fi
    done
}

default ()
{
    test $# -eq 2 || return 255
    validate_var_names "$1" || return 255
    eval 'test "$'$1'" || '$1'="$2"'
}

# Pass it variables to test their content
test_ints ()
{
    for int
    do
        test "$int" -eq "$int" 2> /dev/null || return 1
    done
}

test_files ()
{
    local test_args="$1"; shift
    local tests=true
    for i in $(seq 1 ${#test_args})
    do
        tests="$tests && test -$(echo "$test_args" | cut -c $i) \$file"
    done
    for file
    do
        eval $tests || {
            echo >&2 "$z: problem related to file: $file"
            return 1
        }
    done
}

# This trapping mechanism is win

make_trap ()
{
    test $# -eq 1 || return 255
    echo "$1" | grep -q '[^A-Z]' && return 255
    eval '
        '$1'file="${TMPDIR:-/tmp}"/"$z"-$1file-$$
        touch "$'$1'file" || {
            echo >&2 "$z: could not create file: $'$1'"
            return 1
        }
        trap'$1'(){ echo >> "$'$1'file" "$@"; }
        trap'$1' "#!/bin/sh"
        trap'$1' "rm -f \"\$0\""
        chmod u+x "$'$1'file"
        trap "\"$'$1'file\"" $1
    '
}

make_trap EXIT # Initialize this right away; it's very common
               # Other functions even assume it to be existant

make_tmp_files ()
{
    for file
    do
        validate_var_names "$file" || return 255
        eval '
            '$file'="${TMPDIR:-/tmp}"/"$z"-$file-$$
            trapEXIT "rm -f \"$'$file'\""
            touch "$'$file'" || {
                echo >&2 "$z: could not create file: $'$file'"
                return 1
            }
        '
    done
}

make_tmp_dirs ()
{
    for dir
    do
        validate_var_names "$dir" || return 255
        eval '
            '$dir'="${TMPDIR:-/tmp}"/"$z"-$dir-$$
            trapEXIT "rmdir -p \"$'$dir'\" 2> /dev/null"
            mkdir -p "$'$dir'" || {
                echo >&2 "$z: could not create directory: $'$dir'"
                return 1
            }
        '
    done
}

touch_files ()
{
    for file
    do
        >> "$file" || {
            echo >&2 "$z: could not create file: $file"
            return 1
        }
    done
}

touch_dirs ()
{
    for dir
    do
        mkdir -p "$dir" || {
            echo >&2 "$z: could not create directory: $dir"
            return 1
        }
    done
}

random_num ()
{
    test_ints "$@" || return 255
    local min=${1:-0}
    local max=${2:-$((min+32767))} # Default max of bash
    seq $min $max | shuf -n 1
}

Last edited by TaylanUB (2010-06-06 09:11:43)


``Common sense is nothing more than a deposit of prejudices laid down by the mind before you reach eighteen.''
~ Albert Einstein

Offline

#1081 2010-05-31 19:39:40

3])
Member
From: Netherlands
Registered: 2009-10-12
Posts: 215

Re: Post your handy self made command line utilities

This will organize everything according to file format. Helps neated up your "Download" directory, or any equivalent directory you got where you have lots of important junk hanging around.

#!/usr/bin/bash
#Filename:Bashstuff1.bh
#organizes files by type into a special subdirectory
#Author:3]) Archlinux forums
#License:GLP3.0-use at your own risk ;-)
mkdir organizedstuff

mkdir organizedstuff/videostuff/
mv *.flv organizedstuff/videostuff/
mv *.swf organizedstuff/videostuff/
mv *.fla organizedstuff/videostuff/
mv *.rmvb organizedstuff/videostuff/
mv *.rm organizedstuff/videostuff/
mv *.flr organizedstuff/videostuff/
mv *.mkv organizedstuff/videostuff/
mv *.mov organizedstuff/videostuff/
mv *.mpeg organizedstuff/videostuff/
mv *.mpg organizedstuff/videostuff/
mv *.mpe organizedstuff/videostuff/
mv *.mp4 organizedstuff/videostuff/
mv *.ogm organizedstuff/videostuff/
mv *.ogg organizedstuff/videostuff/
mv *.wmv organizedstuff/videostuff/
mv *.avi organizedstuff/videostuff/


mkdir organizedstuff/audiostuff/
mv *.aiff organizedstuff/audiostuff/
mv *.au organizedstuff/audiostuff/
mv *.cdda organizedstuff/audiostuff/
mv *.iff-8svx organizedstuff/audiostuff/
mv *.iff-16sv organizedstuff/audiostuff/
mv *.raw organizedstuff/audiostuff/
mv *.wav organizedstuff/audiostuff/
mv *.flac organizedstuff/audiostuff/
mv *.la organizedstuff/audiostuff/
mv *.pac organizedstuff/audiostuff/
mv *.m4a organizedstuff/audiostuff/
mv *.ape organizedstuff/audiostuff/
mv *.optimfrog organizedstuff/audiostuff/
mv *.rka organizedstuff/audiostuff/
mv *.shn organizedstuff/audiostuff/
mv *.tta organizedstuff/audiostuff/
mv *.wv organizedstuff/audiostuff/
mv *.wma organizedstuff/audiostuff/
mv *.mp2 organizedstuff/audiostuff/
mv *.mp3 organizedstuff/audiostuff/
mv *.speex organizedstuff/audiostuff/
mv *.vorbis organizedstuff/audiostuff/
mv *.gsm organizedstuff/audiostuff/
mv *.wma organizedstuff/audiostuff/
mv *.m4a organizedstuff/audiostuff/
mv *.mp4 organizedstuff/audiostuff/
mv *.m4p organizedstuff/audiostuff/
mv *.aac organizedstuff/audiostuff/
mv *.mpc organizedstuff/audiostuff/
mv *.vqf organizedstuff/audiostuff/
mv *.ra organizedstuff/audiostuff/
mv *.rm organizedstuff/audiostuff/
mv *.ots organizedstuff/audiostuff/
mv *.swa organizedstuff/audiostuff/
mv *.vox organizedstuff/audiostuff/
mv *.voc organizedstuff/audiostuff/
mv *.dwd organizedstuff/audiostuff/
mv *.smp organizedstuff/audiostuff/
mv *.swa organizedstuff/audiostuff/
mv *.cust organizedstuff/audiostuff/
mv *.mid organizedstuff/audiostuff/
mv *.mus organizedstuff/audiostuff/
mv *.sib organizedstuff/audiostuff/
mv *.gym organizedstuff/audiostuff/
mv *.vgm organizedstuff/audiostuff/
mv *.psf organizedstuff/audiostuff/
mv *.nsf organizedstuff/audiostuff/
mv *.mod organizedstuff/audiostuff/
mv *.ptb organizedstuff/audiostuff/
mv *.s3m organizedstuff/audiostuff/
mv *.xm organizedstuff/audiostuff/
mv *.it organizedstuff/audiostuff/
mv *.mt2 organizedstuff/audiostuff/
mv *.mng organizedstuff/audiostuff/
mv *.psf organizedstuff/audiostuff/
mv *.rmj organizedstuff/audiostuff/
mv *.spc organizedstuff/audiostuff/
mv *.niff organizedstuff/audiostuff/
mv *.musicxml organizedstuff/audiostuff/
mv *.ym organizedstuff/audiostuff/
mv *.jam organizedstuff/audiostuff/
mv *.ram organizedstuff/audiostuff/
mv *.aup organizedstuff/audiostuff/
mv *.cel organizedstuff/audiostuff/
mv *.cpr organizedstuff/audiostuff/
mv *.npr organizedstuff/audiostuff/
mv *.cwp organizedstuff/audiostuff/
mv *.drm organizedstuff/audiostuff/
mv *.omf organizedstuff/audiostuff/
mv *.ses organizedstuff/audiostuff/
mv *.stf organizedstuff/audiostuff/
mv *.syn organizedstuff/audiostuff/




mkdir organizedstuff/imagestuff/
mv *.jpeg  organizedstuff/imagestuff/
mv *.gif  organizedstuff/imagestuff/ 
mv *.png organizedstuff/imagestuff/
mv *.act  organizedstuff/imagestuff/
mv *.art  organizedstuff/imagestuff/ 
mv *.bmp organizedstuff/imagestuff/
mv *.blpjpeg  organizedstuff/imagestuff/
mv *.cit  organizedstuff/imagestuff/ 
mv *.cpt organizedstuff/imagestuff/
mv *.cut  organizedstuff/imagestuff/
mv *.dib  organizedstuff/imagestuff/ 
mv *.djvu organizedstuff/imagestuff/
mv *.egt  organizedstuff/imagestuff/
mv *.exif  organizedstuff/imagestuff/ 
mv *.gif organizedstuff/imagestuff/
mv *.icns  organizedstuff/imagestuff/
mv *.ico  organizedstuff/imagestuff/ 
mv *.iff organizedstuff/imagestuff/
mv *.jng  organizedstuff/imagestuff/
mv *.jpg  organizedstuff/imagestuff/ 
mv *.jp2 organizedstuff/imagestuff/
mv *.lbm  organizedstuff/imagestuff/
mv *.max  organizedstuff/imagestuff/ 
mv *.miff organizedstuff/imagestuff/
mv *.mng organizedstuff/imagestuff/
mv *.msp organizedstuff/imagestuff/
mv *.nitf organizedstuff/imagestuff/
mv *.ota organizedstuff/imagestuff/
mv *.pbm organizedstuff/imagestuff/
mv *.pc1 organizedstuff/imagestuff/
mv *.pc2 organizedstuff/imagestuff/
mv *.pc3 organizedstuff/imagestuff/
mv *.pcf organizedstuff/imagestuff/
mv *.pcx organizedstuff/imagestuff/
mv *.awg organizedstuff/imagestuff/
mv *.ai organizedstuff/imagestuff/
mv *.eps organizedstuff/imagestuff/
mv *.cgm organizedstuff/imagestuff/
mv *.cdr organizedstuff/imagestuff/
mv *.cmx organizedstuff/imagestuff/
mv *.dxf organizedstuff/imagestuff/
mv *.egt organizedstuff/imagestuff/
mv *.svg organizedstuff/imagestuff/
mv *.wmf organizedstuff/imagestuff/
mv *.emf organizedstuff/imagestuff/
mv *.art organizedstuff/imagestuff/
mv *.xar organizedstuff/imagestuff/



mkdir organizedstuff/programmingstuff/
mv *.as organizedstuff/programmingstuff/
mv *.asl organizedstuff/programmingstuff/
mv *.asp organizedstuff/programmingstuff/
mv *.au3 organizedstuff/programmingstuff/
mv *.bat organizedstuff/programmingstuff/
mv *.bas organizedstuff/programmingstuff/
mv *.bb organizedstuff/programmingstuff/
mv *.bh organizedstuff/programmingstuff/
mv *.bmax organizedstuff/programmingstuff/
mv *.cmd organizedstuff/programmingstuff/
mv *.egg organizedstuff/programmingstuff/
mv *.egt organizedstuff/programmingstuff/
mv *.hta organizedstuff/programmingstuff/
mv *.ici organizedstuff/programmingstuff/
mv *.itcl organizedstuff/programmingstuff/
mv *.js organizedstuff/programmingstuff/
mv *.jsfl organizedstuff/programmingstuff/
mv *.lua organizedstuff/programmingstuff/
mv *.m organizedstuff/programmingstuff/
mv *.mrc organizedstuff/programmingstuff/
mv *.ncf organizedstuff/programmingstuff/
mv *.nut organizedstuff/programmingstuff/
mv *.php organizedstuff/programmingstuff/
mv *.pl organizedstuff/programmingstuff/
mv *.pm organizedstuff/programmingstuff/
mv *.ps1 organizedstuff/programmingstuff/
mv *.ps1xml organizedstuff/programmingstuff/
mv *.psc1 organizedstuff/programmingstuff/
mv *.psd1 organizedstuff/programmingstuff/
mv *.psm1 organizedstuff/programmingstuff/
mv *.py organizedstuff/programmingstuff/
mv *.pyc organizedstuff/programmingstuff/
mv *.pyo organizedstuff/programmingstuff/
mv *.rb organizedstuff/programmingstuff/
mv *.scpt organizedstuff/programmingstuff/
mv *.sh organizedstuff/programmingstuff/
mv *.tcl organizedstuff/programmingstuff/
mv *.vbs organizedstuff/programmingstuff/
mv *.c organizedstuff/programmingstuff/
mv *.cls organizedstuff/programmingstuff/
mv *.cob organizedstuff/programmingstuff/
mv *.cbl organizedstuff/programmingstuff/
mv *.cpp organizedstuff/programmingstuff/
mv *.cc organizedstuff/programmingstuff/
mv *.cxx organizedstuff/programmingstuff/
mv *.cs organizedstuff/programmingstuff/
mv *.d organizedstuff/programmingstuff/
mv *.e organizedstuff/programmingstuff/
mv *.efs organizedstuff/programmingstuff/
mv *.frm organizedstuff/programmingstuff/
mv *.frx organizedstuff/programmingstuff/
mv *.ged organizedstuff/programmingstuff/
mv *.gm6 organizedstuff/programmingstuff/
mv *.gmd organizedstuff/programmingstuff/
mv *.gmk organizedstuff/programmingstuff/
mv *.gml organizedstuff/programmingstuff/
mv *.h organizedstuff/programmingstuff/
mv *.hpp organizedstuff/programmingstuff/
mv *.hxx organizedstuff/programmingstuff/
mv *.inc organizedstuff/programmingstuff/
mv *.java organizedstuff/programmingstuff/
mv *.l organizedstuff/programmingstuff/
mv *.m organizedstuff/programmingstuff/
mv *.m4 organizedstuff/programmingstuff/
mv *.ml organizedstuff/programmingstuff/
mv *.n organizedstuff/programmingstuff/
mv *.pas organizedstuff/programmingstuff/
mv *.pp organizedstuff/programmingstuff/
mv *.p organizedstuff/programmingstuff/
mv *.php organizedstuff/programmingstuff/
mv *.php3 organizedstuff/programmingstuff/
mv *.php4 organizedstuff/programmingstuff/
mv *.php5 organizedstuff/programmingstuff/
mv *.phps organizedstuff/programmingstuff/
mv *.phtml organizedstuff/programmingstuff/
mv *.pas organizedstuff/programmingstuff/
mv *.resx organizedstuff/programmingstuff/
mv *.y organizedstuff/programmingstuff/
mv *.pas organizedstuff/programmingstuff/

mkdir organizedstuff/textstuff/
mv *.123 organizedstuff/textstuff/
mv *.aws organizedstuff/textstuff/
mv *.gnumeric organizedstuff/textstuff/
mv *.numbers organizedstuff/textstuff/
mv *.ods organizedstuff/textstuff/
mv *.ots organizedstuff/textstuff/
mv *.opw organizedstuff/textstuff/
mv *.slk organizedstuff/textstuff/
mv *.stc organizedstuff/textstuff/
mv *.sxc organizedstuff/textstuff/
mv *.tab organizedstuff/textstuff/
mv *.vc organizedstuff/textstuff/
mv *.wk1 organizedstuff/textstuff/
mv *.wk2 organizedstuff/textstuff/
mv *.wk3 organizedstuff/textstuff/
mv *.wk4 organizedstuff/textstuff/
mv *.wks organizedstuff/textstuff/
mv *.wq1 organizedstuff/textstuff/
mv *.xlk organizedstuff/textstuff/
mv *.xls organizedstuff/textstuff/
mv *.xlsb organizedstuff/textstuff/
mv *.xlsm organizedstuff/textstuff/
mv *.xlsx organizedstuff/textstuff/
mv *.xlr organizedstuff/textstuff/
mv *.xlt organizedstuff/textstuff/
mv *.xltm organizedstuff/textstuff/
mv *.xlw organizedstuff/textstuff/
mv *.abw organizedstuff/textstuff/
mv *.ans organizedstuff/textstuff/
mv *.asc organizedstuff/textstuff/
mv *.aww organizedstuff/textstuff/
mv *.csv organizedstuff/textstuff/
mv *.cwk organizedstuff/textstuff/
mv *.doc organizedstuff/textstuff/
mv *.docx organizedstuff/textstuff/
mv *.dot organizedstuff/textstuff/
mv *.dotx organizedstuff/textstuff/
mv *.egt organizedstuff/textstuff/
mv * .ftm organizedstuff/textstuff/
mv *.ftx organizedstuff/textstuff/
mv *.hwp organizedstuff/textstuff/
mv *.lwp organizedstuff/textstuff/
mv *.mcw organizedstuff/textstuff/
mv *.nb organizedstuff/textstuff/
mv *.nbp organizedstuff/textstuff/
mv *.abf organizedstuff/textstuff/
mv *.afm organizedstuff/textstuff/
mv *.bdf organizedstuff/textstuff/
mv *.bmf organizedstuff/textstuff/
mv *.fon organizedstuff/textstuff/
mv *.mgf organizedstuff/textstuff/
mv *.otf organizedstuff/textstuff/
mv *.pcf organizedstuff/textstuff/
mv *.snf organizedstuff/textstuff/
mv *.tfm organizedstuff/textstuff/
mv *.ttf organizedstuff/textstuff/
mv *.key organizedstuff/textstuff/
mv *.keynote organizedstuff/textstuff/
mv *.snf organizedstuff/textstuff/
mv *.nb organizedstuff/textstuff/
mv *.nbp organizedstuff/textstuff/
mv *.odt organizedstuff/textstuff/
mv *.ott organizedstuff/textstuff/
mv *.pages organizedstuff/textstuff/
mv *.pap organizedstuff/textstuff/
mv *.radix-64 organizedstuff/textstuff/
mv *.rtf organizedstuff/textstuff/
mv *.sdw organizedstuff/textstuff/
mv *.stw organizedstuff/textstuff/
mv *.sxw organizedstuff/textstuff/
mv *.tex organizedstuff/textstuff/
mv *.info organizedstuff/textstuff/
mv *.troff organizedstuff/textstuff/
mv *.txt organizedstuff/textstuff/
mv *.uoml organizedstuff/textstuff/
mv *.wpd organizedstuff/textstuff/
mv *.wps organizedstuff/textstuff/
mv *.wpt organizedstuff/textstuff/
mv *.wrd organizedstuff/textstuff/
mv *.wrf organizedstuff/textstuff/
mv *.wri organizedstuff/textstuff/
mv *.odp organizedstuff/textstuff/
mv *.otp organizedstuff/textstuff/
mv *.pps organizedstuff/textstuff/
mv *.ppt organizedstuff/textstuff/
mv *.prz organizedstuff/textstuff/
mv *.shf organizedstuff/textstuff/
mv *.sti organizedstuff/textstuff/
mv *.sxi organizedstuff/textstuff/
mv *.watch organizedstuff/textstuff/
mv *.sti organizedstuff/textstuff/
mv *.sti organizedstuff/textstuff/
mv *.pdf organizedstuff/textstuff/



mkdir organizedstuff/archivestuff/
mv *.q organizedstuff/archivestuff/
mv *.7z organizedstuff/archivestuff/
mv *.ace organizedstuff/archivestuff/
mv *.alz organizedstuff/archivestuff/
mv *.at3 organizedstuff/archivestuff/
mv *.bke organizedstuff/archivestuff/
mv *.arc organizedstuff/archivestuff/
mv *.dds organizedstuff/archivestuff/
mv *.arj organizedstuff/archivestuff/
mv *.big organizedstuff/archivestuff/
mv *.bkf organizedstuff/archivestuff/
mv *.bz2 organizedstuff/archivestuff/
mv *.cab organizedstuff/archivestuff/
mv *.cpt organizedstuff/archivestuff/
mv *.sea organizedstuff/archivestuff/
mv *.daa organizedstuff/archivestuff/
mv *.deb organizedstuff/archivestuff/
mv *.dmg organizedstuff/archivestuff/
mv *.eea organizedstuff/archivestuff/
mv *.egt organizedstuff/archivestuff/
mv *.ecab organizedstuff/archivestuff/
mv *.ezip organizedstuff/archivestuff/
mv *.ess organizedstuff/archivestuff/
mv *.gho organizedstuff/archivestuff/
mv *.ghs organizedstuff/archivestuff/
mv *.gz organizedstuff/archivestuff/
mv *.jar organizedstuff/archivestuff/
mv *.lbr organizedstuff/archivestuff/
mv *.lqr organizedstuff/archivestuff/
mv *.lzh organizedstuff/archivestuff/
mv *.lzo organizedstuff/archivestuff/
mv *.lzx organizedstuff/archivestuff/
mv *.bin organizedstuff/archivestuff/
mv *.pak organizedstuff/archivestuff/
mv *.par organizedstuff/archivestuff/
mv *.par2 organizedstuff/archivestuff/
mv *.pk4 organizedstuff/archivestuff/
mv *.rar* organizedstuff/archivestuff/
mv *.sit organizedstuff/archivestuff/
mv *.sitx organizedstuff/archivestuff/
mv *.tar organizedstuff/archivestuff/
mv *.tar.gz organizedstuff/archivestuff/
mv *.tgz organizedstuff/archivestuff/
mv *.tb organizedstuff/archivestuff/
mv *.tib organizedstuff/archivestuff/
mv *.uha organizedstuff/archivestuff/
mv *.vsa organizedstuff/archivestuff/
mv *.z organizedstuff/archivestuff/
mv *.zoo organizedstuff/archivestuff/
mv *.zip organizedstuff/archivestuff/
mv *.q organizedstuff/archivestuff/
mv *.iso organizedstuff/archivestuff/
mv *.img organizedstuff/archivestuff/
mv *.adf organizedstuff/archivestuff/
mv *.adz organizedstuff/archivestuff/
mv *.dms organizedstuff/archivestuff/
mv *.dsk organizedstuff/archivestuff/
mv *.d64 organizedstuff/archivestuff/

“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”-- C.A.R. Hoare

Offline

#1082 2010-05-31 19:46:15

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

3]) wrote:
#!/usr/bin/bash

bash is in /bin

Offline

#1083 2010-05-31 22:16:15

Hiato
Member
Registered: 2009-01-21
Posts: 76

Re: Post your handy self made command line utilities

I know it's been done before, but since when has that been a disincentive? My cd replacement however, uses a simple move-to front transform on an arbitrary number of the most recent dirs, and as a by-product of using grep, it supports whatever regexs grep does. Examining the code should make its purpose and operation quite clear, but there is something to note: It must be called as ". scriptname arg" [use an alias or some such] otherwise the cd remains local to the script. Tweak as you will (the 42, for instance)

#!/bin/dash
fl="$XDG_DATA_HOME/dir_lst"; [ ! -f "$fl" ] && touch "$fl"
ln=`grep -n "$1" "$fl" | head -n 1 | cut -d : -f 1`
if [ ! "$1" = "" ]; then if [ ! "$ln" = "" ]; then 
    cd `echo "$ln m0\n1p\nw" | ed -s "$fl"`;
else 
    while [ ! -d "$1" ]; do echo "?" && 1="$(line)"; done;
    1=`echo "$1" | sed "s|^[^/]|$PWD/&|"`
    cd `echo "0i\n$1\n.\n1p\nw" | ed -s "$fl"`
    #cat "$fl" | head -n 42 > "$fl"
fi; else cd `echo "2 m0\n1p\nw" | ed -s "$fl"`; fi;

Oh, and I realise it can be made more compact, but I feel that it strikes a nice balance with readability.

EDIT: Some slight improvements: calling without an arg goes back to the previous directory; apparently 'cat "$fl" | head -n 42 > "$fl"' breaks the file on some systems, so it's been commented out for now until I find another solution

Last edited by Hiato (2010-05-31 23:16:14)

Offline

#1084 2010-05-31 23:12:56

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

If you make it a function sourced by bashrc you won't have to source it to run it

Offline

#1085 2010-05-31 23:18:04

Hiato
Member
Registered: 2009-01-21
Posts: 76

Re: Post your handy self made command line utilities

I use zsh and a simple alias, so no problems there, but I see what you're saying - perhaps it should just return a dir name?

Offline

#1086 2010-05-31 23:21:40

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

If &> is a bashism, how come it works when invoked with /bin/sh, that's supposed to make it POSIX-compliant?

Offline

#1087 2010-05-31 23:35:30

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

Because arch has bash for sh

Offline

#1088 2010-06-01 02:39:22

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

And like I said, when invoked with sh, it'll conform to the POSIX standard.

       If bash is invoked with the name sh,  it  tries  to  mimic  the  startup
       behavior of historical versions of sh as closely as possible, while con‐
       forming to the POSIX standard as well.

Therefore my question... there's no reason to be rude.

Offline

#1089 2010-06-01 09:25:27

TaylanUB
Member
Registered: 2009-09-16
Posts: 150

Re: Post your handy self made command line utilities

dmz wrote:

And like I said, when invoked with sh, it'll conform to the POSIX standard.

       If bash is invoked with the name sh,  it  tries  to  mimic  the  startup
       behavior of historical versions of sh as closely as possible, while con‐
       forming to the POSIX standard as well.

Therefore my question... there's no reason to be rude.

It will conform to the standards as in not doing anything that POSIX says should be different, but it accepts, and doesn't warn about, extra features that POSIX doesn't mention.

It's dangerous to symlink /bin/sh to dash because of all the scripts flying around that use bash features while specifying #!/bin/sh.


``Common sense is nothing more than a deposit of prejudices laid down by the mind before you reach eighteen.''
~ Albert Einstein

Offline

#1090 2010-06-01 21:04:04

3])
Member
From: Netherlands
Registered: 2009-10-12
Posts: 215

Re: Post your handy self made command line utilities

steve___ wrote:
3]) wrote:
#!/usr/bin/bash

bash is in /bin

It's both in /bin/ and /usr/bin/.

[123@123mobile bin]$ pwd
/usr/bin
[123@123mobile bin]$ bas
base64    basename  bash      bashbug
[123@123mobile bin]$ pwd
/bin
[123@123mobile bin]$ bash
bash     bashbug

“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”-- C.A.R. Hoare

Offline

#1091 2010-06-01 21:11:13

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

you, my friend, have an odd system.

//blue/0/~/ ll /usr/bin/bas
base64    basename  bashrun
//blue/0/~/ ll /usr/bin/bash
ls: cannot access /usr/bin/bash: No such file or directory
//blue/2/~/ ll /bin/bash
-rwxr-xr-x 1 root root 690K May 16 20:40 /bin/bash

Offline

#1092 2010-06-01 23:02:02

wolfbytes56
Member
Registered: 2009-12-26
Posts: 2

Re: Post your handy self made command line utilities

I made a todo list becuase honest to god its imossible to find a very simple no bullshit cli todolist program so......

#!/bin/bash
todo="$HOME/.todo-list
cat $todo
cat >> $todo

Offline

#1093 2010-06-04 05:56:07

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

Why is the -printf option missing from ls...

find "$@" -printf "\033[38;5;172m%M\033[38;5;104m %TY-%Tm-%Td\033[0m ▎ \033[38;5;100m%d\033[38;5;197m%y\033[0m ▎ %f\033[38;5;190m ->\033[0m(\033[38;5;166m%.20P\033[0m) \n"

ccJWCBqfb.png

Offline

#1094 2010-06-06 09:28:08

TaylanUB
Member
Registered: 2009-09-16
Posts: 150

Re: Post your handy self made command line utilities

Today i've learned that the for loop variable is not made local to the loop.

$ var=foo; for var in bar baz; echo $var; done; echo $var
bar
baz
baz

$ # This works:
$ var=foo; ( for var in bar baz; echo $var; done; ) echo $var
bar
baz
foo

In functions, use 'local' well.

Here's a stabilized version of common_stuff.sh (still use at own risk big_smile ):

#!/usr/bin/printf This file is meant to be sourced!\n\c
#
# common_stuff.sh: Some useful functions and stuff for shell scripts.
#                  It's all POSIX.
#

z="$(basename "$0")"

validate_var_names ()
{
    local var
    for var
    do
        if
            test "$(echo "$var" | wc -l)" -ne 1 ||
            echo "$var" | grep -q '\(^[0-9]\|[^a-zA-Z0-9_]\)'
        then
            return 1
        fi
    done
}

default ()
{
    test $# -eq 2 || return 255
    validate_var_names "$1" || return 255
    eval 'test "$'$1'" || '$1'="$2"'
}

# Pass it variables to test their content
test_ints ()
{
    local int
    for int
    do
        test "$int" -eq "$int" 2> /dev/null || return 1
    done
}

test_files ()
{
    local test_args="$1"; shift
    local tests=true
    local i
    for i in $(seq 1 ${#test_args})
    do
        tests="$tests && test -$(echo "$test_args" | cut -c $i) \$file"
    done
    local file
    for file
    do
        eval $tests || {
            echo >&2 "$z: problem related to file: $file"
            return 1
        }
    done
}

# This trapping mechanism is win

make_trap ()
{
    test $# -eq 1 || return 255
    echo "$1" | grep -q '[^A-Z]' && return 255
    eval '
        '$1'file="${TMPDIR:-/tmp}"/"$z"-$1file-$$
        touch "$'$1'file" || {
            echo >&2 "$z: could not create file: $'$1'"
            return 1
        }
        trap'$1'(){ echo >> "$'$1'file" "$@"; }
        trap'$1' "#!/bin/sh"
        trap'$1' "rm -f \"\$0\""
        chmod u+x "$'$1'file"
        trap "\"$'$1'file\"" $1
    '
}

make_trap EXIT # Initialize this right away; it's very common
               # Other functions even assume it to be existant

make_tmp_files ()
{
    local file
    for file
    do
        validate_var_names "$file" || return 255
        eval '
            '$file'="${TMPDIR:-/tmp}"/"$z"-$file-$$
            trapEXIT "rm -f \"$'$file'\""
            touch "$'$file'" || {
                echo >&2 "$z: could not create file: $'$file'"
                return 1
            }
        '
    done
}

make_tmp_dirs ()
{
    local dir
    for dir
    do
        validate_var_names "$dir" || return 255
        eval '
            '$dir'="${TMPDIR:-/tmp}"/"$z"-$dir-$$
            trapEXIT "rmdir -p \"$'$dir'\" 2> /dev/null"
            mkdir -p "$'$dir'" || {
                echo >&2 "$z: could not create directory: $'$dir'"
                return 1
            }
        '
    done
}

touch_files ()
{
    local file
    for file
    do
        >> "$file" || {
            echo >&2 "$z: could not create file: $file"
            return 1
        }
    done
}

touch_dirs ()
{
    local dir
    for dir
    do
        mkdir -p "$dir" || {
            echo >&2 "$z: could not create directory: $dir"
            return 1
        }
    done
}

random_num ()
{
    test_ints "$@" || return 255
    local min=${1:-0}
    local max=${2:-$((min+32767))} # Default max of bash
    seq $min $max | shuf -n 1
}

Also, dash (POSIX?) requires a file to have execute permission, to source it with '.' (while the actual command 'source' is a bashism anyway).


``Common sense is nothing more than a deposit of prejudices laid down by the mind before you reach eighteen.''
~ Albert Einstein

Offline

#1095 2010-06-07 16:11:13

lagagnon
Member
From: an Island in the Pacific...
Registered: 2009-12-10
Posts: 1,087
Website

Re: Post your handy self made command line utilities

Small Bash Function for Console Based Canadian Weather Text Forecasts
==========================================================

Canucks: place this function in your ~/.bashrc and type "wx" in a console to get an instant short and long range text weather forecast in your terminal.
NOTE: you must replace the http address with the correct Bulletin number for your city, and the sed regular expression with your city's name as it appears in the forecast.
See here: http://text.weatheroffice.gc.ca/forecas … ast_e.html

 wx(){
        wget -qO - http://text.weatheroffice.gc.ca/forecast/textforecast_e.html?Bulletin=fpcn11.cwvr | sed -n "/Greater Victoria./,/^$/p"
        wget -qO - http://text.weatheroffice.gc.ca/forecast/textforecast_e.html?Bulletin=fpcn50.cwvr | sed -n "/Greater Victoria./,/^$/p"
}

Philosophy is looking for a black cat in a dark room. Metaphysics is looking for a black cat in a dark room that isn't there. Religion is looking for a black cat in a dark room that isn't there and shouting "I found it!". Science is looking for a black cat in a dark room with a flashlight.

Offline

#1096 2010-06-07 16:35:10

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

Here is what I use:

alias weather="w3m -dump http://www.theweathernetwork.com/weather/CAON0282 | grep -E -A10 'Short Term Forecast|Long Term Forecast' | awk '! /^Daytime/ && ! /^Conditions/' | uniq"

It's kinda gross but works well.

Offline

#1097 2010-06-09 13:14:15

jhvid
Member
From: /dev/denmark
Registered: 2009-08-19
Posts: 52

Re: Post your handy self made command line utilities

Two very simple and very useful (i think) ruby scripts: smile

highlight.rb
Highlightes it's first argument in the output of the second argument (ran as a command)

#!/usr/bin/ruby

text    = ARGV[0]
command = ARGV[1]
color   = "41"   # TODO: can't get it to work with ARGV[2]

output = `#{command}`
puts output.gsub(/(#{text})/, "\e[#{color}m\\1\e[0m")

nicefile.rb
Takes a filename and renames the file to look better (no spaces, no special charaters and that stuff)

#!/usr/bin/ruby -w
require "fileutils"
#
# Convert file names to something more non-ugly.
#
# Examples:
#   nicefile.rb "Some Ugly File Name  <<by TheMaster!>>.JPG"
#   gives: some_ugly_file_name_by_themaster_.jpg
#
#   nicefile.rb "WHOtheFUCK_named this file?????%%"
#   gives: whothefuck_named_this_file
#

BEFORE = ARGV[0]
after  = BEFORE.dup

after.gsub!(" ", "_")
after.downcase!
after.gsub!(/[^a-zA-z0-9._]/, "")
after.squeeze!("_")
after.squeeze!("")

FileUtils.mv BEFORE, after

Last edited by jhvid (2010-06-09 13:15:39)


GCS d- s-:+ a12 C++++ U++++ L+++ P--- E--- W+++ N+ o K- w--- O? M-- V? PS PE
Y- PGP? t? 5? X? R tv b+++ DI+ D- G++ e++ h! !r !y

Please ignore me... I'm pretty weird...

Offline

#1098 2010-06-09 14:20:23

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

For nicefile.rb I use this in my $HOME/.bashrc

sanitize() { for i in "$@" ; do j=$(echo "$i" | tr A-Z a-z | tr ' ' _) && mv "$i" "$j" ; done ; }

Offline

#1099 2010-06-09 14:54:52

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

You should use '[[:upper:]]' and '[[:lower:]]' for tr, since it will respect locales.

Offline

#1100 2010-06-09 16:39:08

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

Nice.  Thank you.

Offline

Board footer

Powered by FluxBB