You are not logged in.

#2251 2013-11-08 03:32:51

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,987
Website

Re: Post your handy self made command line utilities

I've been searching for ages for a variant of antiword or catdoc that works with docx files.  I've found a few tools that are advertised to do this, but most have several dependencies and most of those don't even work.  But then I discovered xsltproc which I already had as a dependency for something else.  I found an example xsl online to convert from document.xml files to latex.  That failed pretty miserably, but it gave me the starting point for the following: catdocx

#/bin/bash

[[ -z $1 ]] && echo -e "USAGE:\tcatdocx <file.docx>" && exit

DIR="$(mktemp -d)"
cp "$1" $DIR
cd $DIR
unzip "$1" >/dev/null 2>&1

[[ $? -ne 0 ]] && echo "Unable to unzip $1" && rm -rf $DIR && exit

cat <<"EOF" > xslt.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

<xsl:template match="w:p">
<xsl:apply-templates/><xsl:if test="position()!=last()"><xsl:text>

</xsl:text></xsl:if>
</xsl:template>

<xsl:template match="w:r">
 <xsl:if test="w:rPr/w:b"><xsl:text>\033[1m</xsl:text></xsl:if>
 <xsl:call-template name="pastb"/>
 <xsl:if test="w:rPr/w:b"><xsl:text>\033[0m</xsl:text></xsl:if>
</xsl:template>

<xsl:template name="pastb">
 <xsl:if test="w:rPr/w:i"><xsl:text></xsl:text></xsl:if>
 <xsl:call-template name="pasti"/>
 <xsl:if test="w:rPr/w:i"><xsl:text></xsl:text></xsl:if>
</xsl:template>

<xsl:template name="pasti">
 <xsl:apply-templates select="w:t"/>
</xsl:template>

</xsl:stylesheet>
EOF

echo -e "$(xsltproc xslt.xsl word/document.xml)" | sed '1d' | fold -s | less
rm -rf $DIR

Last edited by Trilby (2013-11-08 03:37:22)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#2252 2013-11-08 09:41:54

kokoko3k
Member
Registered: 2008-11-14
Posts: 1,491

Re: Post your handy self made command line utilities

Kaustic wrote:

What steve above did was more correct but you should really use printf in scripts.

I didn't even realized what steve posted was related smile

Offline

#2253 2013-11-20 13:00:41

ezzetabi
Member
Registered: 2006-08-27
Posts: 943

Re: Post your handy self made command line utilities

To workaround the networkmanager dispatcher scripts timeout (the idea in the wiki failed for me) I just start the script with `screen -d -m`.
I wanted to avoid that two scripts could be called at the same time and so I made this:

#!/bin/bash
#needs lockfile-progs package

set -e
LOCKNAME=/tmp/myscript
EXIT=1

function atexit {
    kill "$LOCKKEEPERPID"
    lockfile-remove "$LOCKNAME"

    exit "$EXIT"
}

lockfile-create -q -r 0 "$LOCKNAME"
lockfile-touch "$LOCKNAME" &
LOCKKEEPERPID="$!"
trap atexit SIGHUP SIGINT SIGTERM EXIT

echo Real work here... starting.
sleep 3s
echo Real work ended...

EXIT=0

Only one instance of this script can be executed at the same time.

Offline

#2254 2013-11-21 21:47:35

Nattgew
Member
Registered: 2013-10-09
Posts: 30

Re: Post your handy self made command line utilities

I find that sometimes I like to monitor things on the command line, and got tired of putting in a whole "while true; do..." sequence every time.
The script can take arguments for what command to run (-c), how many times to run it (-i), how long to wait in between runs (-d), and whether to print out a countdown during the delay (-q).

#!/bin/bash
inc=0
iter=1
delay=1
quiet=0
while getopts "d:c:i:q" OPTION
do
  case $OPTION in
   d)
#   echo "Found a delay of $OPTARG"
    delay="$OPTARG"
    ;;
   c)
