You are not logged in.

#576 2009-10-04 06:24:22

lonecat
Member
Registered: 2008-06-18
Posts: 32

Re: Post your handy self made command line utilities

My 2 cents:
screenshot.sh:

#!/bin/bash
scrot -s -e 'rox $$(dirname $f)' -b

share_pic:

#!/usr/bin/env ruby
# vim: set ft:ruby:

# imageshack uploader
# Author: Jose Felix Gomez
require 'rubygems'
require 'mechanize'
require 'zerenity'

class Uploader
  POST_PATH    = "http://load.imageshack.us/"
  INPUT_NAME   = "fileupload"
  LINK_TEXT    = "link"
  HREF_MATCHER = "imageshack"

  attr_reader :link

  def initialize(img_path)
    @path = img_path
    upload(@path)
  end

  protected

  def upload(path)
    raise "File doesn't exist" unless File.exists? path
    @image = File.open(path)
    response = send_file(@image, POST_PATH, INPUT_NAME)
    return parse_response(response)
  ensure
    @image.close unless @image.nil? || @image.closed?
  end

  def send_file(file, receiver, name)
    agent = WWW::Mechanize.new
    return agent.post(receiver, name => file)
  end

  def parse_response(response)
    @link = (response/'a').find do |l|
      l.inner_text.match(/#{LINK_TEXT}/) &&
        l.get_attribute('href').match(/#{HREF_MATCHER}/)
    end
    @link = @link.get_attribute("href") if @link
    return @link if @link
    return false
  end
end

def valid_photo?(file)
  valid_types = %w{.jpg .png .jpeg .gif .bmp}
  return true if valid_types.include? File.extname(file)
  return false
end

if __FILE__ == $0
  begin
    if ARGV[0] && File.exists?(ARGV[0]) && valid_photo?(ARGV[0])
      upload = Uploader.new(ARGV[0])
      Zerenity::Info(:text => "The image was successfully uploaded to
                     Imageschack:\n#{upload.link}")
    else
      raise "Didn't specify a valid picture"
    end
  rescue => e
    Zerenity::Error(:text => "An error ocurred:\n#{e.message}\nBacktrace:
                    #{e.backtrace.join('\n')}")
  end
end

Offline

#577 2009-10-04 06:27:55

Lich
Member
Registered: 2009-09-13
Posts: 437

Re: Post your handy self made command line utilities

lolilolicon wrote:

2. You don't need raw[start + 18:start + (end - start)] instead of raw[start + 18:end]

Lol you are correct, I have no idea why I came up with that snake of a code. Was kinda tired when I wrote it I guess

Can you give a complete list of "LOC CODE" for easy reference? I can't seem to find one instead of the one-by-one lookup method.

I just went to http://www.accuweather.com/rss-center.asp , entered my city, and copied the code for it smile


Archlinux | ratpoison + evilwm | urxvtc | tmux

Offline

#578 2009-10-04 19:43:57

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

Re: Post your handy self made command line utilities

I use this script to track which arch posts I have seen as well output
to a file any unique posts.

I use vim in urxvt with the mark-and-yank perl plugin to open urls.  I
read from bottom to top.  I either open the url and delete the line or
delete the line.

I have all my rss feeds to go email which I read with mutt.  I decided I
would like to process this file when I read my email.  I do this by
symlinking a mail file to the output file.  One needs to leave the mail
headers at the top of the file, if they wish to have this feature:

    Date: Sun, 04 Oct 2009 14:36:58 -0400
    From: Steve
    To: Steve
    Subject: ArchLinux Forums

When the script writes to the file it will bump the timestamp to the
current time.  It does not mark the file as new.  I edit the file in
mutt with this key binding:

    macro generic,index,pager   E "<shell-escape>vim + /PATH/TO/FILE<enter>" "unique archlinux forum posts"

If one does not wish to have the output file managed with their email
client then comment out the 'cat "$af" line.

I'm new to bash scripts so feedback is welcomed.  Here is the script:

#!/bin/bash

script=${0##*/}
af=$HOME/doc/af
aflist=$HOME/doc/aflist
aftmp=$HOME/tmp/af.tmp

usage() {

    cat << EOF

    usage: $script

    This script is used to download the most recent archlinux.org forum posts.
    Forum posts being the posts' title along with its URL. It has the ability
    to track which posts the user has viewed and store the unread forum posts
    in a file.

EOF
}

if [[ "$#" -ne "0" ]]; then
    usage
    exit 1
fi

ps a | \
    grep -q "[0-9] vim /root/doc/af" && exit 0

wget -q -O - http://bbs.archlinux.org/search.php?action=show_24h | \
    grep -v stickytext | \
    grep "viewtopic.php?id=" | \
    awk -F'"' '{print $2 $3}' | \
    sed -e 's@</a> <span class=@@' | \
    sed -e 's@viewtopic.php?id=@@' | \
    awk -F'>' '{printf("%06d>%s", $1, $2 "\n")}' >> "$aflist"

sort -r "$aflist" | \
    uniq -w 6 > "$aftmp"

mv "$aftmp" "$aflist"

grep -v '^[0-9]*#' "$aflist" | \
    sed 's/0*//' | \
    awk -F'>' '{print "http://bbs.archlinux.org/viewtopic.php?id=" $1 " " $2}' >> "$af"

cat "$af" | \
    sed 1d | \
    awk -v var="$(echo "Date: $(date -R)")" 'BEGIN {print var}{print}' > "$af"

sed -i -e 's/^\([0-999999]*\)>/\1#/g' "$aflist"

Last edited by steve___ (2009-10-04 19:47:57)

Offline

#579 2009-10-04 19:45:55

scj
Member
From: Sweden
Registered: 2007-09-23
Posts: 158

Re: Post your handy self made command line utilities

#!/bin/dash

case "$1" in
  "")         $0 cmdlist | socat - tcp-connect:localhost:6600 ;; # netcat alternative: "") $0 cmdlist | sed '$a close' | nc localhost 6600 ;;
  "get")      shift; grep -i "^$1:" | sed 's|^'$1': \(.*\)|\1|i' ;;
  "filter")   shift; eval $(perl -e 'print join ("|",map { "grep -i " . $_ } @ARGV);' $*) ;;
  "cmdlist")  sed -e '1i command_list_begin' -e '$a command_list_end' ;;
  "add")      sed 's|\(.*\)|add \"\1\"|' ;;
  "queue")    gawk '{i--;printf "addid \"%s\" %d\n", $0, i}' ;;
  "newlist")  sed -e '1i clear' -e '$a play' ;;
  "toggle")   sed -e 's#state: \(stop\|pause\)#play#' -e 's#state: play#pause#' | tr 01 10 | tr -d : ;;
  "nextsong") $0 playlistid $($0 status | $0 get nextsongid) ;;
  "menu")     dmenu -xs -b -i -l 15 ;;
  "reset-pl") echo add / | $0 newlist |  $0 ;;
  "pick")     $0 play $($0 playlist | grep -v "^OK" | sed 's|:|: |' | $0 menu | cut -d : -f 1) ;;
  "pt")       $0 status | grep state | $0 toggle | $0 ;;
  "cs")       $0 currentsong ;;
  *)          echo $* | $0 ;;
