You are not logged in.

#2176 2013-08-11 02:37:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

I guess not - just another option.  A link just seemed simpler than parsing the file, but no real difference.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#2177 2013-08-11 02:41:01

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Post your handy self made command line utilities

It probably is (simpler); I just hadn't thought of it... smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#2178 2013-08-11 10:22:46

Awebb
Member
Registered: 2010-05-06
Posts: 6,602

Re: Post your handy self made command line utilities

I just have one static wallpaper "file" with no name extention. It's just called "wallpaper" and is a symlink to whatever wallpaper I currently use. This link is used by the background setter, the screensaver and i3-lock. The moment you add another program, that calls this file, the less complicated it gets with a symlink. In your case, however, the only reason to prefer a symlink is, that anything, that changes a "critical" file like .xinitrc might corrupt that file in some edge cases.

But I think Trilby meant, that all that sed and awk is not very elegant here and that the symlink approach could solve this in half the lines. :-)

EDIT: Okay, I'm late to the show. Nevermind then.

Last edited by Awebb (2013-08-11 10:23:57)

Offline

#2179 2013-08-11 16:17:52

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

Re: Post your handy self made command line utilities

display -window root -size 0000x000 ~/.local/share/wall

Can even handle .webp formats.
I find it hard to believe anyone doesn't have imagemagick or graphicsmagick installed.

Offline

#2180 2013-08-11 16:23:18

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

Re: Post your handy self made command line utilities

Kaustic wrote:

I find it hard to believe anyone doesn't have imagemagick or graphicsmagick installed.

https://www.archlinux.de/?page=PackageStatistics (the page is a bit big, may load a while)
* imagemagick    85.99 %
* graphicsmagick 22.67 %

Offline

#2181 2013-08-11 16:55:50

HalosGhost
Forum Moderator
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 2,095
Website

Re: Post your handy self made command line utilities

Kaustic wrote:
display -window root -size 0000x000 ~/.local/share/wall

I'm actually rather interested in using this option to display my wallpaper instead of `feh` as I, also, am trying to switch over to sxiv. However, I'm having some trouble with that -size argument. It doesn't seem to be correctly sizing the image. Instead, the image is scaled so that the height fits which then repeats the image as tiles. The argument to size is meant to be the pixel counts (width and height) for which the image should be displayed, right?

All the best,

-HG

Offline

#2182 2013-08-11 17:18:44

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

Re: Post your handy self made command line utilities

karol wrote:
Kaustic wrote:

I find it hard to believe anyone doesn't have imagemagick or graphicsmagick installed.

https://www.archlinux.de/?page=PackageStatistics (the page is a bit big, may load a while)
* imagemagick    85.99 %
* graphicsmagick 22.67 %

Those statistics are far from true or accurate or even up to date.  They're fun though tongue

HalosGhost wrote:

The argument to size is meant to be the pixel counts (width and height) for which the image should be displayed, right?

The -size parameter is usually given when the size of an image isn't known (RAW) or to override the information in the image header.  It's mostly unnecessary in this case as display automatically scales the image to fit the root window's dimensions.

Last edited by Earnestly (2013-08-11 17:21:30)

Offline

#2183 2013-08-11 17:22:31

HalosGhost
Forum Moderator
From: Twin Cities, MN
Registered: 2012-06-22
Posts: 2,095
Website

Re: Post your handy self made command line utilities

Kaustic wrote:

The -size parameter is usually given when the size of an image isn't known or to override the information in the image header.  It's mostly unnecessary in this case as display automatically scales the image to fit the root window's dimensions.

But, it appears to fit the image to the height of the window, not the width, correct? Is there a way to reverse this?

All the best,

-HG

Offline

#2184 2013-08-11 17:32:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: Post your handy self made command line utilities

I've never used `display`, but most other imagemagick tools will preserve the aspect ratio while scaling (by default), so the smaller scale axis is used for both.  Usually a '!' after the size will scale to the exact size ignoring aspect ratio.  Otherwise, if you don't mind the top/bottom being cut off, you can just put a very large height and set the width to your screen width.  Then the image will be scaled (vertically and horizontally) with a scale factor to fit the width to the screen.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#2185 2013-08-11 18:11:29

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

Re: Post your handy self made command line utilities

I'm not sure, try asking on ##imagemagick on freenode or reading the documentation.  I would be surprised if it couldn't.
Edit: Ahh, Trilby has already provided an idea. -gravity center may work but I haven't tested.

/me still has to learn a lot about the power of imagemagick.

Last edited by Earnestly (2013-08-11 18:17:01)

Offline

#2186 2013-08-13 21:24:17

jefdaj
Member
Registered: 2012-12-05
Posts: 25

Re: Post your handy self made command line utilities

Slight modification of dmenu_run that pipes the output through xargs:

#!/bin/sh
# usage: dmenu_xargs <command> <dmenu_run args>

cmd="$1"; shift 1

cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
if [ -d "$cachedir" ]; then
  cache=$cachedir/dmenu_xargs
else
  cache=$HOME/.dmenu_cache
fi

( IFS=:
  if stest -dqr -n "$cache" $PATH; then
    stest -flx $PATH | sort -u | tee "$cache" | dmenu -p "$cmd" "$@"
  else
    dmenu -p "$cmd" "$@" < "$cache"
  fi
) | xargs -0 $cmd

It also defaults to using the command as the prompt name. I use it with i3 to have a variety of prompts in addition to regular dmenu_run:

# ~/.config/i3/config

# dmenu prompts
bindsym $mod+r dmenu_run
bindsym $mod+n dmenu_xargs i3-rename
bindsym $mod+b dmenu_xargs xbacklight

etc.

EDIT: Oh, and i3-rename is this:

#!/bin/bash
i3-msg "rename workspace to \"$@\""

Last edited by jefdaj (2013-08-13 21:25:46)

Offline

#2187 2013-08-14 11:33:55

windows_me
Member
From: England
Registered: 2013-08-14
Posts: 36
Website

Re: Post your handy self made command line utilities

If you use PowerDNS as your nameserver, then this script provides an easy way to manage dynamic DNS:
https://github.com/battlesnake/ddns
Includes a hacky web interface to manage your dynamic DNS remotely (and of course, for updating the dynamic IP address via a curl command!)

If you access your home PC a lot from work/other external networks, but don't want to run up an energy bill, then turn a Raspberry Pi into a wake-on-lan proxy:
https://github.com/battlesnake/macs
(note: via cron job, also maintains a list of machines detected on your network, with IP/hostname/MAC address, so you don't have to remember your PC's MAC address)

Last edited by windows_me (2013-08-14 11:34:57)


[10:04:21] Time for weekly full server backup.
[10:04:25] Redirecting it to "/dev/null" to make it go faster.
[10:04:53] Backup done! Amazing how fast modern technology is!

Offline

#2188 2013-08-14 17:49:31

wstewart90
Member
Registered: 2013-07-13
Posts: 17

Re: Post your handy self made command line utilities

Just a handy vnc script I wrote to deal with the whole xauth thing.

#!/usr/bin/bash
# Run this command as root
CURRENTUSER=$(ps -u|sed -n 2p|awk '{print $1}')

if [ "$CURRENTUSER" == "root" ]; then
X11AUTH=$(ps x|grep X|awk '{print $14}')
echo $X11AUTH
x11vnc -forever -rfbport $1 -display :0 -auth $X11AUTH
else
echo "You must be root to run this command"
fi

Edit: You may need to edit the awk like depending on your display manager. kdm has it in the 14th column but gdm-old has it in column 10. Also thanks for the $USER and $UID tip.

Last edited by wstewart90 (2013-08-15 06:05:06)

Offline

#2189 2013-08-14 17:54:36

Thaodan
Member
From: Dortmund, Nordrein-Westfalen
Registered: 2012-04-28
Posts: 448

Re: Post your handy self made command line utilities

you can use $USER or better $UID to detect if you're not root


Linux odin 3.13.1-pf #1 SMP PREEMPT Wed Mar 5 21:47:28 CET 2014 x86_64 GNU/Linux

Offline

#2190 2013-08-14 17:57:29

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Post your handy self made command line utilities

or whoami

Offline

#2191 2013-08-14 21:15:44

apoulos
Member
Registered: 2011-03-08
Posts: 5

Re: Post your handy self made command line utilities

After getting frustrated trying to find a TV episode downloader, I hacked together the following script. Anyone know of something similar?

#!/bin/bash

# Script to automatically scrape the kickass.to rss feed for tv episodes.
# Place episode configuration files with a .dl extension in same directory.
# Sample .dl file is shown below.  Script will source the .dl files one by one,
# and search for the episode by the variable URI. Results are filtered by
# variable MINSEEDS, and sorted to accept only the highest seeded match. If
# found, magnet is posted to transmission, and the EPISODE number in the .dl
# file is incremented.
#
# Dependencies: Script uses "transmission-cli", "curl", "xmlstarlet" for command "xml"
# Todo: season increment logic, download .torrent file to a "watch" directory
# 
# Example:
# Search for "burn notice 720p s07e10"
# file: burnNotice.dl
# ##################
# SEASON=7
# EPISODE=10
# SEASON0=`printf "%0*d" 2 ${SEASON}`
# EPISODE0=`printf "%0*d" 2 ${EPISODE}`
# URI="http://kickass.to/usearch/burn%20notice%20720p%20s${SEASON0}e${EPISODE0}/?rss=1"
# ##################

# authentication for transmission-remote
AUTH=user:password
# filter torrents with less than MINSEEDS
MINSEEDS=2

