You are not logged in.

#2376 2014-06-12 21:33:05

mid-kid
Member
Registered: 2013-05-07
Posts: 177

Re: Post your handy self made command line utilities

karol wrote:

What is an aur binary repository? If midrepo is a regular repo, why not just run 'pacman -Syu'?

It's for when you don't want to compile the same program on every machine, you just create a repository and build stuff once.
I basically compare the version on the repo with the aur.


If it ain't broke, you haven't tweaked it enough.

Offline

#2377 2014-06-12 21:36:04

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

Re: Post your handy self made command line utilities

If you have these packages installed, you can do this directly with an AUR helper.

Offline

#2378 2014-06-12 21:39:50

mid-kid
Member
Registered: 2013-05-07
Posts: 177

Re: Post your handy self made command line utilities

karol wrote:

If you have these packages installed, you can do this directly with an AUR helper.

Yaourt -Syua only checks for AUR updates on foreign packages (listed with pacman -Qm).
That said, I haven't tried it with any other AUR helper.


If it ain't broke, you haven't tweaked it enough.

Offline

#2379 2014-06-13 11:35:48

quequotion
Member
From: Oita, Japan
Registered: 2013-07-29
Posts: 473
Website

Re: Post your handy self made command line utilities

This is essentially the same as what I'm using for indicator-session-systemd, just a little stripped down.

It provides handy shortcuts to hibernate, suspend, shutdown, and reboot that don't require a password

#! /bin/bash
# Shortcuts to systemd/logind's dbus session management

case "$1" in
	logout|quit|exit)
		SESSIONID=$(gdbus call -y -d org.freedesktop.login1 -o /org/freedesktop/login1 -m org.freedesktop.login1.Manager.GetSessionByPID $$ | sed -nr '/.*session\/(.{,2}).*/s//\1/p')
		# gdbus will throw and IO Error if sending more than two arguments,
		# even if the method requires three or more like KillSession.
		# Only root can kill sessions.
		sudo loginctl kill-session $SESSIONID
		exit # curious if this exit actually happens...
	;;
	suspend|standby|sleep)
		METHOD="Suspend true"
	;;
	hibernate)
		METHOD="Hibernate true"
	;;
	reboot|reset)
		METHOD="Reboot true"
	;;
	shutdown|poweroff)
		METHOD="PowerOff true"
	;;
	*)
		exit
	;;
esac

gdbus call -y -d org.freedesktop.login1 -o /org/freedesktop/login1 -m org.freedesktop.login1.Manager.$METHOD

Only root can end a session, so logout still requires a password.

Last edited by quequotion (2014-06-13 23:25:38)

Offline

#2380 2014-06-16 02:25:24

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,304
Website

Re: Post your handy self made command line utilities

Basic pomf uploader

#!/usr/bin/env bash
# upload images to pomf: requires jshon 

image="$1"
ext="(\.jpg|\.png|\.gif)$"