esac

# vim:set sw=2 ts=2 et:

My latest mpd script, called m,which is almost like a forth-ish DSL for interacting with a mpd at the protocol level, instead of via mpc. The main idea is to query mpd directly, apply some transformations to the stream, and possibly pipe the result back again. First of all you have a bunch of words defined in the script, ignore those for one second. The words not defined are sent straight to the mpd server ("m pause" would pause the player). Then you have "m", invoked without any arguments, acts as a sink for protocol commands. ("echo pause | m" would also pause the player). The rest of the words are either meant as pipe transformers or shortcuts for complicated ones.

For instance, say you want to add all the songs in your music library that contain the words 'sabbath' and 'reality', you'd type

m listall | m get file | m filter sabbath reality | m add | m

Now, imagine that you find that it's useful to be able to quickly add things to your playlist in that manner, you can easily define it as a new word by adding the line

 "qadd")  shift; $0 listall | $0 get file | $0 filter $* | $0 add | $0 ;;

to your script and hey presto; you are now able to quickly add new stuff by issuing "m qadd foo bar". This is obviously a fairly idiosyncratic way of interacting with mpd, so it might take some experimenting to get used to.

Offline

#580 2009-10-05 16:11:20

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: Post your handy self made command line utilities

Hello.
I maked simple script to backup current files:

