You are not logged in.

#1301 2010-09-08 14:19:43

hellomynameisphil
Member
From: /home/phil/Vancouver
Registered: 2009-10-02
Posts: 257
Website

Re: Post your handy self made command line utilities

GraveyardPC wrote:

I prefer:

mplist(){ mplayer -playlist <(tree -aif "$1" | awk '/\.(mp3|wav|ogg|avi|mpg|iso)$/'); }

Why use 'tree | awk' instead of 'find'? Just curious, not challenging.

Offline

#1302 2010-09-08 14:47:06

JeffreyV
Member
Registered: 2010-07-24
Posts: 15

Re: Post your handy self made command line utilities

hellomynameisphil wrote:
GraveyardPC wrote:

I prefer:

mplist(){ mplayer -playlist <(tree -aif "$1" | awk '/\.(mp3|wav|ogg|avi|mpg|iso)$/'); }

Why use 'tree | awk' instead of 'find'? Just curious, not challenging.

Asking myself the same question, might be a lot shorter, but I did need a pacman -S tree to perform it

Offline

#1303 2010-09-09 23:13:33

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

Re: Post your handy self made command line utilities

Hatton wrote:

I use

#!/bin/bash
echo "For what do you want to make a password?"
read name
echo "How long?"
read len
pw=$(dd if=/dev/urandom count=1 2> /dev/null | uuencode -m -| sed -ne 2p | cut -c-$len)
echo $name ';' $pw >> ~/password/file
cat ~/password/file | grep $pw

for that, found it somewhere on the internet and modified it for my own uses.