#   echo "Found command $OPTARG"
    cmd="$OPTARG"
    ;;
   i)
#   echo "Found $OPTARG iters"
    iter="$OPTARG"
    inc=1
    ;;
   q)
#   echo "Be vewy quiet"
    quiet=1
    ;;
   *)
    echo "Received $OPTARG"
    exit 1
    ;;
  esac
done

function actions() {
  let iter-=inc
  slept=$delay
  if [ $quiet -ne 1 ]; then
   echo -e "Sleeping $delay"
   while [ $slept -gt 0 ]; do
    echo -ne "\e[0K\r$slept                       "
    let slept-=1
    sleep 1
   done
   echo -e "\e[0K\r$slept                       "
  else
   sleep $delay
  fi
# echo "Runnin $cmd"
  eval "$cmd"
}

#echo "Runnin $cmd"
eval "$cmd"
let iter-=inc

while [ $iter -gt 0 ]; do
  actions
done

I left some debugging echoes commented out, maybe to clarify what is going on. There are probably some less-than-ideal things in it, fortunately I recently read up on the "Bash Pitfalls" and related references. I already rewrote it due to unexpected issues. I welcome any suggestions.

Offline

#2255 2013-11-22 05:38:51

EvanPurkhiser
Member
From: Akron, Ohio
Registered: 2010-08-17
Posts: 225
Website

Re: Post your handy self made command line utilities

Here's a little bash function to copy your current termcap file to a remote server.

Pretty useful if you use an exotic terminal like rxvt-unicode-256colors or xterm-termite

#!/bin/bash

# Copy the current terminfo file to a remote host
ssh-copy-terminfo()
{
	local termpath="share/terminfo/$(echo "$TERM" | cut -c1)"
	local terminfo="$termpath/$TERM"

	for host in $@
	do
		local ssh_sock="$XDG_CACHE_HOME/ssh-copy-terminfo-$host"

		# Open a master SSH connection
		ssh -nNf -o ControlMaster=yes -o ControlPath="$ssh_sock" $host

		# Create the terminfo directory and copy the terminfo file
		ssh -o ControlPath="$ssh_sock" $host mkdir -p ".local/$termpath"
		scp -o ControlPath="$ssh_sock" "/usr/$terminfo" $host:.local/$terminfo

		# Close the connection
		ssh -O exit -o ControlPath="$ssh_sock" $host 2> /dev/null
	done
}

complete -F _known_hosts ssh-copy-terminfo

You may need to modify it slightly depending on what value you have set for TERMINFO. I have this line in my bashrc which puts my terminfo under the XDG_DATA_HOME directory.

Could probably use an extra check to see if the current termcap file is under TERMINFO instead of /usr/share/terminfo

Offline

#2256 2013-11-22 10:42:45

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,472
Website

Re: Post your handy self made command line utilities

Here's the script I use to set up an external monitor easier:

#!/bin/bash
# usage: monitor [on/off] mode
#        mode is optional, and is one of the positional arguments of xrandr.
#          valid options are: above [default], below, left-of and right-of

IN="LVDS1"
TV="HDMI1"
MON="VGA1"

case "$1" in
    on) MODE=$2
        if [ x"$2" = x ]; then
            MODE="above"
            echo "No mode parameter passed. Will fall back to above."
        fi
        if (xrandr | grep "$MON connected"); then
            xrandr --output $IN --primary --auto --output $MON --auto --$MODE $IN > /dev/null &
        elif (xrandr | grep "$TV connected"); then
            xrandr --output $IN --primary --auto --output $TV --auto --$MODE $IN > /dev/null &
        else
            echo "Monitor not connected."
        fi
    ;;
    off) if (xrandr | grep "$TV connected"); then
            xrandr --output $IN --auto --output $TV --off > /dev/null &
        elif (xrandr | grep "$MON connected"); then
            xrandr --output $IN --auto --output $MON --off > /dev/null &
        else
            echo "Monitor not connected."
        fi
    ;;
    single) if (xrandr | grep "$TV connected"); then
            xrandr --output $IN --off --output $TV --auto > /dev/null &
        elif (xrandr | grep "$MON connected"); then
            xrandr --output $IN --off --output $MON --auto > /dev/null &
        else
            echo "Monitor not connected."
        fi
    ;;
    *) echo "Command not supported."