#!/bin/bash
#This script will backup current file.
echo "Path to file to backup:"
read file
echo "Backup dir:"
read backup
cp -a $file $backup

Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#581 2009-10-05 16:48:31

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

Re: Post your handy self made command line utilities

lolilolicon wrote:

@brisbin,
Look, I think you should separate artist and album with something more unique ... The advantage is that if you want to search other sites which treat artist and album separately(e.g. last.fm uses the format of .../artist/album).

Yeah, i doubt i'll be using any other search sites anytime soon, so leaving it w/o the separator just makes things simpler IMHO.

lolilolicon wrote:

As for a wider format support, you can try audiotag, it's a simple perl script and it's in AUR.

I just tried to install this but it needs atomicparsley which fails to build for me... haven't looked into it further though.

lolilolicon wrote:

Another thought: Why not just use mpd.db to produce the list?

i actually didn't know that the mpd.db was a plain text file that i could parse like that.  certainly makes things easier... if one were using mpd.  as it is now, my script is media player agnostic and i think i might want to keep it like that.

thanks for the comments!

Offline

#582 2009-10-05 17:33:29

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

brisbin33 wrote:

I just tried to install this but it needs atomicparsley which fails to build for me... haven't looked into it further though.

atomicparsley is not necessary. Just download from this mirror, extract it and put the perl script in $PATH. That's all. smile

brisbin33 wrote:

i actually didn't know that the mpd.db was a plain text file that i could parse like that.  certainly makes things easier... if one were using mpd.  as it is now, my script is media player agnostic and i think i might want to keep it like that.

Yeah, that's why I said it's an 'alternative' way. Actually, if you think about it, you can make your list formatting to mimic (a stripped-down) mpd.db, so non-mpd users and mpd users can use the same parsing method while mpd users gets extra speed-up.

After all, nice script brisbin.


This silver ladybug at line 28...

Offline

#583 2009-10-06 18:55:27

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

Command line calculator.  I find it a little easier to type in the terminal than doing mouse-clicks in the gui.

#!/bin/sh
# calc - command line calculator

# display usage if full argument isn't given
if [[ -z $@ ]]; then
  echo ' calc "<input>" <*decimals> - command line calculator (-h for example)'
  exit 1
fi

# decimal to be carried out to, uses default (4) unless value is given
if [[ -z $2 ]]; then
  decimals=4; else
  decimals=$2
fi

case $1 in
  -h )  echo ' calc "5*((2^3*2)/1.352)"'
        echo ' calc "p=10; n=9; p*n+10"' ;;
  *  )  echo "scale=$decimals;$1" | bc
esac

Last edited by Gen2ly (2009-10-06 18:59:28)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#584 2009-10-06 19:17:48

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Post your handy self made command line utilities

# Never mind.

Last edited by Peasantoid (2009-10-06 19:18:38)

Offline

#585 2009-10-06 19:36:23

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

Re: Post your handy self made command line utilities

lolilolicon wrote:

atomicparsley is not necessary

