Do any nice shell scripts lately? I've been writing and optimising scripts, perhaps needlessly, for bourne shell and korn shell.
I wrote two scripts that allow a person to save a randomised list of files in a directory and then read one name from the list again and again, incrementing through it.
I dont feel like any of mine are worth talking about cause they're all trivial.Thats partly out of lack of experience and partly out of deliberate effort to do things trivially where possible, and make it possible where not.
Haven't really done anything too recent except some ricing shit.But a little while back I made a script to make these demotivators, even thought that meme is long past dead I guess I was just really bored one day. So you can just enter the input image, maximum dimensions, big and small text and the output filename.#!/usr/bin/env bash### NOTE: If Imagemagick can't find a font then you can specify a direct path to the font by using -font /path/to/font.otfif [ $# -lt 5 ]; then echo './memeDemotivator IN.jpg widthxheight big_text little_text out.jpg'else convert $1 -resize ${2}\> -bordercolor black -border 2%x2% -bordercolor white -border 2x2 -bordercolor black -border 10%x10% -extent 100%x95% -background black -gravity south -fill white -pointsize 42 label:"${3}" -append -pointsize 16 label:"${4}" -append -gravity north -extent 100%x103% $5fi
>>4Fuck, keep forgetting that the code tags are "cb" here.
]#!/usr/bin/env bash### NOTE: If Imagemagick can't find a font then you can specify a direct path to the font by using -font /path/to/font.otfif [ $# -lt 5 ]; thenecho './memeDemotivator IN.jpg widthxheight big_text little_text out.jpg'elseconvert $1 -resize ${2}\> -bordercolor black -border 2%x2% -bordercolor white -border 2x2 -bordercolor black -border 10%x10% -extent 100%x95% -background black -gravity south -fill white -pointsize 42 label:"${3}" -append -pointsize 16 label:"${4}" -append -gravity north -extent 100%x103% $5fi
>>6Cool! Though, I think the widthxheight parameter should be optional. Had to re-arrange things a little, as optional parameters are usually first or last in a list of parameters, I decided to make it the last parameter. I'm also a huge fag so I changed some of the style to what I prefer.#!/bin/sh#NOTE: If Imagemagick can't find a font then you can specify a direct path to the font by using -font /path/to/font.otfcase $# in5)convert $1 -resize "$5>" -bordercolor black -border 2%x2% -bordercolor white -border 2x2 -bordercolor black -border 10%x10% -extent 100%x95% -background black -gravity south -fill white -pointsize 42 label:"$2" -append -pointsize 16 label:"$3" -append -gravity north -extent 100%x103% $4;;4)convert $1 -bordercolor black -border 2%x2% -bordercolor white -border 2x2 -bordercolor black -border 10%x10% -extent 100%x95% -background black -gravity south -fill white -pointsize 42 label:"$2" -append -pointsize 16 label:"$3" -append -gravity north -extent 100%x103% $4;;*)echo "Usage: $0 in.file big_text little_text out.file [widthxheight]";;esacBy the way, it works on PNG images too.
$ echo $SECONDS 8.843Can your shell do this?
>>8It can. Interesting, I never knew about that variable before.
Found a neat tool:https://www.shellcheck.net/Gives you tips on how to improve your scripts.
I made a script for making maps from nasa hgt data#this depends on libgdal, libgdal-tools, and GraphicsMagick.(imagemagick didnt work for some reason) #those are their names on void linux. maybe different on other systems.#Take two arguments- a .hgt file, named as such. (ie M32.hgt), and a cmap file as the second.#!/usr/local/bin/rc file=`{echo $1|sed 's/.hgt//'}#tweaking z and s will change the black and white hillshade imagegdaldem hillshade $1 $file^'_hillshade.png' -z 1 -s 10000\t#youll need a cmap, given as the second argument. see:#https://thangbui.wordpress.com/2012/06/24/create-map-tiles-from-srtm-data-gdal-and-imagemagick/gdaldem color-relief $1 $2 $file^'_color.png' #adjust 'dissolve' here to see more or less of the hillshade image on the color heightmap#more hillshade, more 3d looking, less and its more colorfulgm composite -dissolve 33% $file^'_hillshade.png' $file^'_color.png' $file^'.png'rm $file^'_hillshade.png' $file^'_color.png'rm $1#example usage:#for(i in *.hgt){hgtconv.rc $i /somewhere/cmap.txt&}#this will freeze your system for some time while it runs.the image I got from it is far too big to upload but its here:https://a.safe.moe/FMDqK.bz2its a bzipped farbfeld so name it something.ff.bz2 if you download it.
>11Is this for plan 9? If not, will this run without changes on it? Genuinely curious.
>>12see the comments on the top, and the #!/usr/local/bin/rc, instead of just #!/bin/rcI just use rc on unix because Its my favorite and the only shell I know well enough. Obviously it wont run on plan9 unless you port libgdal, libgdal-tools, and graphics magick. or, maybe thered be a way to access them via a unix machine from plan9.
>>13I see. Thank you.
Search youtube acceptably quickly from the command line:#!/usr/local/bin/rcSEARCH=https://www.youtube.com/results?search_query=^`{echo $*|sed 's/ /+/g'}URLS=`{\t{for (i in ``(' ' $nl '"' '<' '>' ';'){curl -s $SEARCH}){\t\techo $i}\t}|grep 'watch?v='.\+|uniq|sed 's|https://www.youtube.com||'}{for(i in $URLS){\tyoutube-dl -q --get-title --get-id https://www.youtube.com^$i&}}|awk '!(NR%2){print "https://www.youtube.com/watch?v="$0};NR%2{print $0}'#eat shit, mpsyt!+does not interrupt a terminal session with a screen interface of the gimped imitation of GUI for hipsters that is TUI+does not require learning any special hotkeys, commands, or the organization of screens and menus, just "yt '[search term]'"+pipe output to other programs -no options to eg, change the number of search results, or print the thumbnail urls, or video descriptions. The latter there would be dead easy to add, the former not quite so easy, but not hard or anything. You'd have to make it get however many pages of search results necessary with curl and get a list of id's from all of them, then trim it to exactly the number you wanted with sed.--**all** of the information it needs is in the output of 'curl https://www.youtube.com/results?search_query=[argument with spaces translated to plus signs]' and yet, it uses youtube-dl. theres really no reason other than that I dont want to waste that much of my youth unwinding that shitpile. its not ideal and not easily fixed.--while acceptably fast, still apparantly slower than mpsyt or going to youtube.com, I think. I'm really happy about it though.
>>15Newer, faster, shittier edition:#!/usr/local/bin/rcSEARCH=https://www.youtube.com/results?search_query=^`{echo $*|sed 's/ /+/g'}youtube-dl --flat-playlist -j $SEARCH|jq '.title,.url' -rM|awk '!(NR%2){print "https://www.youtube.com/watch?v="$0}; NR%2{print $0}'++++As fast as mpsyt or searching youtube directly---depends on some new 'jq' json parser thing to translate \u83dc\u6708 \u5f3e\u304d\u8a9e\u308a\u7df4\u7fd2 cover \u3042\u306a\u305f\u306f\u7159\u8349 \u308f\u305f\u3057\u306f\u30b7\u30e3\u30dc\u30f3/\u30e9\u30d6\u30ea\u30fc\u30b5\u30de\u30fc\u3061\u3083\u3093--I see no obvious way to get it to report uploader, duration, thumbnail urls, or descriptions, as in the other version, the only easy extension I can see would be telling it how many pages of results to get and how many results exactly to display.-sometimes it doesnt report a title for the video-it still could have gotten all of this from just 'curl -s $SEARCH'This is the "Crystal edition" of yt, because it uses the /J/avascript /O/bject notation.
>>12>>14heres an example of a plan 9 script!#!/bin/rcif(~ $#* 0){ echo 'Usage: ehget [gallery link]'; exit 1 }nl=''seps=(" '>' '<')fn imgurl{\tfor(i in `$seps{hget $*}){\t\techo $i|grep -e '.jpg' -e '.png'|grep -v 'https://e-hentai.org'|grep 'http://'\t}}fn getimg{\turl=`{imgurl $*}\tsplit=`'/'{echo $url}\tname=`{ {for(i in $split){echo $i}}|tail -n 2|sed 1q }\thget $url > $name}fn pgurls{\tfor(i in `$seps{hget $*}){ \t\techo $i |grep 'https://e-hentai.org/s/'\t} }for(i in `$nl{pgurls $*}){\tgetimg $i&}dont install it though. glenda is a PURE bunny.
I didnt do my reps today, but I wrote a script that lets you do your reps on plan9! So it evens out![cb]#!/bin/rc tab='\t'nl=''fn isdue{\thoc -e 'n=6 \treps='$2'\tif(reps > 2){\tn *= '$3'^(reps-2)\t} else if(reps < 2){\tn=reps\t}\tif(n <= ('^`{date -n}^'-'$1')/86400){\tprint "due"\t} else { print "dame" }'}fn nef{\thoc -e '\tef = '$2' + (0.1-(5-'$1') * (0.08+(5-'$1') * 0.02) )\tif(ef > 2.5) { ef = 2.5 } else if(ef < 1.3){ef = 1.3}\tef'}#juku deckfiletouch /tmp/jrev.tmpwhile(c=`$tab$nl{fortune $1}){\tnr=$c(1)\tq=10\tif(~ `{isdue $c(2 3 4) } due){\t\techo $c(5)\t\tread >>/dev/null\t\techo $c(`{seq 6 $#c})\t\techo 0 1 2 3 4 5\t\twhile(! q=`{read|grep '^[0-5]$'}){echo 0 1 2 3 4 5 }\t\tef=`{nef $q $c(4)}\t\tdate=`{date -n}\t\tawk 'BEGIN {FS="\t"; OFS="\t";}\t\t\tNR==ENVIRON["nr"] {if(ENVIRON["q"]<=3) {$3=0} else {$2=ENVIRON["date"];++$3;$4=ENVIRON["ef"]} } \t\t\t{print}' $1 >/tmp/$1.tmp\t\tcat /tmp/$1.tmp >$1\t}\tif(~ `{hoc -e '3<'$q} 1 && ! grep -s '^'$nr'$' /tmp/jrev.tmp){echo $nr >>/tmp/jrev.tmp}\tif(~ `{wc -l < /tmp/jrev.tmp} `{wc -l < $1}){\t\techo 'All done for today~'\t\trm /tmp/jrev.tmp; rm /tmp/$1.tmp;\t\texit 0\t}}[cb]It implements the sm2 algorithm, like anki uses. Decks are 'id timestamp reps easynessscore front back [back ...]'., tab seperated. timestamp isi the date it was last reviewed in unix seconds, reps the number of times its been reviewed before, easynessscore a number between 1.3 and 2.5 that should start at 2.5. id is any(unique!) id.
Which OS are you running rc as your shell? OpenBSD?
>>19#!/bin/rc on plan9 itself.#!/usr/local/bin/rc on void linux.on unix I use http://github.com/muennich/rc , which has tab completion of program names and of ~/(with readline).
>>6>>7I realised I could avoid the dumb mistake of repeating code.#!/bin/sh#NOTE: If Imagemagick can't find a font then you can specify a direct path to the font by using -font /path/to/font.otfcase $#in4|5)RESIZE=${5:+"-resize $5>"}convert "$1" $RESIZE -bordercolor black -border 2%x2% -bordercolor white -border 2x2 -bordercolor black -border 10%x10% -extent 100%x95% -background black -gravity south -fill white -pointsize 42 label:"$2" -append -pointsize 16 label:"$3" -append -gravity north -extent 100%x103% "$4";;*)echo "Usage: $0 in.file big_text little_text out.file [widthxheight]";;esacCode is much more maintainable if you don't repeat the same things twice.
>>21Thanks. Been saving each version
[code]test[code]
>>23You messed up the closing tagtest
>>24test2
I found that on some Unix systems where the daily, weekly, or monthly scripts seem to not work from the command line, running them in the background (applying the & after a command) makes them work.I think it's a bug in the way they mail the results to root.
#!/bin/shfind_in_directory() { find "$1" -type f -print0 \ | xargs -0 exiftool -genre 2>/dev/null \ | awk -v RS='======== ' -v FS='\n' -v ORS='\0' \ "toupper(\$2) ~ /^GENRE[^\n]+${GENRE}/ { print \$1 }"}if [ $# -lt 1 ]; then echo "${0}: find music by genre." echo "Usage: ${0} GENRE [PATH...]" exit 1fiGENRE=$(echo $1 | tr '[a-z]' '[A-Z]')shiftif [ $# -gt 0 ]; then while [ $# -gt 0 ]; do find_in_directory "$1" shift doneelse find_in_directory .fi
>>27Fascinating. I didn't know about shift.