esac

It automatically detects which screen is attached (in my case, I have two external screens I can attach - using only one at a time). Since I'm not good at bash scripting, I have this faint idea that it can be optimized very much wink

Last edited by Unia (2013-11-22 10:45:03)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#2257 2013-11-22 14:58:24

Marshal Kilgore
Member
Registered: 2013-10-29
Posts: 11

Re: Post your handy self made command line utilities

Unia wrote:

(...) Since I'm not good at bash scripting, I have this faint idea that it can be optimized very much wink

Is that a challenge? wink
This should do the same (be warned that I couldn't test it:)

#!/bin/bash

CMD="${1}"
MODE="${2:-above}"
IN=LVDS1
TV=HDMI1
MON=VGA1

errormsg () { echo "${0##*/}: ${1}" >&2; exit 1; }

if  xrandr -q | grep -q "^$MON connected"; then
    EXT="$MON"
elif xrandr -q | grep -q "^$TV connected"; then
    EXT="$TV"
else
    errormsg 'No monitor connected.'
fi

case "$CMD" in
    -h|--help|'')
        echo "Usage: ${0##*/} on|off|single [mode]"
        echo 'Mode is optional, and is one of the positional arguments of xrandr.'
        echo 'Valid options are: above (default), below, left-of and right-of.' ;;
    on)
        xrandr --output "$IN" --primary --auto --output "$EXT" --auto "--$MODE" "$IN" > /dev/null & ;;
    off)
        xrandr --output "$IN" --auto --output "$EXT" --off  > /dev/null & ;;
    single)
        xrandr --output "$IN" --off  --output "$EXT" --auto > /dev/null & ;;
    *)  
        errormsg "Command '$CMD' not supported." ;;
esac

Last edited by Marshal Kilgore (2013-11-23 12:37:35)

Offline

#2258 2013-11-23 22:14:47

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,472
Website

Re: Post your handy self made command line utilities

Marshal Kilgore wrote:
Unia wrote:

(...) Since I'm not good at bash scripting, I have this faint idea that it can be optimized very much wink

Is that a challenge? wink

Ha, yes it was a request for improving in disquise wink I copied your version, and it's working very smoothly!


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#2259 2013-11-23 23:18:28

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

Unia wrote:

Since I'm not good at bash scripting

When you have time, please read:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashPitfalls

Another useful resource is http://wiki.bash-hackers.org/

Anything else on the internet is likely wrong and misleading.  (There are one or two exceptions listed here: "http://wiki.bash-hackers.org/scripting/tutoriallist")

Offline

#2260 2013-11-24 11:47:32

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,472
Website

Re: Post your handy self made command line utilities

Thanks, but I'm probably not going to have time for that soon - university is really killing me with stuff I have to do.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#2261 2013-11-27 00:31:14

bb010g
Member
Registered: 2012-08-08
Posts: 16

Re: Post your handy self made command line utilities

One liner to set your PulseAudio volume and mute/unmute with dmenu and ponymix (I have it mapped to mod-v in xmonad):

dmenu -p 'Set volume %:' <<< "$(ponymix get-volume)"$'\n'"mute" | sed -n '/^[1-9]\?[0-9]$\|100\|^mute$/{s/[1-9]\?[0-9]\|100/ponymix set-volume &/ep; s/mute/ponymix toggle/ep; q}; q1'

date '+%A, %B %d, %Y %T' | pig | figlet | cowsay -n

Offline

#2262 2013-11-27 23:14:17

MrCode
Member
Registered: 2010-02-06
Posts: 373