> audiotag -l Music/cake/comfort_eagle/*
WARNING: `AtomicParsley' not found in path, required for mp4 support!
WARNING: `AtomicParsley' not found in path, required for m4a support!

boo hiss, i have some m4a's... me's off to apply some ugly cpp patch for atomicparsley...

Offline

#586 2009-10-06 19:42:06

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

Re: Post your handy self made command line utilities

I guess I should really get to update audiotag to use a module instead of shelling out... sigh. Maybe I'll work on it in a bit smile

Offline

#587 2009-10-06 21:56:24

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: Post your handy self made command line utilities

@brisbin33 -- hey, is the fix for filenames with spaces still in the works? I'd love to give this a whirl, but it totally choked on my collection.

I also unsuccessfully tried to get atomicparsely working awhile ago when I was trying another tagger. No luck sad

Thanks for your work!

Scott

Offline

#588 2009-10-07 15:44:51

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Post your handy self made command line utilities

For the bored who watch American TV, this script tells you what should be available for download today (so what aired yesterday) using eztv.it database (you can also run it with arguments like today or specific days)

#! /bin/bash
day=$(LANG=en_US.utf8 date +%A -d "${1:-yesterday}")
echo $day
echo '--------'
curl -s 'http://eztv.it/showlist/' | grep -F -B1 "$day" | grep -F '/shows/' | sed 's/^[[:blank:]]*//;s/<[^>]*>//g'

Offline

#589 2009-10-08 05:16:16

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: Post your handy self made command line utilities

Themaister wrote:

Dummypkg: Let pacman know if a program has been installed manually from source: (kernel and nvidia driver (libgl) in my case tongue)

This is a nice package. I use it for TeX Live (since I would rather use the TeX Live net installer).

However, Dummypkg lacks a solution for pure release number changes (i.e. release 1 to 2 without an upstream upgrade). I can tell from the source that it is set to 1 by default.

Is it possible to set this variable other than editing the source of Dummypkg?

Thanks,
Rasmus


Arch x64 on Thinkpad X200s/W530

Offline

#590 2009-10-08 10:56:00

csat
Member
From: Rio de Janeiro, Brazil
Registered: 2008-08-06
Posts: 93

Re: Post your handy self made command line utilities

Hi

I am using this script to access my Gmail accounts and give me an audio information about how many messages are available.  It was copied from Ubuntu forum as written at the script header.  I've tried to implement it in order to access archlinux.us mail but not succeeded so far.  I was told to try curl instead of wget but considering the lack of knowledge on script program I am puzzled on this issue too.

#!/bin/bash
# include it into user crontab by crontab -e                      #
# make sure your /etc/rc.conf has crond at daemon array #
# */5 * * * * /home/user/.scripts/gmailLED                    #
# ------------------------------------------------------------ #
#                                                                                  #
# credits:                                                                      #
# http://ubuntuforums.org/showthread.php?t=812656    #
#                                                                                 #
# ----------------------------------------------------- ------#
# depends:  espeak                                                     #
# ----------------------------------------------------- -----#
#                                                                               #
# account1:password1 = gmail account:password         #
#                                                       #
# ----------------------------------------------------- #
echo "Checking for new email";
mailsa="$(wget --secure-protocol=TLSv1 --timeout=3 -t 1 -v -O - \
https://account1:password1@mail.google.com/mail/feed/atom \
--no-check-certificate | grep 'fullcount' \
| sed -e 's/.*<fullcount>//;s/<\/fullcount>.*//' 2>/dev/null)"

echo $mailsa;

mailsb="$(wget --secure-protocol=TLSv1 --timeout=3 -t 1 -v -O - \
https://account2:password2@mail.google.com/mail/feed/atom \
--no-check-certificate | grep 'fullcount' \
| sed -e 's/.*<fullcount>//;s/<\/fullcount>.*//' 2>/dev/null)"

echo $mailsb;

#speak announcements

if [ "$mailsa" -gt "0" ];
then
    espeak "$mailsa new emails in account one";