A while ago I decided to make a nice and compact version of my own (I've puth this in my zshrc):

mkpw () { cat /dev/urandom | tr -dc "[:print:]\n" | grep ".\{16\}" | cut -b -16 | head -n `[[ "$1" == "" ]] && echo 1 || echo "$1"`}

Offline

#1304 2010-09-10 04:56:48

k3ttc4r
Member
From: around somewhere
Registered: 2008-12-28
Posts: 137
Website

Re: Post your handy self made command line utilities

Simple script to set a new wallpaper. Restarts ROX and conky to make the change take effect.
Uses ImageMagick to resize and crop-to-fit the image and saves it in a specified path, which is also used by SLiM, so that I have the same background in SLiM and OB.

I use it as a custom action in Thunar.

#!/bin/bash

#   makewall
#   A simple script to resize and crop-to-fit an image to use as Wallpaper
#   Usage: makewall IMAGE
#
#   Christian Brassat <christian@crshd.cc>

# Display help
if [ "$1" = "-h" ]; then
  echo "Usage: makewall IMAGE"
else
  # GET TO WORK
  # Resize Image to Fit
  convert "$1" -resize 1280x800^ -gravity Center -crop 1280x800+0+0 +repage /home/christian/Bilder/Wallpapers/current.png
  # Set as Wallpaper
  feh --bg-center /home/christian/Bilder/Wallpapers/current.png
  # Restart ROX to update Wall
  killall ROX-Filer
  ( rox -S ) &
  # Restart Conky
  killall conky
  ( conky -c /home/christian/.config/conky/conkyrc ) &
  ( conky -c /home/christian/.config/conky/conkyrc2 ) &
  ( conky -c /home/christian/.config/conky/conkyrc3 ) &
fi

Last edited by k3ttc4r (2010-09-10 04:59:28)


arch64   ||   github | dotshare | blog

Offline

#1305 2010-09-10 15:17:59

GraveyardPC
Member
Registered: 2008-11-29
Posts: 99

Re: Post your handy self made command line utilities

hellomynameisphil wrote:
GraveyardPC wrote:

I prefer:

mplist(){ mplayer -playlist <(tree -aif "$1" | awk '/\.(mp3|wav|ogg|avi|mpg|iso)$/'); }

Why use 'tree | awk' instead of 'find'? Just curious, not challenging.

No specific reason, find works equally well there.

EDIT: Oh, I remember why. I just used tree really quick one day and was doing more with awk than just that expression. Then I shortened it and just never changed it. That was a direct copy/paste from my bashrc.

EDIT2: I just created an alternate method, but you still must use both find and sort to get the same result as tree sorts by default:

mplist(){ mplayer -playlist <(find "$1" -regex '.*\.\(mp3\|wav\|ogg\|avi\|mpg\|iso\)' | sort); }

My original took (real: 0.013s, user: 0.010s) vs. find/sort (real: 0.009s, user: 0.003s). So I guess find is faster.

Last edited by GraveyardPC (2010-09-10 15:51:36)

Offline

#1306 2010-09-10 16:34:41

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

Re: Post your handy self made command line utilities

if you didn't mind the unsorted-ness you could actually do it without the subshell entirely:

find "$1" -regex '.*(wh|at|ev|er)' -exec mplayer -playlist {} \+

Last edited by brisbin33 (2010-09-10 16:35:54)

Offline

#1307 2010-09-10 16:58:19

GraveyardPC
Member
Registered: 2008-11-29
Posts: 99

Re: Post your handy self made command line utilities

brisbin33 wrote:

if you didn't mind the unsorted-ness you could actually do it without the subshell entirely:

find "$1" -regex '.*(wh|at|ev|er)' -exec mplayer -playlist {} \+

This won't work, because the result would look like:

mplayer -playlist file.avi file2.avi file3.avi ...etc

And mplayer would actually open "file.avi" and read it as a playlist instead of appending it to the list. So you'll see stuff like:

Playing /dir1/dir2/¶[Ï01wbÀ.
File not found: '/dir1/dir2/¶[Ï01wbÀ'
Failed to open /dir1/dir2/¶[Ï01wbÀ.

where it tried to load file paths from the binary information of "file.avi".

Last edited by GraveyardPC (2010-09-10 16:59:33)

Offline

#1308 2010-09-10 17:02:41

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

Re: Post your handy self made command line utilities

Drop the '-playlist' part.

Offline

#1309 2010-09-10 17:46:00

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

Re: Post your handy self made command line utilities

karol wrote:

Drop the '-playlist' part.

Yes.  I was unfamiliar with that option, sorry.

Offline

#1310 2010-09-10 18:00:07

GraveyardPC
Member
Registered: 2008-11-29
Posts: 99

Re: Post your handy self made command line utilities

brisbin33 wrote:
karol wrote:

Drop the '-playlist' part.

Yes.  I was unfamiliar with that option, sorry.

Heh, no need to apologize. In fact right now I'm writing an mplayer frontend just to circumvent these problems. It's gtk based but has the feel of curses, with navigation like mc + ncmpcpp for playlist operation.

Last edited by GraveyardPC (2010-09-10 18:01:22)

Offline

#1311 2010-09-20 23:28:33

Lucian
Member
From: Indianapolis, IN
Registered: 2010-09-07
Posts: 6

Re: Post your handy self made command line utilities

Utilizes LS to give it headings, detailing what each column represents. File sizes are in human readable format, meaning KB, MB, GB, etc. A total directory size is given at the bottom if not pointed to a file.

function listit() 
{
    local dVar=''
    if [ ! "$1" ]; then
        dVar="$PWD"
    else
        dVar="$1"
    fi
    
    if [ ! -f $dVar ]; then
        (printf "PERM LINKS OWNER GROUP SIZE DATE TIME FILENAME\n---- ----- ----- ----- ---- ---- ---- --------\n"; ls -lh $dVar | sed 1d) | column -t
        echo "Total: $(du -hc | tail -n1 | cut --fields=01)"
    else
        (printf "PERM LINKS OWNER GROUP SIZE DATE TIME FILENAME\n---- ----- ----- ----- ---- ---- ---- --------\n"; ls -lh $dVar) | column -t
    fi

}

To use, simply place in your .bashrc file then source it. Afterwards, just type listit or listit <dir/file>

Offline

#1312 2010-09-20 23:36:34

Lucian
Member
From: Indianapolis, IN
Registered: 2010-09-07
Posts: 6

Re: Post your handy self made command line utilities

The following is what I wrote to display Pacman updates in Conky. How to use it is commented in to the script. Language = Python

#!/usr/bin/python

# Script Name: checkupdates.py
# Author: Lucian Adamson <lucian.adamson@yahoo.com>
# What This Script Does: Formats how the output of pacman updates in conky looks
#
# Examples:
# Say you want to output your pacman updates to conky but only want the top 5 to show
# using the colors red, blue, and green for each line and you wish to have the number of updates
# in a header, you would do this:
#
# ${execpi 900 /path/to/checkupdates.py -e 'Updates ($n):' -c 'red,blue,green' -n 5}
# OR using long options
# ${execpi 900 /path/to/checkupdates.py --header='Updates ($n):' --colors='red,blue,green' --number-of-updates=10}
#
# This function can be run from the command line and there is a -h, --help option
# /path/to/checkupdates.py --help
#
# You can use this script to just output the number of updates like this:
# /path/to/checkupdates.py -g
#
# Sample output of checkupdates.py --colors='red,white,blue' -e '$n updates'
#
#6 updates
#${color red}chromium 5.0.375.125-1$color
#${color white}libpciaccess 0.11.0-1$color
#${color blue}mutagen 1.19-1$color
#${color red}orc 0.4.6-1$color
#${color white}q4wine 0.119-1$color
#${color blue}wine 1.3.0-1$color


from sys import argv as gv
from optparse import OptionParser as op
import re

onecolor=False # Variable that will change if more than one color is given

#--------------------------- Run a shell command and retrieve output --------------------------
class Command(object):
    def __init__(self, command):
        self.command = command
    def run(self, shell=True):
        import subprocess as sp
        process = sp.Popen(self.command, shell = shell, stdout = sp.PIPE, stderr = sp.PIPE)
        self.pid = process.pid
        self.output,self.error = process.communicate()
        self.failed = process.returncode
        return self
    
    @property
    
    def returncode(self):
        return self.failed

# ------------------------ Parse command line options and arguments ------------------------
parser = op()
parser.add_option('-c', '--colors', action='store', default='orange,red', help='Supply either one or more colors in comma separated values. Example: orange,red,blue')
parser.add_option('-n', '--number-of-updates', action='store', default=10, type='int', help='The number of maximum updates to display in Conky.')
parser.add_option('-g', '--get-number', action='store_true', default=False, help='Returns the number of updates available through pacman.')
parser.add_option('-e', '--header', action='store', default='', help='Sets a header for your conky output. Replacable variables are $n for number of updates.')
(options, args) = parser.parse_args()

# --------------------------------------------- The -g flag ------------------------------------------------

theupdates = Command("pacman -Qu").run()
theupdates_ord = theupdates.output
theupdates = theupdates_ord.split("\n") # Split output of -Qu by line
numdates = len(theupdates) # Count the lines
if numdates > 0: # if there are actually updates
    try:
        theupdates.remove('') #remove the blank line at end
        numdates=len(theupdates) # recount
    except ValueError:
        pass # If error because there is no blank line, keep going
if options.get_number:
    print numdates
    quit()

# ------------------------------------------ Set Variables -------------------------------------------
numupdates = options.number_of_updates #Variables set by optparse
colors = options.colors
header = options.header
# ----------------------------------------- Parse the Colors -----------------------------------------

colors = colors.lower() # convert text to lowercase
if re.search(',', colors): # if more than one color
    colors = colors.split(',') # split them
    for x in range(0, len(colors), 1): # loop through and remove unnec spaces
        colors[x] = colors[x].strip(' ')
    colorlist_len = len(colors) # This counts the colors
else:
    onecolor=True

# --------------------------------------------- The Code -----------------------------------------------

pacup_split = theupdates 

i = 1
pHolder = ''
totalnow = 1
if not header=='': # If there is a header...
    header = header.replace("$n", str(numdates)) #...replace $n with num of updates
    pHolder = header + '\n' # and append if to pHolder variable for output with actual updates
    
if onecolor== True: # if there is only one color
    for each in pacup_split: # loop through
        if not totalnow > numupdates: # makes sure we only display the alloted number of updates
            pHolder = pHolder + "${color " + colors + "}" + each + "$color\n"
        else:
            pHolder = pHolder.rstrip('\n') + '...\n' #if there are more, we recognize that by adding 3 periods
            break
        totalnow += 1
    print pHolder.rstrip('\n') # strip off the remaining new line to conserve our conky space
    quit() #we're done so quit the script

for newout in pacup_split: # this is the code for more than one color
    if i > colorlist_len: i = 1 # if i is greater than number of colors, then reset i
    if not totalnow > numupdates:
        pHolder = pHolder + '${color ' + colors[i - 1] + '}' + newout + '$color\n'
    else:
        pHolder = pHolder.rstrip('\n') + '...\n' # we put a \n to conform to line of code above
        break
    i += 1 # increase i by one until it is reset
    totalnow += 1 # helps make sure we only display the correct number of updates

print pHolder.rstrip('\n') #print the updates without the trailing \n

-- mod edit: added another post from the wrong thread --

The following script was originally written by a now banned user, Evil. It is still posted in this thread somewhere but he has given me explicit permission to make the necessary re-writes. The bug/issue was that if you renamed using a prefix that was already in use then you would lose some of your files. This is because if filename "SeniorPictures-01.jpg" was already in use, and you renamed another file to that, then you would lose the original picture. What I have done here is during a rename, it renames all the files to temporary names using a random number scheme, then names each file according to your set prefix. I felt the need to make this re-write because I love the script as I have used it many, many times and found out of this bug. Fortunately, I always keep backups of everything so there was no harm.

#!/usr/bin/python

# Script Name: pyrename
# Original Author: Evil
# New Author: Lucian Adamson <lucian.adamson@yahoo.com>
# Version: 2.0
# Date: 09/17/2010


# IMPORT LIBRARYS
import os, sys, random
from optparse import OptionParser

# GENERATE CLI OPTIONS
parser = OptionParser()
parser.add_option("-f", "--filetype", metavar="filetype",default=".jpg", help="Choose file type to rename", type="string", dest="ren_filetype")
parser.add_option("-p", "--prefix", metavar="prefix", default="Pictures", help="Choose a filename prefix", type="string", dest="ren_prefix")
parser.add_option("-s", "--startcount", metavar="startcount", default=1, help="Choose what number to start counting from", type="int", dest="ren_startcount")
parser.add_option("-d", "--directory", metavar="directory", default=".", help="Define a different directory besides the working directory", type="string", dest="ren_directory")

#PARSE OPTIONS
(options, args) = parser.parse_args()

# ASSIGN TO SHORTER VARIABLES
ren_filetype = options.ren_filetype
ren_prefix = options.ren_prefix
ren_startcount = options.ren_startcount
ren_directory = options.ren_directory

#GENERATE A RANDOM NUMBER
randint = random.randint(10000, 100000)

#GET DIRECTORY LISTING
directoryListing=os.listdir(ren_directory)
#GET COUNT ON LISTING
listingCount = len(directoryListing)
#GET NUMBER OF DIGITS IN LIST COUNT
zeroCount = len(str(listingCount))

# RENAME FILES TO SOMETHING RANDOM
# Explanation: If you start to rename a directory of jpgs that are in this 
#                     format: IllinoisTrip-##.jpg and are using the same prefix
#                     then if this step is skipped, you will lose some files due to
#                     the renaming convention using already existing file names.
#                     To prevent that, we create temp files then rename using the
#                     prefix desired. Therefore, solving the dilemma.
for tmp in directoryListing[:]:
    if tmp.endswith(ren_filetype):
        zeroMaker = ('%(#)0' + str(zeroCount) + 'd') % {"#":ren_startcount}
        os.rename(tmp, 'd' + zeroMaker + ren_filetype)
        ren_startcount += 1

# Reset our variables, getting a new listing, list count and digit count
# as well as resetting the starting count
directoryListing=os.listdir(ren_directory)
listingCount = len(directoryListing)
zeroCount = len(str(listingCount))
ren_startcount = options.ren_startcount

# Go ahead and rename files accordingly
for renLoop in directoryListing[:]:
    if renLoop.endswith(ren_filetype):
        zeroMaker = ('%(#)0' + str(zeroCount) + 'd') % {"#":ren_startcount}
        os.rename(renLoop, ren_prefix + '-' + zeroMaker + ren_filetype)
        ren_startcount += 1

Last edited by jasonwryan (2010-09-20 23:49:27)

Offline

#1313 2010-09-21 21:11:43

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

Re: Post your handy self made command line utilities

This script does what the help says it does. A quick summary though is that it enables you to sort through images using feh.

***WARNING*** you can delete images, using this script.

I used a more simplified version for many months, but thought I should sort the TODOs and add some help before posting it here. Therefore some parts (mainly the options) are not as heavily tested (and as they impacted on the main body, that is the same) though it has been in use for a while.

The help was added just now, so there may be typos and errors in it.

#!/bin/bash
#
# imgreview
#
# Enable to go through a folder of images and move them to
# a specified folder, or delete them.
#
# $1 - $9 dirs to move images to 1 - 9 to select
# <Enter> to delete
#
# Depends on feh
#
# -a option needs to be wrapped in "'....'" to work
#

THIS=$(basename $0)
let INDEX=0
ACTION="--action 'rm %f'"
MOVE=1
SRC=.

usage () { echo "Usage: $THIS [-a action] [-c] [-h] [-s source dir] [[[dir1] [dir2]...[dir9]]" ; }

help () {
cat << ENDHELP
  $THIS: Uses feh(1) to sort through a list of images in the current directory.
         Using feh's ACTIONS the user can rm(1) or mv(1) the displayed image to
         one of a specified set of directories. These are default actions that 
         can be overridden using the options.

         When an image is displayed, pressing <ENTER> will delete the file.

         When an image is displayed, pressing the appropriate number key (1-9)
         will move the file to that respective directory, as long as that number
         of directories were passed to the script. That is, pressing 1 will
         move the file to the first directory passed, etc.

         Normal feh commands are all usable.

         <ESCAPE> is used to exit feh, and the script, even if all the files
         have been deleted or moved.

         Options:
                 -a "'...'"    changes the <ENTER> action to what is passed in.
                               The action is any shell command, and must be
                               wrapped in "' '".

                 -c            changes the behaviour of the numeric keys from
                               mv to cp(1)

                 -s directory  changes the source directory for the images.

                 -h           display this help and exit

ENDHELP
}

# getopts
while getopts "a:cs:h" opt; do
    case $opt in
       a  ) ACTION="--action $OPTARG" ;;
       c  ) MOVE=0           ;;
       s  ) SRC="$OPTARG"    ;;
       h  ) help
            exit 0           ;;
       \? ) usage
            exit 1
    esac
