You are not logged in.

#3201 2018-10-03 13:58:49

eschwartz
Trusted User/Bug Wrangler
Registered: 2014-08-08
Posts: 2,459

Re: Post your handy self made command line utilities

Trilby wrote:

Eschwartz, to be fair, they didn't sed pacman.conf themselves.  They read pacman.conf line by line in a clumsy and pointless loop and sed''ed part of each line for no reason only to check whether it matched 'IgnorePkg' and then only to oddly and conditionally append a space then a value to a simple variable when an array would be much easier needing no conditional or spaces.

And this is you being "fair"... tongue

Honest, yes, I will grant you that. But generally the phrase "to be fair" is used by people extending mercy, not additional objections. big_smile

If pacman-conf were not an option, that entire loop could just be:

IGNORE=$(sed -n 's/^IgnorePkg[^=]*=//p' /etc/pacman.conf)
# or better as an array
IGNORE=($(sed -n 's/^IgnorePkg[^=]*=//p' /etc/pacman.conf))

If it comes to that, matching on 'IgnorePkg222 = nope' and finding that the "nope" package should be ignored, is rather erroneous...

Don't match:

^IgnorePkg[^=]*=

Do match:

^IgnorePkg[[:space:]]*=

Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#3202 2018-10-03 14:01:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 20,306
Website

Re: Post your handy self made command line utilities

Good revision to my sed command.  To be fair, I'm an idiot. tongue


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3203 2018-10-03 15:31:43

Crouze
Member
Registered: 2017-10-04
Posts: 7

Re: Post your handy self made command line utilities

And to be fair, learned something new again today. Of course there was an easier way wink

Offline

#3204 2018-10-05 08:47:36

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

Re: Post your handy self made command line utilities

I've found this nice site that is missing an rss but gives you updates about new free games, so i wrote a little script that checks it and send me a notification when there is a new free game; didn't tested it deeply... yet!
It uses the handy "sendemail" tool from aur.

#!/bin/bash
wdir=/koko/scripts/gamecheck
f1="$wdir/1.txt"
f2="$wdir/2.txt"
mkdir "$wdir" &>/dev/null
interval=7200 #secs

function notify() {
	 /usr/bin/sendEmail \
		-f source_email@gmail.com \
		-t dest_email@gmail.com \
		-s smtp.gmail.com:25 \
		-xu gmail_username \
		-xp gmail_password \
		-u "GameCheck" \
		-m "New updates on: https://givemekey.com/?s=" 
}

function check_out() {
	curl "https://givemekey.com/?s=" |\
		tr "<" "\n"|\
		grep free |\
		grep "title\=" |\
		cut -d \" -f 2
}

check_out > "$f1"

while true ; do
	sleep $interval
	check_out > "$f2"
	if [ $(<"$f1") != $(<"$f2") ] ; then
		notify "$f2"
	fi
	cp "$f2" "$f1"
done

...and a systemd service as well

[Unit]
Description=Check for free games

[Service]
Type=simple
ExecStart=/home/koko/scripts/gamecheck.sh

[Install]
WantedBy=default.target

Last edited by kokoko3k (2018-10-05 08:55:15)

Offline

#3205 2018-10-24 13:28:47

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

Re: Post your handy self made command line utilities

This one will set the same "custom" refresh and resolution for all of the connected monitors.
I use it as a convenient way to play games with lower refresh rates when my GPU can't handle more.

#!/bin/bash