fi
if [ "$mailsb" -gt "0" ];
then
    espeak "$mailsb new emails in account2";
fi
sleep 4;
exit

I've tried adding this before the exit command in order to get archlinux.us mail information but it doesn't work.

mailsc="$(wget --secure-protocol=TLSv1 --timeout=3 -t 1 -v -O - \
https://archaccount:archpassword@mail.google.com/a/archlinux.us/mail/feed/atom \
--no-check-certificate | grep 'fullcount' \
| sed -e 's/.*<fullcount>//;s/<\/fullcount>.*//' 2>/dev/null)"

Thanks


CSAT
Arch User

Offline

#591 2009-10-08 15:20:19

scragar
Member
Registered: 2009-07-14
Posts: 108

Re: Post your handy self made command line utilities

No [code ] tags Csat?

Last edited by scragar (2009-10-08 15:20:53)

Offline

#592 2009-10-13 12:09:35

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Post your handy self made command line utilities

Nothing big, but handy:

#!/bin/bash
if [[ -z $2 ]]; then
    ossmix vmix0-outvol $1
else
    ossmix $(ossmix|grep $1|cut -d\  -f1) $2
fi

$vol mpd 25


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#593 2009-10-13 14:31:31

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

Re: Post your handy self made command line utilities

This snippet will bring you to the top level of your current git repo, no matter where you are inside it

alias cdg='cd $(git rev-parse --git-dir)/..'

master!arch-games *> cd packages/tremulous-updated/src/
master!arch-games/packages/tremulous-updated/src *> cdg
master!arch-games *> cd scripts/
master!arch-games/scripts *> cdg
master!arch-games *> cd /
! *> cdg
fatal: Not a git repository (or any of the parent directories): .git

Offline

#594 2009-10-15 19:27:48

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

hey guys here is a first big script of mine that I have been developing for some time.
Its not written from scratch, I've edited & added some code from other scripts.
It consists mostly of brisbin33's burn script & disk blanking from Gen2ly's burniso script.
Rest is added by me.

Since it's my first script It's not as much efficient & might be some functions are available other way.

please comment

#!/bin/zsh
#
# a commom utility for disk related tasks
# 
# depends: cdrdao, dvd+rw-tools, cdrecord, cdparanoia, vcdimager
#
#####

# colors {{{
grey="$(echo -n '\e[1;30m')"
red="$(echo -n '\e[1;31m')"
green="$(echo -n '\e[1;32m')"
yellow="$(echo -n '\e[1;33m')"
blue="$(echo -n '\e[1;34m')"
magenta="$(echo -n '\e[1;35m')"
cyan="$(echo -n '\e[1;36m')"
white="$(echo -n '\e[1;37m')"
lowhite="$(echo -n '\e[0;37m')"
# }}}

WD="/tmp/burning"
DEV="/dev/sr0"

# Functions

message() {
  echo "${blue} .:: ${lowhite}disk [${white}argument${lowhite}] ${blue}::.${lowhite}"
  echo
  echo "${white} h ${lowhite} ==> Help"
  echo "${white} b ${lowhite} ==> Burn [directories/iso]"
  echo "${white} s ${lowhite} ==> Copy Disk to System"
  echo "${white} c ${lowhite} ==> Copy Disk to Another Disk"
  echo "${white} f ${lowhite} ==> Format Disk"
  echo "${white} ra${lowhite} ==> Rip Audio CD"
  echo "${white} rv${lowhite} ==> Rip Video CD"
  echo
  if [ -e $WD ] ; then 
    rm -rf $WD
  fi
  exit 1
}

make_clean() {
  rm -rf $WD; mkdir -p $WD
}

rel2abs() {
  local file="$(basename "$1")"
  local dir="$(dirname "$1")"
  pushd "${dir:-./}" &>/dev/null && local dir="$PWD"
  echo "$dir/$file"
  popd &>/dev/null
}