done
shift $(($OPTIND -1))

ACTIONSTRING="--draw-actions $ACTION"

# loop through inputs building action string
for DIR in "$@"
do
    INDEX=$(($INDEX + 1))
    if [ $INDEX -gt 9 ]; then
        break
    fi
    if [ $MOVE = 1 ]; then
        ACTIONSTRING="$ACTIONSTRING --action${INDEX} 'mv %f ${DIR}/%n'"
    else
        ACTIONSTRING="$ACTIONSTRING --action${INDEX} 'cp %f ${DIR}/%n'"
    fi
done
echo $ACTIONSTRING

# feh command
eval feh  -dF $ACTIONSTRING ${SRC}/*

#EOF

This script (has been posted elsewhere) is used to rename images to their date, time and original name. The format can be altered. It depends on exiftool.
The regular expression at the start stops it acting on already renamed files.

The commented out lines show an alternative method that could use a different exif info. tool (and indeed, used to).

#!/bin/bash
#
# renimg
#
# Renames image file based on Exif creation date & time
#

# Ignore already formatted imgs
for f in *
do
    if [[ ! $f =~ [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]-.*\.[[:alpha:]][[:alpha:]][[:alpha:]] ]]
    then
        exiftool '-filename<CreateDate' -d "%Y-%m-%d-%H%M%S-$f" $f > /dev/null 2>renimg.log
        #dt="$(exiftool -CreateDate $f)"
        #d="$(echo $dt | awk '{print $4 }' | sed 's/:/-/g')"
        #t="$(echo $dt | awk '{print $5 }' | sed 's/://g')"
        #name="$d-$t-$f"
        #echo $f -- $name
        #mv $f $name
    fi
done

# EOF

"...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

#1314 2010-09-23 15:13:35

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: Post your handy self made command line utilities

i used to use ossvol but never liked the idea of temp files for storing volume, etc. perhaps it was written when oss didn't support mute? anyway, it's been a long time since i looked at the original code but i decided to take another look and ended up revamping everything completely. written in zsh (i dont think it will work in bash unless edited).. and a few variables to define:
so here it is; a fork(?) of ossvol:

#!/bin/zsh
# -------------------------------------------------------------------
# author: milomouse <vincent[at]fea.st>
# credit: original by Daniel J Griffiths <ghost1227[at]archlinux.us>
# -INFO:-------------------------------------------------------------
# a fork of `ossvol' with major changes, no more tmp file, etc.
# bears no real resemblance to original script. concept only.
# -------------------------------------------------------------------

## NOTE: USER VARIABLES,
# define channels for master outvol and mute toggling:
master_channel="vmix0-outvol"
mute_channel="codec1.misc.pcm1-mute"
# toggle between headphones and pc speaker,
# define each mixer for strict compatibility:
headphonetoggle="codec1.jack.int-speaker.mode"
  headphone_mix="mix3"
  pcspeaker_mix="mix2"

## NOTE: SCRIPT VARIABLES,
# do not change these:
current_vol=${${$(ossmix|grep ${master_channel})[-2]}[1,2]}
argument=${${${${@:/$1}//-}//[a-z]}// }

## NOTE: FUNCTIONS,
# do not chese these [unless you know what you're doing]:
# first check to see if argument is valid:
function __intcheck() {
  if [[ -z ${argument} ]]; then :
  elif [[ ${argument} != <-> ]]; then
       print argument not accepted. must be a valid integer. ; exit 1
  else : ; fi
}

# set a static volume level for ${master_channel}:
function setvol() {
  if [[ ${argument} != <-> ]]; then print argument is not an integer
  elif [[ ${argument} = <-> \
       && ${argument} -le 25 \
       && ${argument} -ge 1 ]]; then
    ossmix ${_v} ${master_channel} ${argument}
  fi
}

# toggle outvol of ${mute_channel}:
function mute() {
  if [[ ${$(ossmix|grep ${mute_channel})[-1]} =~ OFF ]]; then
       ossmix ${_v} ${mute_channel} ON
  else ossmix ${_v} ${mute_channel} OFF ; fi
}

# increase volume by 1, or by valid Integer,
# do not allow increase to over-extend proper values:
function increase() {
  __intcheck
  if [[ -z ${argument} ]]; then
    ossmix ${_v} ${master_channel} +1 ; else
    if [[ ${argument} -gt $((25 - ${current_vol})) ]]; then exit 1
    elif [[ ${argument} -le 0 ]]; then exit 1
    else ossmix ${_v} ${master_channel} $((${current_vol} + ${argument}))
    fi ; fi
}

# decrease volume by 1, or by valid Integer,
# do not allow decrease to over-extend proper values:
function decrease() {
  __intcheck
  if [[ -z ${argument} ]]; then
    if [[ ${current_vol} == 0. ]]; then exit 0 ; else
    ossmix ${_v} ${master_channel} -- -1 ; fi ; else
    if [[ ${argument} -gt ${current_vol} ]]; then exit 1
    elif [[ ${argument} -le 0 ]]; then exit 1
    else ossmix ${_v} ${master_channel} -- $((${current_vol} - ${argument}))
    fi ; fi
}

# toggle ${headphone_channel} to/from headphones <-> speakers:
function toggle() {
  if [[ $(print ${$(ossmix|grep ${headphonetoggle})[-1]}) =~ \
    ${headphone_mix} ]]; then
       ossmix ${_v} ${headphonetoggle} ${pcspeaker_mix}
  else ossmix ${_v} ${headphonetoggle} ${headphone_mix} ; fi
}

# output current volume of ${master_channel}:
function announce() { print ${$(ossmix|grep ${master_channel})[-2]} dB }

# print proper script usage and exit:
usage() {
  << EOF
usage: ossvol [option(s)] [argument]

options:
     -i, --increase - increase volume by 1 or [argument]
     -d, --decrease - decrease volume by 1 or [argument]
     -s, --set      - set master volume level to [argument]
     -m, --mute     - toggle mute/unmute
     -t, --toggle   - toggle speaker/headphones
     -a, --announce - display current volume level
     -h, --help     - display this
         -,--quiet  - do not display output of command
EOF
exit 0
}

## NOTE: [REAL-TIME] SCRIPT EXECUTION,
# simple cli parsing to get valid options:
if [[ ${${@//[0-9]}//-} =~ quiet ]]; then
  _v="-q" ; else _v=() ; fi
case ${${${${@//[0-9]}//-}//quiet}// } {
  'i'|'incresae') increase ;;
  'd'|'decrease') decrease ;;
  's'|'set')      setvol ;;
  'm'|'mute')     mute ;;
  't'|'toggle')   toggle ;;
  'a'|'announce') announce ;;
  'h'|'help'|*)   usage ;;
}

not really a big deal but i find it useful.

edit: added set option

Last edited by milomouse (2010-10-14 21:32:51)

Offline

#1315 2010-09-24 23:49:29

SiD
Member
From: Germany
Registered: 2006-09-21
Posts: 729

Re: Post your handy self made command line utilities

I just wanted to improve my C skills (I'm still beginner). I found the GeoIP library and played a bit with it.
So here is the result. The "geoip" package must be installed and you need the GeoLiteCity.dat database file from http://www.maxmind.com/app/geolitecity

/*
Get informations about IP addresses using the GeoIP lib
and the GeoLiteCity database file from http://www.maxmind.com/app/ip-location
The GeoLiteCity database is updated every 1th of a month.
*/