if (( $# != 1 )); then
  printf '%s\n' "Usage: ${0##*/} IMAGE"
  exit 1
elif [[ $image =~ $ext ]]; then
  up=$(curl -sf -F files[]=@"$image" http://pomf.se/upload.php)
  url=$(jshon -e files -a -e url -u <<< "$up")
  printf '%s\n' "http://a.pomf.se/${url}"
else
  printf '%s\n' "Not a recognized image type"
fi

Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#2381 2014-06-16 09:03:47

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

Re: Post your handy self made command line utilities

jasonwryan wrote:

Basic pomf uploader

...
  up=$(curl -sf -F files[]=@"$image" http://pomf.se/upload.php)
  url=$(jshon -e files -a -e url -u <<< "$up")
  printf '%s\n' "http://a.pomf.se/${url}"
...

The last two days I learned about httpie and jq; can't help using when a chance like this hits me in the face:

http -bf http://pomf.se/upload.php "files[]@$image" | jq -r '"http://a.pomf.se/\(.files[].url)"'

This silver ladybug at line 28...

Offline

#2382 2014-06-16 09:18:45

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,304
Website

Re: Post your handy self made command line utilities

lolilolicon wrote:

The last two days I learned about httpie and jq; can't help using when a chance like this hits me in the face:

http -bf http://pomf.se/upload.php "files[]@$image" | jq -r '"http://a.pomf.se/\(.files[].url)"'

Thanks lolilolicon: I saw jq on Lobsters (yesterday or today?) but hadn't come across httpie before. jq certainly seems simpler than jshon, but the advantage(s) of httpie still eludes me...

#edit: other way around: httpie is still on the front page (although I hadn't read it), not sure where I saw jq...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#2383 2014-06-16 10:20:19

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

Re: Post your handy self made command line utilities

jasonwryan wrote:

jq certainly seems simpler than jshon, but the advantage(s) of httpie still eludes me...

I'm not sure about jshon, but I feel jq is quite powerful/ambitious.
The main thing I see in httpie is a more convenient syntax, pretty output formatting, and --session feels nice too -- but it's absolutely not as powerful as curl. For example httpie seems not to be able to attach more than one file in a multipart form POST.
So, I've abandoned httpie for this script; here's my version:

#!/bin/bash

declare -a formparams
for image in "$@"; do
  formparams+=(-F "files[]=@$image")
done

response=$(curl http://pomf.se/upload.php "${formparams[@]}")
if [[ $(jq -c -r '.success' <<< "$response") == true ]]; then
  jq -c -r '"http://a.pomf.se/\(.files[]|.url + "  " + .name)"'
else
  jq -c -r '"Failed: \"\(.error)\""'
  exit 1
fi <<< "$response"

By the way, this site is really rude!

pomf.se wrote:

Enable JavaScript you fucking autist neckbeard, it's not gonna hurt you

or

pomf.se wrote:

Nigga what you doin' here?

completely offended offhandedly!


This silver ladybug at line 28...

Offline

#2384 2014-06-16 10:23:17

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,304
Website

Re: Post your handy self made command line utilities

lolilolicon wrote:
#!/bin/bash

declare -a formparams
for image in "$@"; do
  formparams+=(-F "files[]=@$image")
done

response=$(curl http://pomf.se/upload.php "${formparams[@]}")
if [[ $(jq -c -r '.success' <<< "$response") == true ]]; then
  jq -c -r '"http://a.pomf.se/\(.files[]|.url + "  " + .name)"'
else
  jq -c -r '"Failed: \"\(.error)\""'
  exit 1
fi <<< "$response"

That's pretty tidy...

pomf.se wrote:

Enable JavaScript you fucking autist neckbeard, it's not gonna hurt you

big_smile


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#2385 2014-06-16 12:48:57

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

Re: Post your handy self made command line utilities

jasonwryan wrote:

That's pretty tidy...

jq is somewhat powerful: I was able to put the if-else logic inside the jq "script"; here's the v2:

@!/bin/bash

declare -a formparams
for file in "$@"; do
  formparams+=(-F "files[]=@$file")
done
curl http://pomf.se/upload.php "${formparams[@]}" | jq -c -e -r '
if .success then
  "http://a.pomf.se/\(.files[]|.url + "  " + .name)"
else
  "Failed: \"\(.error)\"", null
end'

This silver ladybug at line 28...

Offline

#2386 2014-06-16 14:27:59

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

Re: Post your handy self made command line utilities

A CLI reddit listing, not pasting the code here since it contains non-printing characters (\x1b) used for color/underline. Download it here:

curl http://a.pomf.se/nhkudf.txt -o reddit.sh

Opionally, you can pass r/<subreddit> as $1.
screenshot: http://a.pomf.se/ypkbix.png

Edit: updated http://a.pomf.se/asfwml.txt (`empty` seemed to do something different from I expected).

Last edited by lolilolicon (2014-06-16 15:21:37)


This silver ladybug at line 28...

Offline

#2387 2014-06-16 14:29:12

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

Re: Post your handy self made command line utilities

Really jasonwryan, no need to call jshon/curl multiple times... http://sprunge.us/faAW?sh

Last edited by Earnestly (2014-06-16 14:30:04)

Offline

#2388 2014-06-16 14:41:48

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

Re: Post your handy self made command line utilities

@Earnestly, that's not perfect either, e.g. name could include white spaces. I'm sure Jason simply wanted a quick fix-up, something that was enough for his uses.

Edit: oops, I guess name is on its own line? didn't test, sorry mate.

Last edited by lolilolicon (2014-06-16 14:44:54)


This silver ladybug at line 28...

Offline

#2389 2014-06-16 17:17:22

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 18,304
Website

Re: Post your handy self made command line utilities

Earnestly wrote:

Really jasonwryan, no need to call jshon/curl multiple times... http://sprunge.us/faAW?sh

Humour me: how am I calling them multiple times? Or do you mean if I wanted to run it against more than one image?


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#2390 2014-06-16 21:53:25

xero
Member
From: 0x000000
Registered: 2014-04-02
Posts: 85
Website

Re: Post your handy self made command line utilities

simple script to convert a github.com url to a git.io url (of your choosing as opposed to a random one)

#!/bin/sh
echo -e "repo url:"
read repo
echo -e "short link:"
read link
curl -i http://git.io -F "url=$repo" -F "code=$link"

example:

[~]── - bash gitio.sh 
repo url:
https://github.com/syntax-samurai/ryu
short link:
ryu

HTTP/1.1 100 Continue

HTTP/1.1 201 Created
Server: GitHub.com
Date: Mon, 16 Jun 2014 21:50:42 GMT
Content-Type: text/html;charset=utf-8
Connection: keep-alive
Status: 201 Created
Location: http://git.io/ryu
Content-Length: 36
X-Runtime: 0.006205
X-Node: gitio1
X-Revision: bc60edd140f9d30d1bf9ba8313e0066b22xxxxxx

https://github.com/syntax-samurai/ryu

▬▬ι═══════ﺤ
http://git.io/.files

Offline

#2391 2014-06-19 00:24:54

MickeyRat
Member
Registered: 2011-11-15
Posts: 78

Re: Post your handy self made command line utilities

I'm not sure how many people want to replace truecrypt container files but, this is what I use.  First you have to make a LUKS encrypted file using mount loop.  This doesn't do that but, I don't do that very often.  This makes it easy to mount the file which I do a lot more often.  It requires the filename as the first argument on the command line.  What it does is mount the file and prompt to dismount it.  Until you answer that prompt, the file is available for use.

#!/bin/bash

# Verify that running as root
if [[ $UID -ne 0 ]]
then
  echo "$0 must be run as root"
  exit 1
fi

# Verify file exists
if [ ! -e $1 ]
then
	echo -e "$1 not found"
	exit 1
fi

# Find an available loop device and parse out name to use
loopdev=$(losetup -f)
loopnm=${loopdev##*/}
echo -e "Loop device $loopdev"
echo -e "Will mount $1 at /mnt/$loopnm"

# Create the loop device and open the encrypted container
losetup $loopdev $1
cryptsetup luksOpen $loopdev $loopnm

# Mount the encrypted file
if [ ! -d "/mnt/$loopnm" ]
then
	mkdir /mnt/$loopnm
fi

mount /dev/mapper/$loopnm /mnt/$loopnm
echo -e "\n$1 mounted at /mnt/$loopnm"

# Prompt then dismount
while true
do
	echo -e "\nPress enter to dismount"
	read junk
	umount -f /mnt/$loopnm && break
done

rmdir /mnt/$loopnm
cryptsetup luksClose /dev/mapper/$loopnm
losetup -d $loopdev
echo -e "/mnt/$loopnm dismounted and $loopdev available"

Some cause happiness wherever they go; others whenever they go.
- Oscar Wilde

Offline

#2392 2014-06-20 15:07:33

snakeroot
Member
Registered: 2012-10-06
Posts: 103

Re: Post your handy self made command line utilities

Using pam to create a "live" MOTD

This uses pam to run a script on login, here to give a "live" MOTD reflecting information on machine status I want to know when I first login to a console.

The script is what I use (just by way of example):

#!/bin/bash
echo ""
echo "Welcome to ${HOSTNAME}, ${PAM_USER}"
echo "Kernel:     $(uname -r)"
echo "Uptime:     $(uptime -p)"
echo "SSID:       $(iw wlp2s0 link | awk '/SSID/ {print $2}')" 
echo "Iptables:   $(systemctl is-active iptables)"
echo "Disk Use:  $(df --output=pcent / | sed 1d)"
echo "Battery:    $(upower --show-info /org/freedesktop/UPower/devices/battery_BAT0 | awk '/percentage/ {print $2}')"

Note the use of the variable $PAM_USER rather than $USER; if you want to print the user name or disk usage in $USER's home, you need to use $PAM_USER rather than $USER.

You then need to modify your /etc/pam.d/system-login file to add the following line:

session    optional   pam_exec.so          stdout /path/to/your/script/scriptname

Now your MOTD will really give you "news you can use" about the current system state.

I learned about this in this systemd-devel thread.

EDIT: Corrected SSID and Battery items per Steve's suggestion below. Thanks Steve.

Last edited by snakeroot (2014-06-20 21:12:40)

Offline

#2393 2014-06-20 15:56:17

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

Re: Post your handy self made command line utilities

To have awk match a word on a line, the syntax is:

 awk '/word/ {print $2}' 

This will allow you to remove the greps.

Offline

#2394 2014-06-26 01:53:20

firecat53
Member
From: Sammamish, Wa
Registered: 2007-05-14
Posts: 1,437
Website

Re: Post your handy self made command line utilities

Just a little script to import your Keepass 1.x database into GNU pass without needing to export in plain text/xml from keepassx. No GUI required, just:

pip install --user kppy
python keepass_kppy_pass.py <db name> [--keyfile <db key file if necessary>]

Offline

#2395 2014-06-26 02:07:45

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

Re: Post your handy self made command line utilities

jasonwryan wrote:

Splitting out the nbwmon posts to Community Contributions...

You're a good person!

All the best,

-HG


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

Offline

#2396 2014-06-28 07:11:41

axper
Member
Registered: 2013-01-25
Posts: 115

Re: Post your handy self made command line utilities

This script will convert the sepcified manpage to HTML with Arch Wiki's beautiful CSS and open the result in your $BROWSER. Here's a preview image.
The only requirement is: https://aur.archlinux.org/packages/mdocml/
Usage:

./<this script> <section name> <manpage name>

For example:

./<this script> 8 pacman

Latest version at https://github.com/axper/man2archwiki

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
''' This program converts a manpage to HTML with Arch Linux Wiki's CSS style.
    It uses mandoc (aka mdocml) for HTML convertion.
    The resulting file is then opened in a $BROWSER.

    Required arguments are section number and manpage name, for exmaple:
        ./<this script> 8 pacman

    Section number is required because I can't figure out how to specify
    2 poisitional arguments in argparse, with one of them non-required.
'''

import re, argparse, gzip, subprocess, os

CSS_CONTENT = '''
.head, .foot {
	display: none;
}

body {
	background: none repeat scroll 0% 0% #F6F9FC;
	color: #F9F9F9;
	direction: ltr;
	float: right;
	font-family: sans-serif;
	font-size: 12.7px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	font-weight: 400;
	line-height: 16px;
}

.mandoc {
	background-attachment: scroll;
	background-clip: border-box;
	background-color: #FFF;
	background-image: none;
	background-origin: padding-box;
	background-position: 0% 0%;
	background-repeat: repeat;
	background-size: auto auto;
	border-bottom-color: #CCC;
	border-bottom-style: solid;
	border-bottom-width: 1px;
	border-image-outset: 0 0 0 0;
	border-image-repeat: stretch stretch;
	border-image-slice: 100% 100% 100% 100%;
	border-image-source: none;
	border-image-width: 1 1 1 1;
	border-left-color: #CCC;
	border-left-style: solid;
	border-left-width: 1px;
	border-right-color: #CCC;
	border-right-style: solid;
	border-right-width: 1px;
	border-top-color: #CCC;
	border-top-style: solid;
	border-top-width: 1px;
	color: #000;
	direction: ltr;
	font-family: sans-serif;
	font-size: 12.7px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	font-weight: 400;
	line-height: 19.05px;
	margin-bottom: 50px;
	margin-left: 25.4px;
	margin-right: 25.4px;
	margin-top: 25.4px;
	padding-bottom: 12.7px;
	padding-left: 12.7px;
	padding-right: 12.7px;
	padding-top: 12.7px;
	position: relative;
	top: 10px;
	z-index: 2;
}

h1 {
	background-attachment: scroll;
	background-clip: border-box;
	background-color: transparent;
	background-image: none;
	background-origin: padding-box;
	background-position: 0% 0%;
	background-repeat: repeat;
	background-size: auto auto;
	border-bottom-color: #AAA;
	border-bottom-style: solid;
	border-bottom-width: 1px;
	color: #222;
	direction: ltr;
	font-family: sans-serif;
	font-size: 19.05px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	font-weight: 400;
	line-height: 19.05px;
	margin-bottom: 11.4333px;
	margin-left: 0px;
	margin-right: 0px;
	margin-top: 0px;
	overflow: hidden;
	overflow-x: hidden;
	overflow-y: hidden;
	padding-bottom: 3.23333px;
	padding-top: 9.53333px;
}

b, i {
	background-color: #EBF1F5;
	color: #222;
	direction: ltr;
	display: inline-block;
	font-family: monospace;
	font-size: 12.7px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	line-height: 19.05px;
	padding-bottom: 1.26667px;
	padding-left: 3.81667px;
	padding-right: 3.81667px;
	padding-top: 1.26667px;
}

b {
	font-weight: 400;
}

i {
	font-style: italic;
}

dt {
	background-color: #EBF1F5;
	color: #222;
	direction: ltr;
	display: inline-block;
	font-family: monospace;
	font-size: 12.7px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	line-height: 19.05px;
	padding-bottom: 1.26667px;
	padding-left: 3.81667px;
	padding-right: 3.81667px;
	padding-top: 1.26667px;
	margin-bottom: 0.1em;
}

dt b, dt i {
	background-color: #EBF1F5;
	display: inline;
	padding: 0 0 0 0;
	color: #000;
	direction: ltr;
	font-family: monospace;
	font-size: 12.7px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	font-weight: 400;
	line-height: 19.05px;
}

dt b {
	font-weight: 400;
}

dt i {
	font-style: italic;
}

dl {
	margin-top: 0.2em;
	margin-bottom: 0.5em;
	line-height: 1.5em;
}

p {
	color: #000;
	direction: ltr;
	font-family: sans-serif;
	font-size: 12.7px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	font-weight: 400;
	line-height: 19.05px;
	margin-bottom: 6.35px;
	margin-left: 0px;
	margin-right: 0px;
	margin-top: 5.08333px;
}

dd {
	margin-bottom: 0.1em;
	line-height: 1.5em;
	margin-left: 2.0em;
	margin-right: 0px;
}

i.link-sec, a b {
	background-color: #FFF;
	display: inline;
	padding: 0 0 0 0;
	color: #000;
	direction: ltr;
	font-family: sans-serif;
	font-size: 12.7px;
	font-size-adjust: none;
	font-stretch: normal;
	font-style: normal;
	font-variant: normal;
	font-weight: 400;
	line-height: 19.05px;
}

.link-man, .link-sec, .link-ext, a.link-ext > b {
        background-attachment: scroll;
        background-clip: border-box;
        background-color: transparent;
        background-image: none;
        background-origin: padding-box;
        background-position: 0% 0%;
        background-repeat: repeat;
        background-size: auto auto;
        color: #07B;
        direction: ltr;
        font-family: sans-serif;
        font-size: 12.7px;
        font-size-adjust: none;
        font-stretch: normal;
        font-style: normal;
        font-variant: normal;
        font-weight: 700;
        line-height: 19.05px;
        outline-color: #07B;
        outline-style: none;
        outline-width: 0px;
        text-decoration: none;
}

.link-man:hover, .link-sec:hover, .link-ext:hover, a.link-ext > b:hover {
        background-attachment: scroll;
        background-clip: border-box;
        background-color: transparent;
        background-image: none;
        background-origin: padding-box;
        background-position: 0% 0%;
        background-repeat: repeat;
        background-size: auto auto;
        color: #999;
        direction: ltr;
        font-family: sans-serif;
        font-size: 12.7px;
        font-size-adjust: none;
        font-stretch: normal;
        font-style: normal;
        font-variant: normal;
        font-weight: 700;
        line-height: 19.05px;
        outline-color: #999;
        outline-style: none;
        outline-width: 0px;
        text-decoration: underline;
}
'''

TMP_PREFIX = '/tmp/'
CSS_LOCATION = TMP_PREFIX + 'archwiki.css'
CSS_FILE = open(CSS_LOCATION, 'w+')
CSS_FILE.write(CSS_CONTENT)
CSS_FILE.close()


def capitalize_func(match):
    ''' Callback function for regex match.
        This function receives regex text enclosed in HTML header tag(s) and
        returns the same string, but with capitalized title.
    '''
    return match.group(1) + match.group(2).capitalize() + match.group(3)

PARSER = argparse.ArgumentParser(description='Convert manpage to HTML and'
                                 'open in browser')
PARSER.add_argument('manpage_section',
                    help='the section number of manpage')
PARSER.add_argument('manpage_name', help='manpage file to parse')
ARGS = PARSER.parse_args()

try:
    MANPAGE_LOCATIONS = subprocess.check_output(['man',
                                                 '--where',
                                                 '--all',
                                                 ARGS.manpage_section,
                                                 ARGS.manpage_name])
except subprocess.CalledProcessError:
    print('No such manpage found.')
    exit(1)

MANPAGE_LOCATION = MANPAGE_LOCATIONS.split()[0]

UNCOMPRESSED_FILE = gzip.open(MANPAGE_LOCATION, mode='rt')
UNCOMPRESSED_DATA = UNCOMPRESSED_FILE.read()
UNCOMPRESSED_FILE.close()

TEMP_FILE = open(TMP_PREFIX + 'temp_uncompressed_manpage', 'w+')
TEMP_FILE.write(UNCOMPRESSED_DATA)
TEMP_FILE.close()

FILE_MANPAGE = open(TMP_PREFIX + 'temp_uncompressed_manpage')

FILE_HTML_NAME = TMP_PREFIX + \
                 os.path.basename(ARGS.manpage_name) + \
                 '.' + ARGS.manpage_section + \
                 '.html'
FILE_HTML = open(FILE_HTML_NAME, 'w')

HTML_OUTPUT = subprocess.check_output(['mandoc',
                                       '-Thtml',
                                       '-Ostyle=' + CSS_LOCATION,
                                       '-Oman=man://%N.%S_LINKSNOTWORKING.html'
                                      ],
                                      stdin=FILE_MANPAGE)
FILE_MANPAGE.close()

HTML_OUTPUT_DECODED = HTML_OUTPUT.decode()
HTML_OUTPUT_DECODED = re.sub('(<h[1-7][^>]*>)(.+)(</h[1-7]>)',
                             capitalize_func,
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('<div style="height: 1.00em;">',
                             '<div style="height: 0.40em;">',
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('<br>',
                             '',
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('<b>[\n]<p>[\n]</b>',
                             '<p>',
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('<b></b>',
                             '',
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('<dt>[\n]</dt>',
                             '',
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('<dt>[\n]<p>[\n]</dt>',
                             '<p>',
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('<p style="margin-left: 5.00ex;'\
                             'text-indent: -5.00ex;">',
                             '<p>',
                             HTML_OUTPUT_DECODED)
HTML_OUTPUT_DECODED = re.sub('&#8722;',
                             '-',
                             HTML_OUTPUT_DECODED)

for line in HTML_OUTPUT_DECODED.split('\n'):
    FILE_HTML.write(line + '\n')
FILE_HTML.close()

# Redirection to /dev/null is a workaround around
# https://bugzilla.mozilla.org/show_bug.cgi?id=833117
DEVNULL = open(os.devnull, 'wb')
subprocess.call([os.environ['BROWSER'], FILE_HTML_NAME], stderr=DEVNULL)
DEVNULL.close()

Offline

#2397 2014-06-28 13:19:01

stevenhoneyman
Member
From: England
Registered: 2014-05-25
Posts: 240

Re: Post your handy self made command line utilities

A simple script to run something loads of times and tell you the average execution time. I wrote it to prove/decide between different methods of achieving the same thing e.g. how much difference between shell inbuilt commands and the same from /usr/bin (there is a *lot* of difference!)
...and "zcat file.gz | grep words" is faster than "zgrep words file.gz" yikes

Up-to-date should always be available from: GitHub

Current:

#!/bin/bash
#
# benchmark-exec-time.sh - Run a command n times and show average exec time
# 2014-06-28 Steven Honeyman 
#
# Usage: benchmark-exec-time.sh <numRuns> <command>
#

if [[ $# -lt 2 ]]; then
    echo 'Usage: benchmark-exec-time.sh <numRuns> <command>'
    echo ' $ benchmark-exec-time.sh 100 sleep 5'
    echo ' $ V=1 benchmark-exec-time.sh 10 testscript'
    exit 1
fi

logFile=$(mktemp benchmark-XXXXX.log)
TIMEFORMAT="%3R"
total=0
numRuns=$1; shift

for r in $(seq 1 $numRuns); do
    [ ! -z $V ] && echo "run $r... "
    { time $@ >/dev/null || exit 1; } 2>>$logFile
done

echo " Done. calculating..."
while read i; do
    total=$(bc<<<"$total+$i")
done < ${logFile}
rm $logFile

echo " Completed $numRuns runs of: '$@' in $total seconds"
echo " Average run time was: $(bc<<<"scale=3;$total/$numRuns") seconds"

Last edited by stevenhoneyman (2014-06-28 13:22:12)

Offline

#2398 2014-06-29 15:43:03

ObliviousGmn
Member
Registered: 2011-09-14
Posts: 49
Website

Re: Post your handy self made command line utilities

axper wrote:

This script will convert the sepcified manpage to HTML with Arch Wiki's beautiful CSS and open the result in your $BROWSER. Here's a preview image.
The only requirement is: https://aur.archlinux.org/packages/mdocml/
Usage:

./<this script> <section name> <manpage name>

For example:

./<this script> 8 pacman

Latest version at https://github.com/axper/man2archwiki


This is awesome!! Thank you!!

Offline

#2399 2014-06-29 16:47:26

stevenhoneyman
Member
From: England
Registered: 2014-05-25
Posts: 240

Re: Post your handy self made command line utilities

This sounded neater in my head big_smile Shows a list of missing files, but taking into consideration NoExtract settings from pacman.conf

pacman -Qk 2>&1 | awk '{if(NR==FNR){ if($0~/^NoExtract/)for(i=0;i<=NF;i++){if($i!~/^NoExtract|=|^!/){gsub("/","\\/",$i);gsub("\\.","\\.",$i);gsub("?",".",$i);gsub("*",".*",$i);a[$i]++;}}}else{m=0;if($1~/^warning/){for(r in a){if($3~r){m=1;break}};if(m==0){print "MISSING "$3}}}}' /etc/pacman.conf - 

(inspired by https://bugs.archlinux.org/index.php?do … k_id=41028 )

Offline

#2400 2014-06-30 19:24:19

Ashren
Member
From: Denmark
Registered: 2007-06-13
Posts: 1,221
Website

Re: Post your handy self made command line utilities

As a sysadm I've found this script useful in my daily work:

# ChangeLog script v. 0.4 -  Ashren

chlogpath="/ChangeLog"
 
if [[ -z "${SUDO_USER}" ]] && [[ $UID != 0 ]]; then
    echo "Please use sudo or root."
    exit 1
fi
 
if [[ ! -a $chlogpath ]]; then
    echo -e "\nChangeLog for ${HOSTNAME}" >> $chlogpath
fi
 
if [[ -z "${SUDO_USER}" ]]; then
    read -p "Please enter initials: " init
    sed -i '1i '"$(date)"' '"$init"'\n\n\n' $chlogpath
    vi +3 $chlogpath
    exit 0
else
    sed -i '1i '"$(date)"' '"$SUDO_USER"'\n\n\n' $chlogpath
    vi +3 $chlogpath
fi

Last edited by Ashren (2014-06-30 19:27:39)

Offline

Board footer

Powered by FluxBB