make_folder() {
  mkdir $WD/to_burn

  for file in "$@"; do
    target="$(rel2abs "$file")"
    if [ -e "$target" ]; then
      ln -s "$target" $WD/to_burn/
    else
      echo "${red} ===> $target${white} not found, skipping...${lowhite}"
    fi
  done
}

save_iso() {
  if [ -e "$1" ] ; then
    echo "${red} ==>  File exists${lowhite}"
  else
    dd if=$DEV of=$1.iso && echo "${green} ===> ${lowhite}Copying Done\n File Saved as ${white}$1.iso${lowhite}"
  fi
}

rip_iso() {
  dd if=$DEV of=$WD/disc_image.iso && sleep 5
}

rip_adisk() {
  if [[ -n "`hal-device | grep "volume.disc.has_audio = true"`" ]]; then
    echo -n "${cyan} ===> ${lowhite}Please provide directory name for ripped tracks [audio_disc]: "
    read a
    [ -z "$a" ] && a="audio_disc"
    mkdir "$a" || exit 1
    cd "$a"
    cdparanoia -B
  else
    echo "${red} ===> ${lowhite}Is there a ${white}CD${lowhite} in the drive???"
    exit 1
  fi
}

rip_vdisk() {
  if [[ -n "`hal-device | grep "volume.disc.is_vcd = true"`" ]]; then
    echo -n "${cyan} ===> ${lowhite}Please provide directory name for ripped tracks [video_disc]: "
    read a
    [ -z "$a" ] && a="video_disc"
    mkdir "$a" || exit 1
    cd "$a"
    vcdxrip -v -p --nofiles -C
  else
    echo "${red} ===> ${lowhite}Is there a ${white}CD${lowhite} in the drive???"
    exit 1
  fi
}

make_iso() {
  [ -e $1 ] || message
  genisoimage -o $WD/disc_image.iso -input-charset utf-8 -f -r -R -J -l -allow-leading-dots -allow-limited-size -udf $1
}

blank_disk() {
if [[ -n "`hal-device | grep volume_label_`" ]]; then
  if [[ -n "`hal-device | grep "volume.disc.type = 'dvd_plus_rw'"`" ]] || \
  [[ -n "`hal-device | grep "volume.disc.type = 'dvdrw'"`" ]]; then
  echo -n "${red} ===> ${lowhite}DVD is not empty, ${red}blank it?${lowhite} (y/n): "
  read dvd
    if [[ $dvd = [yY] ]]; then
      dvd+rw-format -f $DEV; else
      echo "${grey} exiting ${lowhite}"
      exit
    fi
  fi
  if [[ -n "`hal-device | grep "volume.disc.type = 'cd_rw'"`" ]]; then
    echo -n "${red} ===> ${lowhite}CD is not empty, ${red}blank it?${lowhite} (y/n): "
    read cd
    if [[ $cd = [yY] ]]; then
      cdrecord -v dev=$DEV blank=fast; else
      echo "${grey} exiting ${lowhite}"
      exit
    fi
  fi
else
  echo "${grey} ===> ${white}CD/DVD is blank${lowhite}, continue."
fi
}

burn_iso() {
  [ -e $1 ] || message
  false; while [ $? -ne 0 ]; do
    cdrecord -v -dao dev=$DEV driveropts=burnfree $1
  done
  echo "${green} ===> ${white}Done${lowhite}"
}

eject_disc() {
  if [ $? -eq 0 ]; then
    echo -n "${yellow} ===> ${white}Eject disk ${lowhite}[(yes)/no]: "
    read a
    [ "$a" = "y" -o -z "$a" ] && /usr/bin/eject
  else
    echo "${red} ===> Unable To Eject Disk Drive ${lowhite}"
  fi
}

# 

#----------------------------#