#include <stdio.h>
#include <stdlib.h>
#include <GeoIP.h>
#include <GeoIPCity.h>

void usage(void)
{
    printf("\nusage: geoip <ip-address>  or  geoip <domain>\n");
    printf("\nExample: geoip 74.125.39.103  or  geoip google.com\n\n");
}

static const char * mk_NA( const char * p )
{
 return p ? p : "Not Available";
}

int main (int argc, char *argv[]) 
{
    GeoIP * gi;
    GeoIPRecord * gir;
    char *city_db;
    
    if (argc != 2)
    {
        usage();
        return EXIT_FAILURE;
    }
    
    city_db = "/home/michael/downloads/GeoLiteCity.dat";
    gi = GeoIP_open(city_db, GEOIP_STANDARD);
    gir = GeoIP_record_by_name(gi, argv[1]);

    if (gir == NULL)
    {
        printf("IP Address not found.\n");
        return EXIT_FAILURE;
    }
    else
    {
        printf("\nCountry Code: %s\n",gir->country_code);
        printf("Country Name: %s\n",mk_NA(gir->country_name));
        if (mk_NA(gir->region) == gir->region)
        {
            printf("Region Code: %s\n",gir->region);
            printf("Region Name: %s\n",
                    GeoIP_region_name_by_code(gir->country_code, gir->region));
        }
        else
        {
            printf("Region: Not Available\n");
        }
        printf("City: %s\n",mk_NA(gir->city));
        printf("Postal Code: %s\n",mk_NA(gir->postal_code));
        printf("Latitude: %f\n",gir->latitude);
        printf("Longitude: %f\n\n",gir->longitude);
        GeoIP_delete(gi);
        GeoIPRecord_delete(gir);
    }

    return EXIT_SUCCESS;
}