FILES=*.dl
for file in $FILES
do
	. $file
	echo curling rss feed of $file for s${SEASON0}e${EPISODE0}
	# xml command sorts descending on torrent:seeds where torrent:seeds >= $MINSEEDS
	MAGNET=$(curl --connect-timeout 2 -s "$URI" | xml sel -T -t -m /rss/channel/item -s D:N:- torrent:seeds -i "torrent:seeds>=$MINSEEDS" -v torrent:magnetURI 2>/dev/null)
	if [ "$MAGNET" != "" ]; then
		echo adding magnet to transmission...
		transmission-remote -n $AUTH -a "$MAGNET"
		if [ "$?" == "0" ]; then
			echo incrementing episode number
			SEDEX="s/^EPISODE=$EPISODE$/EPISODE=$(($EPISODE+1))/"
			sed "$SEDEX" -i $file
		else
			echo failed to submit magnet
		fi
	else
		echo failed to get magnet
	fi
	# give rss server a break
	sleep 1
done

Offline

#2192 2013-08-14 21:17:53

Thaodan
Member
From: Dortmund, Nordrein-Westfalen
Registered: 2012-04-28
Posts: 448

Re: Post your handy self made command line utilities

x33a wrote:

or whoami

using a var is faster than calling an external command, but if you using a posix shell than this would be what I do.

Here my function to calculate on shell:

rechner() {
        echo "$@" | bc -l
}

Linux odin 3.13.1-pf #1 SMP PREEMPT Wed Mar 5 21:47:28 CET 2014 x86_64 GNU/Linux

Offline

#2193 2013-08-14 21:26:05

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

Re: Post your handy self made command line utilities

Thaodan wrote:

Here my function to calculate on shell:

rechner() {
        echo "$@" | bc -l
}

I suggest using e.g.

echo "scale=3;$@" | bc -l

for your ordinary calculations, like '20/7'.

Offline

#2194 2013-08-15 00:21:36

Thaodan
Member
From: Dortmund, Nordrein-Westfalen
Registered: 2012-04-28
Posts: 448

Re: Post your handy self made command line utilities

karol wrote:
Thaodan wrote:

Here my function to calculate on shell:

rechner() {
        echo "$@" | bc -l
}

I suggest using e.g.

echo "scale=3;$@" | bc -l

for your ordinary calculations, like '20/7'.

Thanks I extended it a  bit:

rechner() {
        local arg scale=3 ;
        while getopts s: arg ; do 
                case $arg in
                        s) scale=$OPTARG;
                esac
        done
        shift $((OPTIND-1))
        echo "scale=$scale;$@" | bc -l 
}

Linux odin 3.13.1-pf #1 SMP PREEMPT Wed Mar 5 21:47:28 CET 2014 x86_64 GNU/Linux

Offline

#2195 2013-08-15 16:11:31

defer
Member
From: Finland
Registered: 2013-06-25
Posts: 46
Website

Re: Post your handy self made command line utilities

repeat.sh - Repeat command $x times after $y interval

if [[ "$1" =~ ^[0-9]+$ ]] && [[ "$2" =~ ^[0-9]+$ ]]; then	
	max=$1
	count=$2
	shift
	shift
	for ((i=1; i <= max ; i++)); do
		sleep $count
		eval "$@"
	done
else
	echo "Usage: $0 [count] [interval] command"
fi

Offline

#2196 2013-08-15 16:27:10

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

Re: Post your handy self made command line utilities

defer wrote:
		eval "$@"

Don't do eval, not here. Simply "$@". Hint: repeat.sh 2 0 echo '$(:)'


This silver ladybug at line 28...

Offline

#2197 2013-08-27 15:57:33

windows_me
Member
From: England
Registered: 2013-08-14
Posts: 36
Website

Re: Post your handy self made command line utilities

python -m http.server <port>

shares the current directory via HTTP over a certain port.  Ctrl+C to stop sharing.

To make this nicer, try the following:

echo 'python -m http.server $1' | sudo tee /usr/bin/share && sudo chmod +x /usr/bin/share

Then to share a folder temporarily, just use:

share <port>

Ctrl+C is still of course used to stop the server.

To run the server in the background, use

share <port> &

Then to stop it, use your favourite syntax of the kill command.

Note that you need root permission to use a port less than 1024.

Last edited by windows_me (2013-08-27 15:58:09)


[10:04:21] Time for weekly full server backup.
[10:04:25] Redirecting it to "/dev/null" to make it go faster.
[10:04:53] Backup done! Amazing how fast modern technology is!

Offline

#2198 2013-08-28 22:46:56

teckk
Member
Registered: 2013-02-21
Posts: 524

Re: Post your handy self made command line utilities