# Main func. start #
make_clean
case $1 in
  b )        shift
             case $1 in
               *.iso )  burn_iso "$1"
                        eject_disc
                        ;;
               * )      if [ $# -gt 0 ]; then
                          make_folder "$@" && make_iso $WD/to_burn && burn_iso $WD/disc_image.iso
                          eject_disc
                        else
                          message
                        fi
                        ;;
             esac
             ;;
  s )        echo -n "${cyan} ===> ${lowhite}Please provide filename [${white}disc_image.iso${lowhite}]: "
             read name
             [ -z "$name" ] && name="disc_image"
             save_iso $name
             eject_disc
             ;;
  c )        rip_iso
             [ $? -eq 0 ] && echo -n "Please insert a blank disc, Press Enter to Continue: " && /usr/bin/eject && read A
             [ -z "$A" ] && /usr/bin/eject -t && burn_iso $WD/disc_image.iso
             eject_disc
             ;;
  f )        blank_disk
             ;;
  ra)        rip_adisk
             eject_disc
             ;;
  rv)        rip_vdisk
             eject_disc
             ;;
  h | * )    message
             ;;
esac

[ $? -eq 0 ] && rm -rf $WD

exit 0

#------------------------------#

"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#595 2009-10-15 23:29:30

scj
Member
From: Sweden
Registered: 2007-09-23
Posts: 158

Re: Post your handy self made command line utilities

vik_k wrote:

hey guys here is a first big script of mine that I have been developing for some time.
Its not written from scratch, I've edited & added some code from other scripts.
It consists mostly of brisbin33's burn script & disk blanking from Gen2ly's burniso script.
Rest is added by me.

Since it's my first script It's not as much efficient & might be some functions are available other way.

please comment

#!/bin/zsh

# colors {{{
grey="$(echo -n '\e[1;30m')"
red="$(echo -n '\e[1;31m')"
green="$(echo -n '\e[1;32m')"
yellow="$(echo -n '\e[1;33m')"
blue="$(echo -n '\e[1;34m')"
magenta="$(echo -n '\e[1;35m')"
cyan="$(echo -n '\e[1;36m')"
white="$(echo -n '\e[1;37m')"
lowhite="$(echo -n '\e[0;37m')"
# }}}

You could use the color module instead.

autoload colors; colors;
echo $fg[red]$bg[blue]red on blue

Offline

#596 2009-10-16 04:58:53

vik_k
Member
From: Pune, India
Registered: 2009-07-12
Posts: 227
Website

Re: Post your handy self made command line utilities

You could use the color module instead.

autoload colors; colors;
echo $fg[red]$bg[blue]red on blue

yep, I'll be using this,
that's why open-source is so good coz you can improve your code much quickly...


"First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack." ~ George Carrette

Offline

#597 2009-10-16 16:24:01

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

Re: Post your handy self made command line utilities

something just for fun.  the mpd database is surprisingly parsable... need to think of some other nifty stats to calculate...

┌─[ 12:20 ][ blue:~ ]
└─> cat .bin/mpdstats
#!/bin/bash
#
# pbrisbin 2009
#
###

database="$HOME/.mpd/mpd.db"

LC_ALL="C"

number_of() {
  local N=$(grep "$1" "$database" | sort | uniq | wc -l)
  echo $N
}

most_from() {
  local most=$(grep "$1" "$database" | sort | uniq -c | sort -r | head -1 | cut -d ':' -f 2-)
  echo $most
}

echo
echo "tracks  : $(number_of '^Title: ')"
echo "artists : $(number_of '^Artist: ')"
echo "albums  : $(number_of '^Album: ')"
echo
echo "most from artist : $(most_from '^Artist: ')"
echo "most from year   : $(most_from '^Date: ')"
echo "most from genre  : $(most_from '^Genre: ')"
echo
┌─[ 12:22 ][ blue:~ ]
└─> mpdstats

tracks  : 4306
artists : 164
albums  : 414

most from artist : Jimi Hendrix
most from year   : 2000
most from genre  : Rock

Offline