Last edited by SiD (2010-09-24 23:50:54)

Offline

#1316 2010-09-24 23:52:16

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

Re: Post your handy self made command line utilities

Nice! You may want to poke around stackoverflow or superuser, I remember someone asking about a local geoip library to use in something.

Offline

#1317 2010-09-27 01:47:06

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Post your handy self made command line utilities

I'm currently learning Bash scripting, so I wrote this little favorites style script, I think it's pretty handy.

#!/bin/bash
#####
# Xplorer - A launcher to various programs, similar to favorites
# Author - Mega-G33k
#####
RUNNING=1
while [ $RUNNING -eq 1 ]; do
   clear
   echo "Date: $(date)"
   echo "Welcome to \"Xplorer\""
   echo ""
   echo "1 - Midnight Commander"
   echo "2 - Links"
   echo "3 - Mutt"
   echo "4 - Newsbeuter"
   echo "5 - Podbeuter"
   echo "6 - NCMPC"
   echo "7 - Irssi"
   echo "8 - CenterIM"
   echo "9 - Start X"
   echo "0 - Exit"
   echo ""
   echo -n "Please enter an option[0-9] "
   read boole
   if [ $boole -eq 1 ]; then
      mc
   elif [ $boole -eq 2 ]; then
      links
   elif [ $boole -eq 3 ]; then
      mutt
   elif [ $boole -eq 4 ]; then
      newsbeuter
   elif [ $boole -eq 5 ]; then
      podbeuter
   elif [ $boole -eq 6 ]; then
      ncmpc
   elif [ $boole -eq 7 ]; then
      irssi
   elif [ $boole -eq 8 ]; then
      centerim
   elif [ $boole -eq 9 ]; then
      if ps -A | grep X &> /dev/null; then
         echo "X has already been started"
         read -n 1 -s
      else
         startx
      fi
   elif [ $boole -eq 0 ]; then
      exit 0
   else
      echo "Invalid option entered"
      read -n 1 -s
   fi