Re: Post your handy self made command line utilities

#!/usr/bin/bash

pid=`xprop _NET_WM_PID | grep -Eo "[0-9]+"`

if (( $pid != 0 )); then
    kill -CONT $pid
fi

-----------8<-----------8<-----------8<-----------8<-----------

#!/usr/bin/bash

pid=`xprop _NET_WM_PID | grep -Eo "[0-9]+"`

if (( $pid != 0 )); then
    kill -STOP $pid
fi

I'm sure these are incredibly hackish and unwise to use with certain applications (or if you try to stop something during a system call), but I've found that binding these to a couple key combos has been nicely useful for literally pausing games and such that hog CPU/GPU resources even when in a "paused" state.

Offline

#2263 2013-12-05 22:02:09

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,033

Re: Post your handy self made command line utilities

I recently quit using Mendeley for managing my scientific literature because it's a piece of garbage. I now use cb2bib for metadata extraction/fetching and searching and for building bibtex files and reftex in Emacs for inserting references into my LaTeX files. But, I really missed the paper management in Mendeley: knowing when I added papers, which papers I haven't read yet, etc. So, I whipped up the following script for maintaining a reading queue. The queueing part is extremely simple (and I suppose you could easily use it for any kind of file). The bulk of the script is for pretty-printing article information (rather than just printing the file location). To do that, the files need to be named the same as the corresponding bibtex key (i.e. "Jones2012.pdf" -> "@article{Jones2012,..."). Just put one of the following in .config/paperq/paperq.conf:

bibtex-file /path/to/file.bib

or, to search in multiple files:

bibtex-dir /path/to/dir

I'm sure it could be improved in places but so far it's been working well for me.

#!/bin/bash

# paperq --- Maintain a queue of documents to read

# Copyright (C) 2013 Brandon Invergo <brandon@invergo.net>

# Author: Brandon Invergo <brandon@invergo.net>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

WRAP=64
OPENCMD=xdg-open

if [[ "x$XDG_DATA_HOME" == "x" ]]; then
    DATADIR=${HOME}/.local/share/paperq
else
    DATADIR=${XDG_DATA_HOME}/paperq
fi
QFILE=${DATADIR}/paperq
LOGFILE=${DATADIR}/paperq.log

if [[ "x$XDG_CONFIG_HOME" == "x" ]]; then
    CONFDIR=${HOME}/.config/paperq
else
    CONFDIR=${XDG_CONFIG_HOME}/paperq
fi
CONFFILE=${CONFDIR}/paperq.conf