#598 2009-10-16 20:20:57

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

Re: Post your handy self made command line utilities

Yes, the MPD database format is great.

Offline

#599 2009-10-17 13:15:53

Octoploid
Member
From: Berlin, Germany
Registered: 2009-10-13
Posts: 64

Re: Post your handy self made command line utilities

Two little scripts:

1. shellpodder_mpd : A simple podcast client for mpd

#!/bin/sh
#Put your favourite podcast links into pod.conf (one per line)
rm -f playlist.pod
while read podcast
    do
    file=$(wget -q $podcast -O - | tr '\r' '\n' | tr \' \" | sed -n 's/.*url="\([^"]*\)".*/\1/p')
    for url in $file
        do
        echo $url >> playlist.pod
        done
    done < pod.conf
mpc crop
cat playlist.pod | awk '!x[$0]++'  | mpc add
mpc play

2.  conv_to_ogg_sansa_clip: Convert music from my flac collection
to ogg and apply crossfeed (with bs2bstream) for my portable audio player.

#!/bin/zsh

for f in **/*.flac 
do 
    outf=${f%.flac}.ogg
    eval "$(
               metaflac "$f" --show-tag=ARTIST \
                   --show-tag=TITLE \
                   --show-tag=ALBUM \
                   --show-tag=GENRE \
                   --show-tag=TRACKNUMBER \
                   --show-tag=DATE | sed 's/=\(.*\)/="\1"/'
    )"

    ARTIST=${ARTIST:=$artist}
    ARTIST=${ARTIST:=$Artist}
    TITLE=${TITLE:=$title}
    TITLE=${TITLE:=$Title}
    ALBUM=${ALBUM:=$album}
    ALBUM=${ALBUM:=$Album}
    GENRE=${GENRE:=$genre}
    GENRE=${GENRE:=$Genre}
    TRACKNUMBER=${TRACKNUMBER:=$tracknumber}
    TRACKNUMBER=${TRACKNUMBER:=$Tracknumber}
    DATE=${DATE:=$date}
    DATE=${DATE:=$Date}

    flac -s -d -c $f | bs2bstream -l 30 700| \
    oggenc -q 3 -r - -o /mnt/MUSIC/${PWD##*/}/$outf -a $ARTIST -t $TITLE -l $ALBUM -G "${GENRE:-non}" -N $TRACKNUMBER -d ${DATE:-2009}

#    echo $ARTIST $TITLE $ALBUM $GENRE $TRACKNUMBER $DATE

    unset ARTIST Artist artist TITLE Title title ALBUM Album album GENRE Genre genre TRACKNUMBER Tracknumber tracknumber DATE date Date
done

Offline

#600 2009-10-17 17:01:16

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

seek

Uses find for quick searches.

seek top         # find . -iname "*top*"
seek d top /etc  # find /etc -type d -iname "*top*"
seek f top       # find . -type f -iname "*top*"

Think I already saw something like this on this thread but I was too lazy to find it.

#!/bin/bash
# seek - search for name of files/folder(s) recursively in a directory

# display usage if full argument isn't given
if [[ -z "$@" ]]; then
  echo " ${0##*/} <*d,f> <pattern> <*location>"
  echo "   Search directory for file of folder match (*optional)"
  echo "   Example: 'seek d top /home/user'.  Output: '/home/user/Desktop...'"
  exit
fi

case $1 in
  d ) if [ -z "$3" ]; then
        find .    -type d -iname "*$2*"
        else
        find "$3" -type d -iname "*$2*"
      fi
      ;;
  f ) if [ -z "$3" ]; then
        find .    -type f -iname "*$2*"
        else
        find "$3" -type f -iname "*$2*"
      fi
      ;;
  * ) if [ -z "$2" ]; then
        find .    -iname "*$1*"
        else
        find "$2" -iname "*$1*"
      fi
      ;;
esac

Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

Board footer

Powered by FluxBB