done

BTW, if anybody knows how to keep the date at the top refreshed it would be helpful, thanks.

Offline

#1318 2010-09-27 01:53:41

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

Re: Post your handy self made command line utilities

Mega-G33k wrote:

I'm currently learning Bash scripting, so I wrote this little favorites style script, I think it's pretty handy.

#!/bin/bash
#####
# Xplorer - A launcher to various programs, similar to favorites
# Author - Mega-G33k
#####
RUNNING=1
while [ $RUNNING -eq 1 ]; do
   clear
   echo "Date: $(date)"
   echo "Welcome to \"Xplorer\""
   echo ""
   echo "1 - Midnight Commander"
   echo "2 - Links"
   echo "3 - Mutt"
   echo "4 - Newsbeuter"
   echo "5 - Podbeuter"
   echo "6 - NCMPC"
   echo "7 - Irssi"
   echo "8 - CenterIM"
   echo "9 - Start X"
   echo "0 - Exit"
   echo ""
   echo -n "Please enter an option[0-9] "
   read boole
   if [ $boole -eq 1 ]; then
      mc
   elif [ $boole -eq 2 ]; then
      links
   elif [ $boole -eq 3 ]; then
      mutt
   elif [ $boole -eq 4 ]; then
      newsbeuter
   elif [ $boole -eq 5 ]; then
      podbeuter
   elif [ $boole -eq 6 ]; then
      ncmpc
   elif [ $boole -eq 7 ]; then
      irssi
   elif [ $boole -eq 8 ]; then
      centerim
   elif [ $boole -eq 9 ]; then
      if ps -A | grep X &> /dev/null; then
         echo "X has already been started"
         read -n 1 -s
      else
         startx
      fi
   elif [ $boole -eq 0 ]; then
      exit 0
   else
      echo "Invalid option entered"
      read -n 1 -s
   fi