display_help () {
    cat <<EOF
Usage: paperq [-a FILES|-l|-i|-r|-p|-h] [-n NUM]
Manage a reading queue for academic literature

  -a    add FILES to the queue
  -l    list all of the documents in the queue
  -i    display bibliographic data for a file
  -r    remove a file from the queue
  -p    peek at a file: open it, but do not remove it from the queue
  -h    display this message
  -n    set the queue position number on which to operate (default 1)

With no options, the first article in the queue will be opened for
reading and it will be removed from the queue.  Similarly, the options
-i (display bibliographic information), -r (remove item) and -p (peek
at item) work by default on the first item.  To read, display
information on, or remove an article in a different position, specify
it with the -n option.

The display of bibliographic data requires one or more bibtex files.
Document file names are assumed to correspond with the bibtex keys (i.e.
"Smith2012.pdf" -> "@article{Smith2012,...".  Inside the file
\$XDG_CONFIG_HOME/paperq/paperq.conf (or \$HOME/.config/paperq/paperq.conf),
either declare the "bibtex-dir" or "bibtex-file" variable followed by the
corresponding file/directory name, i.e.:

  bibtex-dir /home/john/bibtex

or

  bibtex-file /home/john/bibtex/mybib.bib

Only one of the two variable needs to specified.  Only one bibtex-file
will be used, regardless of the number defined.  bibtex-dir takes
precedence over bibtex-file.

If a bibtex file is available, listing the documents in the queue (-l)
will display their bibliographic data rather than their filenames.

Report bugs to: brandon@invergo.net
EOF
    exit 0
}

add_doc () {
    grep -q "$1" ${QFILE} || echo $1 >>${QFILE}
    echo "`date '+[%Y-%m-%d %H:%M]'` ADDED   $1" >>${LOGFILE}
}

get_doc () {
    sed -n "$1p" ${QFILE}
}

rem_doc () {
    echo "`date '+[%Y-%m-%d %H:%M]'` REMOVED `sed -n "$1p" ${QFILE}`" >>${LOGFILE}
    sed -i "$1d" ${QFILE}
}

read_doc () {
    $OPENCMD `get_doc $1`
    echo "`date '+[%Y-%m-%d %H:%M]'` READ    `sed -n "$1p" ${QFILE}`" >>${LOGFILE}
    rem_doc $1
}

wrap_bib () {
    bib=$1
    len=${#bib}
    numwords=${#bib[@]}
    offset=1
    line="${bib[@]:1:$numwords}"
    while ((offset < ${#bib[@]})); do
        while ((${#line} > WRAP)); do
            numwords=$((numwords - 1))
            line="${bib[@]:offset:numwords}"
        done
        if ((offset > 1)); then
            printf "\t%s\n" "$line"
        else
            printf "%s\n" "$line"
        fi
        offset=$((offset + numwords))
        numwords=$((${#bib[@]} - offset))
        line="${bib[@]:offset:numwords}"
    done
}

print_bib () {
    rec=`sed -n "/$1/,/^\}/{/.*/p}" $2`
    pos=$3
    if [[ "x${rec}" == "x" ]]; then
        # printf "No record for %s found in %s\n\n" "$1" "`basename $2`"
        return 1
    fi
    title=`sed -n -r '/^title/{s/^title *= *\{\{(.*)\}\},?/\1/;p}' <<EOF
$rec
EOF`
    auth=`sed -n -r '/^author/{s/^author *= *\{(.*)\},?/\1/;s/ and/,/g;p}' <<EOF
$rec
EOF`
    year=`sed -n -r '/^year/{s/^year *= *\{(.*)\},?/\1/;p}' <<EOF
$rec
EOF`
    journal=`sed -n -r '/^journal/{s/^journal *= *\{(.*)\},?/\1/;p}' <<EOF
$rec
EOF`
    vol=`sed -n -r '/^volume/{s/^volume *= *\{(.*)\},?/\1/;p}' <<EOF
$rec
EOF`
    num=`sed -n -r '/^number/{s/^number *= *\{(.*)\},?/\1/;p}' <<EOF
$rec
EOF`
    pages=`sed -n -r '/^pages/{s/^pages *= *\{(.*)\},?/\1/;s/ *-/-/;s/--/-/;p}' <<EOF
$rec
EOF`
    #author. year. title. journal vol(num):pages
    # printf '%s:\n' "`basename $2`"
    declare -a bib
    if [[ "x$num" == "x" ]]; then
        bib=(`printf "%s. %s. $(tput bold)%s$(tput sgr0) %s %s:%s\n\n" "$auth" \
            "$year" "$title" "$journal" "$vol" "$pages"`)
    else
        bib=(`printf "%s. %s. $(tput bold)%s$(tput sgr0) %s %s(%s):%s\n\n" \
            "$auth" "$year" "$title" "$journal" "$vol" "$num" "$pages"`)
    fi
    printf "%s:\t%s\n\n" $pos "`wrap_bib $bib`"
}

doc_info () {
    doc=`get_doc $1`
    key=`basename ${doc} .pdf`
    if [[ "x$BIBTEXDIR" != "x" ]]; then
        for bib in "${BIBTEXDIR}"/*.bib; do
            print_bib "${key}" "${bib}" $1
        done
    elif [[ "x$BIBTEXFILE" != "x" ]]; then
        print_bib "${key}" "${BIBTEXFILE}" $1
    else
        printf "No bibtex file specified.  Set either 'bibtex-file FILE' or \
'bibtex-dir DIR' in ${CONFFILE}\n"
    fi
}

list_docs () {
    if [[ "x$BIBTEXFILE" == "x" && "x$BIBTEXDIR" == "x" ]]; then
        awk '{print FNR": \t" $1}' ${QFILE}
    else
        numdocs=`wc -l ${QFILE} | awk '{print $1}'`
        if [[ "$numdocs" == "0" ]]; then
            return
        fi
        for pos in `seq 1 $numdocs`; do
            doc_info $pos
        done
    fi
}

parse_conf () {
    while read option value; do
        case "$option" in
            bibtex-file) BIBTEXFILE="$value";;
            bibtex-dir) BIBTEXDIR="$value";;
            "") ;;
            *) echo "$0: unrecognized config option '$option'";;
        esac
    done <${CONFFILE}
}

[ -d ${DATADIR} ] || mkdir -p ${DATADIR}
[ -d ${CONFDIR} ] || mkdir -p ${CONFDIR}
[ -f ${QFILE} ] || touch ${QFILE}
[ -f ${LOGFILE} ] || touch ${LOGFILE}
if [ -f ${CONFFILE} ]; then
    parse_conf
else
    touch ${CONFFILE}
fi

pos=1
mode=read_doc
while getopts "alirphn:" opt; do
    case $opt in
        a) mode=add_doc ;;
        l) mode=list_docs ;;
        i) mode=doc_info ;;
        r) mode=rem_doc ;;
        p) mode=peek_doc ;;
        h) display_help ;;
        n) pos=$OPTARG ;;
        \?) echo "Invalid option: -$OPTARG" && exit 1;;
    esac
done
case $mode in
    add_doc) shift; \
        for doc in $@; do \
            add_doc `realpath $doc`; \
        done;;
    list_docs) list_docs ;;
    doc_info) doc_info $pos ;;
    rem_doc) rem_doc $pos ;;
    read_doc) read_doc $pos ;;
    peek_doc) $OPENCMD `get_doc $pos` ;;
esac

Last edited by jakobcreutzfeldt (2013-12-05 22:10:50)

Offline

#2264 2013-12-13 14:31:40

Shark
Member
From: /dev/zero
Registered: 2011-02-28
Posts: 663

Re: Post your handy self made command line utilities

Hi

I know there are tons of pacman wrappers in AUR but sometimes it is better to do it yourself. So, i have made a very short python script which does:

  • displays first 3 Arch Linux title news and give you the option of viewing them

  • upgrade the system via Pacman

  • with Cower check for AUR changes and download the packages

That's it.

#!/usr/bin/python

#Reading first three of Arch Linux RSS news; then upgrading the system with pacman; the last
#is upgrading AUR packages with Cower

import feedparser
from subprocess import call
import re
import textwrap


d = feedparser.parse("https://www.archlinux.org/feeds/news/")

for f in range(0,3):
    print("\033[1;36m", d.entries[f].title, "\033[0;0m")
    xy = d.entries[f].published_parsed
    print("\033[3m", xy.tm_mday,xy.tm_mon,xy.tm_year, "\033[0m")
    print(d.entries[f].link, "\n")

def red_news():
    question = input("View the news? ")
    if question == "y" or question == "Y":
        a = int(input("Which news - 0,1,2: "))
        print("\033[1;36m", d.entries[a].title, "\033[0;0m")
        x = d.entries[a].description
        y = re.sub('<[^<]+>', '', x)
        c = textwrap.fill(y, width=90)
        print("\033[3m", c, "\033[0m")
        print("\n")

red_news()
print("\n")
print("\033[1;33m", "Upgrading Official Packages - Pacman Package Manager ...", "\n", "\033[0;0m")
call(["sudo", "pacman", "-Syu"])
print("\n")

print("\033[1;33m", "Upgrading AUR packages ...", "\n", "\033[0;0m")
call(["cower", "-udd"])

print("\n")

If you have built castles in the air, your work need not be lost; that is where they should be. Now put foundations under them.
Henry David Thoreau

Registered Linux User: #559057

Offline

#2265 2013-12-27 01:19:06

hyde
Member
From: Germany
Registered: 2012-11-17
Posts: 9

Re: Post your handy self made command line utilities

I use gnupod for my ipod and wanted to simplify usage. Here is a script that looks for the right disk to mount and mounts it.
Then i can use the gnupod scripts and finish everything with ipod -f, which updates the Ipod and unmounts.

My only disk in lsblk besides sr0 is sda, so if you have more in general you should adapt this.

ipod()
{
        # tested for zsh
	export IPOD_MOUNTPOINT='/media/ipod'

	case $1 in
		-f)
			echo 'updating...'
			mktunes.pl -m ${IPOD_MOUNTPOINT}
			echo 'unmounting...'
			pumount ${IPOD_MOUNTPOINT}
			;;
		*)
			count=$(lsblk | grep disk | grep 'sd[^a]' -c) || { echo 'no device found'; return }
			dev=$(lsblk | grep disk | grep 'sd[^a]' -o)
			if [[ $count != 1 ]]
			then
				echo 'multiple disks; choose:'
				echo $dev
				read dev
				# TODO: error checking
			else
				echo -n 'using '
				echo $dev
			fi

			pmount /dev/${dev}1 ipod

			echo 'ready. Modify your ipod and finish with ipod -f'
			;;
	esac
}

Offline

#2266 2014-01-02 16:22:42

wesleyac
Member
Registered: 2013-08-27
Posts: 80

Re: Post your handy self made command line utilities

A one-liner alarm clock.

sleep 1200;cvlc /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga --loop

Waits 20 minutes, then plays /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga on infinate loop.

It's simple, but I'm still proud of it.  (Plus I didn't have to install a bunch of GUI cruft big_smile )

Offline

#2267 2014-01-02 16:34:52

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,495
Website

Re: Post your handy self made command line utilities

wesleyac wrote:

A one-liner alarm clock.

sleep 1200;cvlc /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga --loop

Waits 20 minutes, then plays /usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga on infinate loop.

It's simple, but I'm still proud of it.  (Plus I didn't have to install a bunch of GUI cruft big_smile )

Neat! I didn't know about cvlc.

By the way, you can write "sleep 20m" for 20 minutes (see the man page for more options). Much easier for humans to read. smile

Offline

#2268 2014-01-02 16:35:42

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

Re: Post your handy self made command line utilities

Nice, I didn't know about sound-theme-freedesktop, thanks for sharing.

Offline

#2269 2014-01-02 22:25:17

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

Re: Post your handy self made command line utilities

If you use

sleep 1200;cvlc /usr/share/sounds/alsa/* --loop

you get an exercise trainer as well. wink

More seriously, as I could never be bothered to find other sounds, if i need a sound reminder in x minutes I tended to use that directory. The sound theme is interesting though, and I'll check it out, thanks.


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

#2270 2014-01-03 03:50:00

wesleyac
Member
Registered: 2013-08-27
Posts: 80

Re: Post your handy self made command line utilities

drcouzelis wrote:

By the way, you can write "sleep 20m" for 20 minutes (see the man page for more options). Much easier for humans to read. smile

Cool.  I was thinking about doing some pipe/sed magic to get that to work, but that's even better!  Teaches me to read the man pages tongue

karol wrote:

Nice, I didn't know about sound-theme-freedesktop, thanks for sharing.

Hah, I ddin't even know that I had anything specal for sounds installed.  Now that you mention it though, I do like the sounds.

Offline

#2271 2014-01-04 13:39:40

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: Post your handy self made command line utilities

Two crude scripts I use together with youtube-dl to manage my Youtube dowloads.

This renames the downloaded files and removes the video ID which is usually part of the file grabbed by youtube-dl. It watched only for a limited amount of extensions (e.g. FLV, MP4). If you grab other formats you might need to extend the search pattern.

renameYT:

#!/usr/bin/python
# -*- coding: utf8 -*-
import os, shutil

file_list = []

files = os.listdir('.')  

for n in files:
     extension = n[-3:].upper()
     if (extension == 'FLV') or (extension == 'MP4') or (extension == 'TXT') or (extension == 'MP3') or (extension == 'JPG'): file_list.append(n)
   
file_list.sort()

for source in file_list: 
  if source[len(source)-16] == '-':
    target = source[:-16] + source[-4:]
    print "Renaming " + '"' + source + '"' + " to " + '"' + target + '"'    
    shutil.move (source, target)
    
    if os.path.isfile (source + '.description'):
      shutil.move (source + '.description', target + '.description')

This script watches out for *part-files which ususally indicate incomplete downloads from youtube-dl. Then it restarts youtube-dl for the aborted downloads.

resumeYT:

#!/usr/bin/python
# -*- coding: utf8 -*-

# Modules used:
import sys, os, subprocess

part_files = os.popen ("ls *.part")

for part_file in part_files:
  target_name = part_file.strip()[:-5]
  target_name = target_name.split(".")[0]
  yt_id = target_name [len(target_name)-11:]
  cli = 'youtube-dl -t http://www.youtube.com/watch?v=' + yt_id
  subprocess.call(cli, shell=True)

I have assigned both script inside Thunar to a user action from my mouse menu. They come in pretty handy for quick processing huge amounts of files grabbed from youtube-channels. smile


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#2272 2014-01-04 19:33:30

aof
Member
Registered: 2010-06-18
Posts: 39

Re: Post your handy self made command line utilities

@Darksoul71

Instead of renaming, you can change filename template in youtube-dl.

youtube-dl -o "%(title)s.%(ext)s"

Offline

#2273 2014-01-04 20:19:47

HalosGhost
Member
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 1,486
Website

Re: Post your handy self made command line utilities

In addition to aof's recommendation, it's a bad idea to parse ls output. It was meant to be human-readable, not easily parseable. For that, use `find`.

All the best,

-HG


"All errors are ᴘᴇʙᴋᴀᴄ errors—It's just a matter of narrowing down which keyboard and chair." -Trilby
\ldots

Offline

#2274 2014-01-06 09:36:18

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: Post your handy self made command line utilities

aof wrote:

@Darksoul71

Instead of renaming, you can change filename template in youtube-dl.

youtube-dl -o "%(title)s.%(ext)s"

Thanks for the hint but this would break the option to (re)initiate download from Youtube. My connection to Youtube somehow is often quite flakey.
Things stop / interrupt quite often.


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#2275 2014-01-06 12:55:09

Earnestly
Member
Registered: 2011-08-18
Posts: 805

Re: Post your handy self made command line utilities

Darksoul71 wrote:
aof wrote:

@Darksoul71

Instead of renaming, you can change filename template in youtube-dl.

youtube-dl -o "%(title)s.%(ext)s"

Thanks for the hint but this would break the option to (re)initiate download from Youtube. My connection to Youtube somehow is often quite flakey.
Things stop / interrupt quite often.

youtube-dl -iwco "%(autonumber)s_%(title)s.%(ext)s" --restrict-filenames "url"

I have no idea what you're referring to about it breaking when you try to resume a download.  I use this command (with some alterations) in a script I use to download lecture playlists from youtube.  Being able to resume is paramount because each of the playlists can be large (for my connection) and `youtube-dl -c -w' handles this for me.

Edit: The program has for some time included a man page you can investigate for more information: `man youtube-dl'

Last edited by Earnestly (2014-01-06 13:11:53)

Offline

Board footer

Powered by FluxBB