if [ ! $# = 2 ] ; then
	echo Example $(basename $0) 1280x1024 75
	exit
fi

w=$(cut -d "x" -f 1 <<< "$1")
h=$(cut -d "x" -f 2 <<< "$1")
hz=$2

#Genera la modeline
	GTF_OUT=$(gtf $w $h $hz|grep Modeline)
	MODE_NAME=koko_"$w"x"$h"_"$hz"
	TIMINGS=$(grep Modeline <<< $GTF_OUT |cut -d \" -f 3)
	MODELINE="$MODE_NAME $TIMINGS"

#aggiungo al pool:
	xrandr --newmode $MODELINE
	
#Aggiunge e setta il modo custom a tutti i monitor
	MONITORS=$(xrandr|grep " conne"|cut -d " " -f 1|tr \\n " ") 

	for MONITOR in $MONITORS ; do
		xrandr --addmode $MONITOR $MODE_NAME
		xrandr --output $MONITOR --mode $MODE_NAME
	done

	echo "Press ENTER in 10 seconds to keep settings"
	read -n 1 -t 10
	if [ ! $? == 0 ]; then
		#Cleanup:
		for MONITOR in $MONITORS ; do
			xrandr --output $MONITOR --preferred
			xrandr --delmode $MONITOR $MODE_NAME
			xrandr --rmmode $MODE_NAME
		done
	fi

EDIT:
removed 2 useless echo, before someone pointed it out; ready for the next tongue

Last edited by kokoko3k (2018-10-24 13:33:43)

Offline

#3206 2018-10-24 13:59:36

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 20,306
Website

Re: Post your handy self made command line utilities

You seem to do a lot of munging of variables only to undo it later.  For example, you have a subshell and pipeline to extract the width an height from $1, but then later you use "$w"x"$h" to put them back together.  You only need the individual width and height values once, so just extract them there, and use shell builtins to do it, e.g.:

timings=$(gtf ${1%x*} ${1#*x} $2 | awk '/Modeline/ { print $3; }')

Also note, the use of awk instead of grep and cut.
I can't test it, but the following should do the same thing as your script:

#!/bin/sh

if [ ! $# = 2 ] ; then
	echo Example $(basename $0) 1280x1024 75
	exit
fi

modename="koko_$1_$2"

xrandr --newmode $modename "$(gtf ${1%x*} ${1#*x} $2 |  awk '/Modeline/ { $1=""; $2=""; print; }')"

monitors=$(xrandr | awk '/ connected/ { print $1; }')

for monitor in $monitors; do
	xrandr --addmode $monitor $modename
	xrandr --output $monitor --mode $modename
done

read -p "Press ENTER in 10 seconds to keep settings" -n 1 -t 10 && exit

for monitor in $monitors; do
	xrandr --output $monitor --preferred
	xrandr --delmode $monitor $modename
done
xrandr --rmmode $modename

One thing that stands out is that almost all of the output of gtf is completely ignored - only the timing value is extracted and passed to xrandr's --newmode.  Is this really how that should work? (edit: ah, I misunerstood your cut pipeline, I've modified my version to get the equivalent output from gtf.)

Last edited by Trilby (2018-10-24 14:05:48)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3207 2018-10-24 14:19:26

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

Re: Post your handy self made command line utilities

Trilby wrote:

You seem to do a lot of munging of variables only to undo it later.

D'oh, you're right ^_^

Also note, the use of awk instead of grep and cut.

Unfortunately i'm not really familiar with it; i've to sort it out.

...apart from that, i prefer to use more and descriptive variables so that i'll not spend too much time next time to understand what i wrote.

Offline

#3208 2018-10-24 14:39:22

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 20,306
Website

Re: Post your handy self made command line utilities

You can still name variables and be more verbose than my examples without needing to use subshells with long pipelines:

...

width=${1%x*}
height=${1#*x}

timings=$(gtf $width $height ...)

My personal style is to never create a variable for something that will only be used once, unless the calculation of the value is pretty complex so that splitting off the calculation (and assignment) to a new line makes it easier to read.  In this case "${1%x*}" is quite short, and never reused, so I'd personally not use a variable for it - but that's a stylistic choice.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3209 2018-11-13 12:25:00

sjmulder
Member
Registered: 2018-11-13
Posts: 11

Re: Post your handy self made command line utilities

Man I just love writing little programs. Here's a few that may be useful:

json-yaml

Converts JSON to YAML for easy reading (or further processing):

$ curl -s http://api.icndb.com/jokes/random | ./json-yaml
type: success
value:
  id: 415
  joke: When Chuck Norris wants an egg, he cracks open a chicken.
  categories: []
nostt

A command line NOS Teletekst reader (a Dutch teletext system, like BBC's Ceefax).

within

My favourite one. Run a command in other directories:

$ within code/* - git status
code/json-yaml: nothing to commit, working tree clean
code/nostt: nothing to commit, working tree clean
code/within: nothing to commit, working tree clean
code/dated: nothing to commit, working tree clean
dated

Adds a timestamp to every line:

$ tail -f test.txt | dated
11/07/18 21:28:31 Hello
11/07/18 21:28:40 World!
$ tail -f test.txt | dated -f "[%H:%M] "
[21:32] Hello
[21:33] World!

Of course there are AUR packages for all of them big_smile

Offline

Board footer

Powered by FluxBB