done

BTW, if anybody knows how to keep the date at the top refreshed it would be helpful, thanks.

I suggest case instead of elifs, like so:

archive () {
  FILE=$1
  shift
  case $FILE in
    *.tar.bz2) tar -cjf $FILE $* ;;
    *.tar.gz) tar -czf $FILE $* ;;
    *.tar.lzma) tar --lzma -cf $FILE $* ;;
    *.tar.xz) tar --xz -cf $FILE $* ;;
    *.tgz) tar -czf $FILE $* ;;
    *.zip) zip $FILE $* ;;
    *.rar) rar $FILE $* ;;
  esac
}

Offline

#1319 2010-09-27 02:00:25

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

Re: Post your handy self made command line utilities

Or better yet, use select:

#!/bin/bash

progs=('mc' 'links' 'mutt' 'newsbeuter' 'ncmpc' 'irssi' 'centerm' 'startx')

select prog in ${progs[@]}; do
  (( REPLY > ${#progs[@]} || REPLY < 0 )) && continue
  break
done

# define quirks in an if else, or just start it up
if [[ $prog == startx ]]; then
  pidof X && echo "X is already running" || $prog
else
  $prog
fi

Last edited by falconindy (2010-09-27 02:00:54)

Offline

#1320 2010-09-27 02:08:36

Mega-G33k
Member
From: Indiana, USA
Registered: 2009-09-14
Posts: 42

Re: Post your handy self made command line utilities

Thanks for your suggestions falconindy and karol, I will take those into consideration and learn the syntax for those commands.

Offline

#1321 2010-09-27 08:08:17

pyknite
Member
Registered: 2010-03-03
Posts: 166

Re: Post your handy self made command line utilities

very simple and totally inefficace on big dir:

ff() { find $1 | xargs grep $2 ;}

Search in some path ($1) for file containing the string $2

Offline

#1322 2010-09-27 09:28:14

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

Re: Post your handy self made command line utilities

@pyknite, why not 'grep -R "$2" "$1"'?

Offline

#1323 2010-09-27 09:37:23

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

Re: Post your handy self made command line utilities

jac wrote:

@pyknite, why not 'grep -R "$2" "$1"'?

Run it on / or something equally big - it may crash if it runs out of memory and it does even if you have 2 GB RAM if the folder is big enough.
I use

find "$1" \! -name "*~" -type f -exec grep --color=always -iwsI "$2" {} \; -print

for searching something in my text documents - the folder is only a couple MB big, so results are almost instant.

Offline

#1324 2010-09-27 13:22:58

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

Re: Post your handy self made command line utilities

karol wrote:
find "$1" \! -name "*~" -type f -exec grep --color=always -iwsI "$2" {} \; -print

I think this way is much better for big directories, -exec \; will run the grep once per match, whereas the xargs approach will build the list of files to search first then pass them all to one grep instance (similar to -exec +).  This way, you can see results as they match and you don't have to first wait for the arguments to be built to completion.  Also, grep has an internal limit on the number of files it can accept for one search anyway.

I typically just use this if I know there are few enough files in the directory I'm searching:

grep pattern ./**

it requires bash >= 4 and `shopt -s globstar` in bashrc to enable the ** globbing.

Offline

#1325 2010-09-27 14:48:56

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

Re: Post your handy self made command line utilities

Quite often I need to open one particular file in a directory (or tree) containing a lot of files, such that I usually use find -iname to get the filename. After copy pasting the filenames a few times I got annoyed enough to write this using select, as it isn't possible to use -exec or xargs if you have more than one match to your search term(s).

findopen() {
    if [[ $1 == '-exec' ]]; then
        cmd="$2"
        shift;shift
    else
        cmd=xdg-open
    fi
    local PS3="select file: "
    files=( $(find "$@" | sort | tr ' ' '|') )
    select file in "${files[@]//|/ }"; do
        (( REPLY > ${#files[@]} || REPLY <= 0 )) && continue
        $cmd "$file"
        break
    done
}

By default it just uses xdg-open (replace with mime-open or whatever if you want), but you can pass it a -exec option to define the command to use. I couldn't be bothered parsing -exec properly, so it needs to be passed as the first argument and only contain one term, but things like -exec 'ls -l' work at least. Also it is just the command, not including {} like find's -exec. I replace spaces with a random character (here I use a pipe character) so that the array loads properly for filenames with spaces in them.

@falconindy: thanks for the (( ... )) && continue bit, tis a good idea. smile

Offline

Board footer

Powered by FluxBB