If anyone can use this.
A couple of scripts that I wrote for Google text to speech, and Google text to text translation.
They work from shell and allow you to translate until you quit.
The text to speech script concatenates to $FILE.mp3 so you can make a single file out of 100 char max. segments. The text to text dumps the output to sdout, redirect to $FILE if you want.
You might want to take it easy on how many times a minute you hit their server.

Text to text

#! /usr/bin/env bash

# Translate text using google. No google account needed.
# Requires curl, awk, iconv, html2text
# Language are:
# af ar az be bn bg bs ca cub zh-CN zh-TW cs cy da de en en_us en_gb en_au 
# el es et eu fa fi fr ga gl gu ht hi hmn hr hu hy is id it iw ja jw ka km 
# kn ko la lv lt mk mr ms mt no nl pl pt ro ru sr sk sl sq sw sv ta te th tl 
# tr uk ur vi yi

clear
OPTIONS="Input_Lang Output_Lang Enter_Text Translate Quit"
echo "Press Enter at any time to return to menu."
echo "Select an option:"
select opt in $OPTIONS; do

if [ "$opt" = "Quit" ]; then
clear
exit

elif [ "$opt" = "Input_Lang" ]; then
echo "Language options are:"
echo "en de fr es it" # Add languages here.
echo "What input language"
read ILAN

elif [ "$opt" = "Output_Lang" ]; then
echo "Language options are:"
echo "en de fr es it" # Add languages here.
echo "What ouput language"
read OLAN

elif [ "$opt" = "Enter_Text" ]; then
echo "Enter text"
read TEXT

elif [ "$opt" = "Translate" ]; then

result=$(curl -s -i --user-agent "" -d "sl=$ILAN" -d "tl=$OLAN" --data-urlencode "text=$TEXT" http://translate.google.com)
encoding=$(awk '/Content-Type: .* charset=/ {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' <<<"$result")

iconv -f $encoding <<<"$result" |  awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text

else
echo "Bad option"
fi; done

Text to voice

#! /usr/bin/env bash

# Text to speech conversion using google text to voice. No google account needed.
# Requires wget
# 100 char. max. Output to "$NAME.mp3"
# Language are:
# af ar az be bn bg bs ca cub zh-CN zh-TW cs cy da de en en_us en_gb en_au 
# el es et eu fa fi fr ga gl gu ht hi hmn hr hu hy is id it iw ja jw ka km 
# kn ko la lv lt mk mr ms mt no nl pl pt ro ru sr sk sl sq sw sv ta te th tl 
# tr uk ur vi yi

clear
OPTIONS="Language Enter_Text Filename Translate Quit"
echo "Press Enter at any time to return to menu."
echo "Select an option:"
select opt in $OPTIONS; do

if [ "$opt" = "Quit" ]; then
clear
exit

elif [ "$opt" = "Language" ]; then
echo "Language options are:"
echo "en de fr es it" # Add languages here.
echo "What Language?"
read LNG

elif [ "$opt" = "Enter_Text" ]; then
echo "Enter text to translate to voice"
read WORDS

elif [ "$opt" = "Filename" ]; then
echo "Output file name?"
read NAME

elif [ "$opt" = "Translate" ]; then
URL="http://translate.google.com/translate_tts?tl=${LNG}&q="
wget -q -U Mozilla -O - "$URL""$WORDS" >> "$NAME".mp3

else
echo "Bad option"
fi; done

Offline

#2199 2013-08-30 12:56:03

Pascal C.Plusplus
Member
From: France
Registered: 2012-09-27
Posts: 17

Re: Post your handy self made command line utilities

inspired from this but with bookmarks in a text file :

mark() {
	echo "$1|$(pwd)" >> $HOME/.marks
}

unmark() {
	sed "/^$1|/d" $HOME/.marks -i
}

jump() {
	dir=$(grep $1 $HOME/.marks)
	dir=${dir/*|/}
	cd $dir
}

# this is for zsh :

function _completemarks {
	reply=($(sed 's/\(.*\)|.*/\1/' $HOME/.marks))
}

compctl -K _completemarks cdm
compctl -K _completemarks delmark

Offline

#2200 2013-09-01 17:15:28

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Post your handy self made command line utilities

A simple wrapper for the surf browser, which removes the cookies.txt file (where surf stores its cookies) when surf exits.

#!/usr/bin/env bash

# start surf
/usr/bin/surf "$@"

# get pid
surf_running=$(pidof surf)

# if surf is not running, remove the cookies file
if [[ -z $surf_running ]]; then
    /usr/bin/rm "$HOME"/.surf/cookies.txt
    /usr/bin/rm -rf "$HOME"/.local/share/webkit
fi

Edit: Added the webkit directory. It has the localstorage crap and other unneeded stuff.

Last edited by x33a (2013-09-18 18:05:44)

Offline

Board footer

Powered by FluxBB