You are not logged in.

#651 2009-11-14 18:08:10

mjunx
Member
From: Chicago
Registered: 2009-11-01
Posts: 17
Website

Re: Post your handy self made command line utilities

Well, for optimising the SQLite databases in Firefox (or any program based on Mozilla's code that uses SQLite):

find ~/.mozilla -name '*.sqlite' -exec sqlite3 '{}' 'VACUUM;' \;

Also, since some Makefiles are generated badly if you have a space in your CC or CXX variables (e.g., if you use distcc or ccache), so I made a couple one-liner scripts for that (this one called ccache-gcc):

exec ccache gcc "$@"

You can make a g++ version of course by substituting g++ for gcc.

Then there's my backup file functions (for .bashrc or anything sourced by it):

_backup_file()
{
    for f; do
        i=0
        while [ -e "$f.$i" ]; do
           (( i++ ))
        done

        FLAGS="-fp" # force; preserve mode, ownership, timestamps
        if [ -d "$f" ]; then
            FLAGS="${FLAGS}r" # recursive
        fi
        ${CP:-cp} $FLAGS "$f" "$f.$i"
        echo "Copied to $f.$i"
    done
}

backup_file()
{
    export CP="cp"
    _backup_file "$@"
}

s_backup_file()
{
    export CP="sudo cp"
    _backup_file "$@"
}

If you want to share some files over the web real quick, there's a nice python one-liner:

alias webshare='python /usr/lib/python2.6/SimpleHTTPServer.py 8001'

Or, alternatively (but doesn't allow you to specify the port):

#!/usr/bin/env python
import SimpleHTTPServer
SimpleHTTPServer.test()

Oh, and some of my most-used aliases:

export EDITOR=vim PAGER=vimpager
alias se='sudo -e'     # sudo edit; make sure $EDITOR is vim or something!
alias v='vimpager'

alias ..='cd ..'        # go back one directory
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'

alias ls='ls --color=auto'
alias sl='ls'           # typos
alias ll='ls -l'        # long listing
alias la='ls -A'        # all
alias lr='ls -lR'       # recursive ls
alias lsd='ls -d */'    # see xd
alias xd='ls -dl */'    # list directories

alias grep='grep --color=auto'
alias rgrep='grep -r'
alias ergrep='grep -E -r'

alias rwrr='chmod 0644' # as in rw-r--r--
alias rmrf='rm -rf' # less typing

# since I used to have APT-related aliases, I made some pacman ones, too
alias pacman='sudo pacman' # especially useful since it's NOPASSWD
alias pac-update='yaourt -Sy'
alias pac-upgrade='yaourt -Syu'
alias pac-dist-upgrade='yaourt -Syu --aur --devel'
alias pac-search='yaourt -Ss'

Offline

#652 2009-11-17 10:18:43

Purch
Member
From: Finland
Registered: 2006-02-23
Posts: 229

Re: Post your handy self made command line utilities

Print ownerships and permissions of the parent paths step by step

#!/bin/bash
# list directory permissions along the path

if [ -n "$1" ];then
   dirpath=$1
else
   dirpath=$(pwd)
fi

IFS='/'

for part in $dirpath;do
   unset IFS
   grow="$grow""$part""/"
   echo $(ls -asld --color --time-style=long-iso $grow | cut -d ' ' -f 2,4,5,9)
done

exit 0

Offline

#653 2009-11-17 13:39:44

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Post your handy self made command line utilities

Purch wrote:

Print ownerships and permissions of the parent paths step by step

I wrote something like this a while back and recently extended it a bit.  Check out http://rsontech.net/projects/shop

Edit: Holy crap, I posted an old (python) version of shop to this thread when I first first wrote it.  It has long since become a bash script though.

Edit2: Ah Ha! You posted yours before too!

Last edited by rson451 (2009-11-17 15:45:36)


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#654 2009-11-18 14:04:04

Purch
Member
From: Finland
Registered: 2006-02-23
Posts: 229

Re: Post your handy self made command line utilities

oh nou!

We should have some sort of database to search from smile

Edit: hahaa see the echo added there to remove empty lines wink

Last edited by Purch (2009-11-18 14:07:27)

Offline

#655 2009-11-18 14:26:11

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

Re: Post your handy self made command line utilities

Something like shell-fu would be best here... Sounds like a great project for someone to make!

It should have sorting/listing and searching based on tags, script name, and a brief description. It should have language-aware syntax highlighting, which would hopefully try to guess (asking the user to confirm language). It should have a discussion area for someone to submit comments and suggestions for the script. The script should be editable by the user who submitted it, with a versioned history (I'm thinking like the mediawiki page history listing). User comments should be tagged somehow with the version they commented against, so that it's clear what they are referring to. It should also have a spot for the author to provide notes or a longer description.

So, who's going to make it? tongue

Offline

#656 2009-11-18 16:46:50

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

Re: Post your handy self made command line utilities

Here is an mpd script for playing a single song and then returning to the previous where you left off.

The arguments are a combined "any" search (using nc localhost 6600, because mpc search is messed up). It works in a tty with a select menu and a non-tty by replacing "dmenuwrap".

It returns to the previous song when you change tracks.

#! /bin/bash
[ $# = 0 ] && exit
mpdstate=~/.mpd/mpdstate
currentsong=$(mpc | sed -n '2s/.*#\([0-9]*\).*/\1/p')
currentseek=$(mpc | sed -n '2s/.*(\([0-9]*%\))/\1/p')
search=$({ echo search any $(echo $* | sed 's/ / any /g'); echo close; } | nc localhost 6600 | grep ^file | cut -d' ' -f2-)
lines=$(echo "$search" | wc -l)
[ $lines = 0 ] && exit
if [ $lines -gt 1 ]; then
    if tty > /dev/null; then
    IFS="
"
    select line in $search; do search=$line; break; done
    [ -z "$REPLY" ] && exit
    else
    search=$(echo "$search" | dmenuwrap)
    [ -z "$search" ] && exit
    fi
fi
mpc play $(( $(grep -F "$search" $mpdstate | cut -d':' -f1) + 1 )) > /dev/null # mpdstate is 0-based 
while mpc idle > /dev/null; do
mpc --format '%file%' | grep -q -F "$search" || break
done
mpc play $currentsong > /dev/null
mpc seek $currentseek > /dev/null

Offline

#657 2009-11-18 20:34:11

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: Post your handy self made command line utilities

Convenient way of building stuff with ABS tongue

#!/bin/bash

FOUND=0

for repo in core extra community testing
do

    if [[ -f /var/abs/$repo/"$1"/PKGBUILD ]]; then
        cp -r -v /var/abs/$repo/"$1" /tmp/ && cd /tmp/"$1"
        FOUND=1
        break
    fi
done

if [[ $FOUND == 0 ]]; then
    echo "Couldn't find PKGBUILD"
else
    echo "Editing PKGBUILD"
    $EDITOR /tmp/"$1"/PKGBUILD
fi

Last edited by Themaister (2009-11-18 20:34:37)

Offline

#658 2009-11-18 20:48:24

ataraxia
Member
From: Pittsburgh
Registered: 2007-05-06
Posts: 1,553

Re: Post your handy self made command line utilities

Themaister wrote:

Convenient way of building stuff with ABS tongue

You should add community-testing. It's downloaded by abs, even though it's not listed in abs.conf.

Offline

#659 2009-11-18 22:04:54

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

Re: Post your handy self made command line utilities

I use tput quite a bit to color my bash scripts, in case I need to remember a reference I wrote this script that displays the tput colors and their definitions:

#!/bin/bash
# tputcolors

# Text color variables
txtund=$(tput sgr 0 1)          # Underline
txtbld=$(tput bold)             # Bold
bldred=${txtbld}$(tput setaf 1) #  red
bldblu=${txtbld}$(tput setaf 4) #  blue
bldwht=${txtbld}$(tput setaf 7) #  white
txtrst=$(tput sgr0)             # Reset
info=${bldwht}*${txtrst}        # Feedback
pass=${bldblu}*${txtrst}
warn=${bldred}!${txtrst}

echo
echo -e "$(tput bold) reg  bld  und   tput-command$(tput sgr0)"

for i in $(seq 1 7); do
  echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0)  \$(tput setaf $i)"
done

echo ' Bold            $(tput bold)'
echo ' Underline       $(tput sgr 0 1)'
echo ' Reset           $(tput sgr0)'
echo

tputcolors.png


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

Offline

#660 2009-11-18 23:46:14

antes
Member
From: Argentina - Ciudad de La Plata
Registered: 2009-06-10
Posts: 4

Re: Post your handy self made command line utilities

Great collection! Here's a simple script i've been working on. It attemps to make a fluxbox applications menu.

#!/bin/bash

# 2009/11/18
menu_write_head() {
    echo -e "[begin] ($2)" >> $1
    echo -e "[submenu] ($3)" >> $1
}
menu_write_tail() {
    echo -e "[end]\n\t[workspaces] (Workspaces)" >> $1
    echo -e "\t[exec] (FBRun) {fbrun}" >> $1
    echo -e "\t[exec] (XTERM) {xterm}\n\t[separator]" >> $1
    echo -e "[submenu] (Styles 1) {Choose a style...}" >> $1
    echo -e "\t[stylesdir] (/usr/share/fluxbox/styles)\n[end]" >> $1
    echo -e "[submenu] (Styles 2) {Choose a style..}" >> $1
    echo -e "\t[stylesdir] (~/.fluxbox/styles)\n[end]" >> $1
    echo -e "\t[exec] (About) {(fluxbox -v; fluxbox -info | sed 1d) | xmessage -file - -center}" >> $1
    echo -e "\t[reconfig] (Reconfigure FB)" >> $1
    echo -e "\t[restart] (Restart FB)" >> $1
    echo -e "\t[exit] (Exit FB)\n[end]" >> $1    
}
menu_add_entry() {
    echo -e "\t[exec] ($2) {$3}" >> "$1"
}

if [ $# -ge 1 ]; then
    if [ -f "$1" ]; then
        echo "File $1 already exists, backing up to: $1.~"
        mv "$1" "$1.~"
    fi
    menu_write_head "$1" "Fluxbox_Menu" "USERMENU"
    archivos=$(ls -1 /usr/share/applications 2> /dev/null | grep ".desktop" 2> /dev/null )
    for i in $archivos; do
        app_command=$(cat "/usr/share/applications/$i" | grep -m 1 "Exec=" | cut -d '=' -f2 | cut -d '%' -f1)
        app_name=$(cat "/usr/share/applications/$i" | grep -m 1 "Name=" | cut -d '=' -f2)    
        menu_add_entry "$1" "$app_name" "$app_command"
    done
    menu_write_tail "$1"
else
    echo -e "Error: Missing argument. Expected $0 file_name\n\t(usually $HOME/.fluxbox/menu or $HOME/.fluxbox/usermenu)"
    exit 1
fi

Offline

#661 2009-11-18 23:58:28

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

Re: Post your handy self made command line utilities

Gen2ly wrote:

I use tput quite a bit to color my bash scripts, in case I need to remember a reference I wrote this script that displays the tput colors and their definitions:

That looks useful. Nice One.

Offline

#662 2009-11-19 00:21:09

owain
Member
Registered: 2009-08-24
Posts: 251

Re: Post your handy self made command line utilities

This started as playing around with command-line access to BBC Radio streams, and I ended up adding an Openbox pipe menu as well.

#!/bin/bash

#####################################################################
#                                                                   #
#  startradio                                                       #
#                                                                   #
#  Quick access to BBC Radio streams, using mplayer                 #
#                                                                   #
#  List urls in ~/.radiourls, in the format                         #
#  URL,name,cli-command                                             #
#                                                                   #
#  eg:                                                              #
#  http://www.bbc.co.uk/radio/listen/live/r1.asx,Radio 1,1          #
#                                                                   #
#  Many URLs can be found at http://www.bbcstreams.com              #
#  Geographic restrictions by IP may affect some streams            #
#                                                                   #
#####################################################################



function startstream () {
    stopstreams
    
    echo -e "Starting new stream..."
    mplayer -playlist ${URLS[$1]} &> /dev/null &
        # Dumping to /dev/null because even the -really-quiet option wasn't really quiet enough
    echo ${NAMES[$1]} > $STATUS        
}


function stopstreams () {
    echo "Stopping all current streams..."
    
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        while [ $"`ps ux | grep mplayer | grep ${URLS[$i]} | grep -v grep | wc -l`" -ne "0" ]; do
            kill `ps ux | grep mplayer | grep ${URLS[$i]} | grep -v grep | head -n 1 | cut -c 10- | cut -c -5`
            sleep 1
        done
    done
    
    echo $NOTPLAYINGSTATUS > $STATUS
}


function liststations () {
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        echo -e "\t"${COMMANDS[$i]}"\t\t"${NAMES[$i]}
    done
    echo
}



###############################################################################

URLSOURCE="$HOME/.radiourls"
STATUS="$HOME/.radiostatus"
NOTPLAYINGSTATUS="Not playing"


### Get URLs, station names, and command-line arguments

URLCOUNT="`cat $URLSOURCE 2>/dev/null  | grep -v '#' | wc -l`"

if [ $URLCOUNT == "0" ]; then
    echo "No URL data found in "$URLSOURCE
    exit 0
else
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        URLS[$i]="`cat $URLSOURCE |  head -n $i | tail -n 1 | cut -d ',' -f 1`"
        NAMES[$i]="`cat $URLSOURCE  |  head -n $i | tail -n 1 | cut -d ',' -f 2`"
        COMMANDS[$i]="`cat $URLSOURCE |  head -n $i | tail -n 1 | cut -d ',' -f 3`"
    done
fi



### 'startradio stop'

if [ "$#" == "1" -a "$1" == "stop" ]; then
    
    stopstreams



### 'startradio list'

elif [ "$#" == "1" -a "$1" == "list" ]; then

    echo -e "\n'startradio start COMMAND', where\n"
    echo -e "\tCommand\t\tStation\n"
    liststations


### 'startradio playing'
    
elif [ "$#" == "1" -a "$1" == "playing" ]; then

    ISPLAYING="0"
    
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        if [ $"`ps ux | grep mplayer | grep  ${URLS[$i]} | grep -v grep | wc -l`" -ne "0" ]; then
            echo -e "\nNow playing: ${NAMES[$i]}\n"
            let    ISPLAYING="1"
        fi
    done
    
    if [ $ISPLAYING == 0 ]; then
        echo -e "\nNo streams playing\n"
    fi


### 'startradio pipemenu'
    
elif [ "$#" == "1" -a "$1" == "pipemenu" ]; then

    echo '<openbox_pipe_menu>'

    if [ "`cat $STATUS`" != "$NOTPLAYINGSTATUS" ]; then
        echo '<separator label="Now playing: '`cat $STATUS`'" />'
    fi

    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        if [ "${NAMES[$i]}" == "$STATUS" ]; then
            echo '<item label="'${NAMES[$i]}'">'
            echo '<action name="Execute">'
            echo '<execute></execute>'
            echo '</action>'
            echo '</item>'
        else
            echo '<item label="'${NAMES[$i]}'">'
            echo '<action name="Execute">'
            echo '<execute>startradio start '${COMMANDS[$i]}'</execute>'
            echo '</action>'
            echo '</item>'
        fi
    done
    
    if [ "$STATUS" != "$NOTPLAYINGSTATUS" ]; then
        echo '<separator/>'
        echo '<item label="Stop radio">'
        echo '<action name="Execute">'
        echo '<execute>startradio stop</execute>'
        echo '</action>'
        echo '</item>'
    fi


    echo '</openbox_pipe_menu>'



### 'startradio start COMMAND'

elif [ "$#" == "2" -a "$1" == "start" ]; then

    STREAMSTARTED="0"
    
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
            if [ ${COMMANDS[$i]} == $2 ]; then
                echo -e "Starting "${NAMES[$i]}""
                #startstream ${URLS[$i]},${NAMES[$i]}
                startstream $i
                let STREAMSTARTED="1"
            fi
    done
    
    if [ $STREAMSTARTED == 0 ]; then
        echo -e "\nError: station choice not recognised\n"
        liststations
    fi


### No valid command(s) found

else 

    echo "Usage:
    startradio start OPTION
    startradio stop
    startradio playing
    startradio list
    startradio pipemenu

Play BBC radio streams using mplayer
"    
    liststations
fi

(Minor edit, to use $HOME to identify user's files)

Here's my ~/.radiourls (the World Service one is different to that on the page referenced in the comments of the script, which didn't work when I tried it):

http://www.bbc.co.uk/radio/listen/live/r1.asx,Radio 1,1
http://www.bbc.co.uk/radio/listen/live/r1x.asx,Radio 1Xtra,1x
http://www.bbc.co.uk/radio/listen/live/r2.asx,Radio 2,2
http://www.bbc.co.uk/radio/listen/live/r3.asx,Radio 3,3
http://www.bbc.co.uk/radio/listen/live/r4.asx,Radio 4,4
http://www.bbc.co.uk/radio/listen/live/r4lw.asx,Radio 4 LW,4lw
http://www.bbc.co.uk/radio/listen/live/r5l.asx,Radio 5 Live,5
http://www.bbc.co.uk/radio/listen/live/r5lsp.asx,Radio 5 Live Sports Extra,5se
http://www.bbc.co.uk/radio/listen/live/r6.asx,Radio 6,6
http://www.bbc.co.uk/radio/listen/live/r7.asx,Radio 7,7
http://www.bbc.co.uk/worldservice/meta/tx/nb/live/www15.ram,World Service,ws
http://www.bbc.co.uk/radio/listen/live/bbcmanchester.asx,Radio Manchester,manc
http://www.bbc.co.uk/radio/listen/live/bbcsuffolk.asx,Radio Suffolk,suff

Last edited by owain (2009-11-19 10:10:46)

Offline

#663 2009-11-19 00:47:23

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

Re: Post your handy self made command line utilities

mikesd wrote:
Gen2ly wrote:

I use tput quite a bit to color my bash scripts, in case I need to remember a reference I wrote this script that displays the tput colors and their definitions:

That looks useful. Nice One.

Thanks, M.  Whenever I feel guilty I take a script and feel better... ahhhhh.

Last edited by Gen2ly (2009-11-19 00:48:14)


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

Offline

#664 2009-11-19 23:39:27

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

Re: Post your handy self made command line utilities

mikesd wrote:

...

alias pud='pwd > ~/.dir'       #push directory
alias pod='cd $( cat ~/.dir )' #pop directory

I know there has to be a better way to do this but this is nice and simple and works for me.

Nice, I've been trying to think of a way to do this - guess I'm too lazy for the pop and pushd smile.  I added to the pud command to add back slashes before spaces because sometimes I navigate my Windows partition:

alias pud="pwd | sed 's/ /\\\ /' > ~/.dir"
alias pod="cd "$(cat ~/.cdo)""

Last edited by Gen2ly (2009-11-20 20:03:54)


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

Offline

#665 2009-11-20 08:20:11

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

Re: Post your handy self made command line utilities

The bash examples at /usr/share/doc/bash/examples (if you've not got them on your machine, they're available from here http://www.bashcookbook.com/bashinfo/ ), has under the functions, in "dirfuncs" a good cd replacement that implements a dir stack. There's also some supplementary functions (dirs to list the stack, mcd gives a menu of dirs, etc.). It's all taken from the book "The Korn Shell". Worth a look.


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

#666 2009-11-20 21:25:01

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

Re: Post your handy self made command line utilities

# strip leading path components
pstrip() {
  sed -r "s/^([^/]*\/*){$1}//"
}
$ echo /foo/bar | pstrip 1
foo/bar
$ echo /foo/bar | pstrip 2
bar

Offline

#667 2009-11-20 21:30:16

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

Re: Post your handy self made command line utilities

It doesn't do all the same stuff, but if you just want a filename, use basename

Offline

#668 2009-11-20 23:01:52

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

Re: Post your handy self made command line utilities

Or using parameter expansion:

## dirname
$ file=/etc/bash_completion.d/pacman ; echo ${file%/*}
$ /etc/bash_completion.d

## basename
$ file=/etc/bash_completion.d/pacman ; echo ${file##*/}
$ pacman

Offline

#669 2009-11-21 00:44:39

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

Re: Post your handy self made command line utilities

One I've posted before, but never actually explained:

ee(){
  if [ "$#" = 0 ]; then
    echo "Error. Usage:
        ee command1 [command2 [command3..]]" >&2
    exit 1
  fi

  while (( "$#" )); do
    echo -e '\e[1;30m' $1 '\e[0m'
    $1
    shift
  done
}

Usage:

ee "echo foo" "echo bar"

Result:

echo foo
foo
echo bar
bar

I really just wrote it up when I was looking to run multiple commands without having to check on them and still see what I was running as it was working, I just look for the bold tongue

Last edited by scragar (2009-11-21 00:47:01)

Offline

#670 2009-11-21 18:32:57

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

Re: Post your handy self made command line utilities

google anything from CLI, opens results in $BROWSER.  if no term's passed, search your primary selection.

# go to google for anything
google() {
  local term="$*"

  [ -z "$term" ] && term="$(xclip -o)"

  local URL="http://www.google.com/search?q=${term// /+}"

  $BROWSER "$URL" &>/dev/null &
}

Offline

#671 2009-11-21 19:26:52

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Post your handy self made command line utilities

owain wrote:

This started as playing around with command-line access to BBC Radio streams, and I ended up adding an Openbox pipe menu as well.

#!/bin/bash

#####################################################################
#                                                                   #
#  startradio                                                       #
#                                                                   #
#  Quick access to BBC Radio streams, using mplayer                 #
#                                                                   #
#  List urls in ~/.radiourls, in the format                         #
#  URL,name,cli-command                                             #
#                                                                   #
#  eg:                                                              #
#  http://www.bbc.co.uk/radio/listen/live/r1.asx,Radio 1,1          #
#                                                                   #
#  Many URLs can be found at http://www.bbcstreams.com              #
#  Geographic restrictions by IP may affect some streams            #
#                                                                   #
#####################################################################



function startstream () {
    stopstreams
    
    echo -e "Starting new stream..."
    mplayer -playlist ${URLS[$1]} &> /dev/null &
        # Dumping to /dev/null because even the -really-quiet option wasn't really quiet enough
    echo ${NAMES[$1]} > $STATUS        
}


function stopstreams () {
    echo "Stopping all current streams..."
    
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        while [ $"`ps ux | grep mplayer | grep ${URLS[$i]} | grep -v grep | wc -l`" -ne "0" ]; do
            kill `ps ux | grep mplayer | grep ${URLS[$i]} | grep -v grep | head -n 1 | cut -c 10- | cut -c -5`
            sleep 1
        done
    done
    
    echo $NOTPLAYINGSTATUS > $STATUS
}


function liststations () {
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        echo -e "\t"${COMMANDS[$i]}"\t\t"${NAMES[$i]}
    done
    echo
}



###############################################################################

URLSOURCE="$HOME/.radiourls"
STATUS="$HOME/.radiostatus"
NOTPLAYINGSTATUS="Not playing"


### Get URLs, station names, and command-line arguments

URLCOUNT="`cat $URLSOURCE 2>/dev/null  | grep -v '#' | wc -l`"

if [ $URLCOUNT == "0" ]; then
    echo "No URL data found in "$URLSOURCE
    exit 0
else
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        URLS[$i]="`cat $URLSOURCE |  head -n $i | tail -n 1 | cut -d ',' -f 1`"
        NAMES[$i]="`cat $URLSOURCE  |  head -n $i | tail -n 1 | cut -d ',' -f 2`"
        COMMANDS[$i]="`cat $URLSOURCE |  head -n $i | tail -n 1 | cut -d ',' -f 3`"
    done
fi



### 'startradio stop'

if [ "$#" == "1" -a "$1" == "stop" ]; then
    
    stopstreams



### 'startradio list'

elif [ "$#" == "1" -a "$1" == "list" ]; then

    echo -e "\n'startradio start COMMAND', where\n"
    echo -e "\tCommand\t\tStation\n"
    liststations


### 'startradio playing'
    
elif [ "$#" == "1" -a "$1" == "playing" ]; then

    ISPLAYING="0"
    
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        if [ $"`ps ux | grep mplayer | grep  ${URLS[$i]} | grep -v grep | wc -l`" -ne "0" ]; then
            echo -e "\nNow playing: ${NAMES[$i]}\n"
            let    ISPLAYING="1"
        fi
    done
    
    if [ $ISPLAYING == 0 ]; then
        echo -e "\nNo streams playing\n"
    fi


### 'startradio pipemenu'
    
elif [ "$#" == "1" -a "$1" == "pipemenu" ]; then

    echo '<openbox_pipe_menu>'

    if [ "`cat $STATUS`" != "$NOTPLAYINGSTATUS" ]; then
        echo '<separator label="Now playing: '`cat $STATUS`'" />'
    fi

    for (( i=1; i<=$URLCOUNT; i++ ))
    do
        if [ "${NAMES[$i]}" == "$STATUS" ]; then
            echo '<item label="'${NAMES[$i]}'">'
            echo '<action name="Execute">'
            echo '<execute></execute>'
            echo '</action>'
            echo '</item>'
        else
            echo '<item label="'${NAMES[$i]}'">'
            echo '<action name="Execute">'
            echo '<execute>startradio start '${COMMANDS[$i]}'</execute>'
            echo '</action>'
            echo '</item>'
        fi
    done
    
    if [ "$STATUS" != "$NOTPLAYINGSTATUS" ]; then
        echo '<separator/>'
        echo '<item label="Stop radio">'
        echo '<action name="Execute">'
        echo '<execute>startradio stop</execute>'
        echo '</action>'
        echo '</item>'
    fi


    echo '</openbox_pipe_menu>'



### 'startradio start COMMAND'

elif [ "$#" == "2" -a "$1" == "start" ]; then

    STREAMSTARTED="0"
    
    for (( i=1; i<=$URLCOUNT; i++ ))
    do
            if [ ${COMMANDS[$i]} == $2 ]; then
                echo -e "Starting "${NAMES[$i]}""
                #startstream ${URLS[$i]},${NAMES[$i]}
                startstream $i
                let STREAMSTARTED="1"
            fi
    done
    
    if [ $STREAMSTARTED == 0 ]; then
        echo -e "\nError: station choice not recognised\n"
        liststations
    fi


### No valid command(s) found

else 

    echo "Usage:
    startradio start OPTION
    startradio stop
    startradio playing
    startradio list
    startradio pipemenu

Play BBC radio streams using mplayer
"    
    liststations
fi

(Minor edit, to use $HOME to identify user's files)

Here's my ~/.radiourls (the World Service one is different to that on the page referenced in the comments of the script, which didn't work when I tried it):

http://www.bbc.co.uk/radio/listen/live/r1.asx,Radio 1,1
http://www.bbc.co.uk/radio/listen/live/r1x.asx,Radio 1Xtra,1x
http://www.bbc.co.uk/radio/listen/live/r2.asx,Radio 2,2
http://www.bbc.co.uk/radio/listen/live/r3.asx,Radio 3,3
http://www.bbc.co.uk/radio/listen/live/r4.asx,Radio 4,4
http://www.bbc.co.uk/radio/listen/live/r4lw.asx,Radio 4 LW,4lw
http://www.bbc.co.uk/radio/listen/live/r5l.asx,Radio 5 Live,5
http://www.bbc.co.uk/radio/listen/live/r5lsp.asx,Radio 5 Live Sports Extra,5se
http://www.bbc.co.uk/radio/listen/live/r6.asx,Radio 6,6
http://www.bbc.co.uk/radio/listen/live/r7.asx,Radio 7,7
http://www.bbc.co.uk/worldservice/meta/tx/nb/live/www15.ram,World Service,ws
http://www.bbc.co.uk/radio/listen/live/bbcmanchester.asx,Radio Manchester,manc
http://www.bbc.co.uk/radio/listen/live/bbcsuffolk.asx,Radio Suffolk,suff

thats way cool!! i would like a way to add absolute radio stream to it, how do i do that?

edit, sorted it big_smile
added to the radio urls file.

http://network.absoluteradio.co.uk/core/audio/wmp/live.asx?service=vrbb,Absolute,absolute

this is simple, but works well, im gona be using cron to call it and use it as an alarm clock big_smile

Last edited by markp1989 (2009-11-22 18:28:09)


Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#672 2009-11-22 18:19:37

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Post your handy self made command line utilities

here is a simple application launcher i wrote a while ago, it was inspired by a screen manager that i found in this thread a while ago.

#!/bin/bash
menuitem=`zenity --list --title="Application Launcher" \
--width=280 --height=225 \
--text="Application Launcher" \
--column="Modename" --column="Description" --separator=";" \
Firefox           "Firefox Web Browser" \
Pcmanfm           "File manager" \
VLC               "Media Player" \
Emesene           "MSN Messenger" `
case $menuitem in 
    Firefox) firefox;;
    Pcmanfm) pcmanfm;;
    VLC) vlc;;
    Emesene) emesene;;
   esac

Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#673 2009-11-22 18:57:32

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

Re: Post your handy self made command line utilities

I CAN DO ZENITY TOO!

#!/bin/bash
ANIMEDIR="$HOME/anime/"
BROWSER='uzbl'
anime () {
    local sel=$(zenity --file-selection --filename "$ANIMEDIR")
    [ -n "$sel" ] && mplayer "$sel"
}
net () {
    local sel=$(zenity --entry --text 'Enter Url:')
    [ -n "$sel" ] && $BROWSER "$sel"
}
killme () {
 true
}
fillme () {
   true
}
scream () {
    cat /dev/sda | aplay
}
$(
zenity --list --text 'What do you want?' --radiolist --print-column 3 \
       --hide-column 3 --column '' --column 'Choices' --column 'CMD' \
       TRUE  'Anime'    "anime" \
       FALSE 'Surfin'    "net" \
       FALSE 'Suicide'   "killme" \
       FALSE 'Refresh'   "fillme" \
       FALSE 'Music'     "scream"
)

The list goes on and on and on...


EDIT: Removed  rm -rf / from script --Snowman
EDIT2: also removed cp of /dev/zero to sda  --Snowman


This silver ladybug at line 28...

Offline

#674 2009-11-22 19:34:10

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Post your handy self made command line utilities

lolilolicon wrote:

I CAN DO ZENITY TOO!

#!/bin/bash
ANIMEDIR="$HOME/anime/"
BROWSER='uzbl'
anime () {
    local sel=$(zenity --file-selection --filename "$ANIMEDIR")
    [ -n "$sel" ] && mplayer "$sel"
}
net () {
    local sel=$(zenity --entry --text 'Enter Url:')
    [ -n "$sel" ] && $BROWSER "$sel"
}
killme () {
   true
}
fillme () {
    cp /dev/zero /dev/sda
}
scream () {
    cat /dev/sda | aplay
}
$(
zenity --list --text 'What do you want?' --radiolist --print-column 3 \
       --hide-column 3 --column '' --column 'Choices' --column 'CMD' \
       TRUE  'Anime'    "anime" \
       FALSE 'Surfin'    "net" \
       FALSE 'Suicide'   "killme" \
       FALSE 'Refresh'   "fillme" \
       FALSE 'Music'     "scream"
)

The list goes on and on and on...

you may wana edit out the rm -rf / because thats destructive as shit (i know u know this , but new people dont)


Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#675 2009-11-22 19:41:46

Snowman
Developer/Forum Fellow
From: Montreal, Canada
Registered: 2004-08-20
Posts: 5,212

Re: Post your handy self made command line utilities

markp1989 wrote:

you may wana edit out the rm -rf / because thats destructive as shit (i know u know this , but new people dont)

Done

Offline

Board footer

Powered by FluxBB