Simple scripts I made over time
- Author: Solène
- Date: 19 July 2021
- Tags: openbsd scripts shell
Introduction
I wanted to share a few scripts of mine for some time, here they are!
Scripts
Over time I'm writing a few scripts to help me in some tasks, they are often associated to a key binding or at least in my ~/bin/ directory that I add to my $PATH.
Screenshot of a region and upload
When I want to share something displayed on my screen, I use my simple "screen_up.sh" script (super+r) that will do the following:
- use scrot and let me select an area on the screen
- convert the file in jpg but also png compression using pngquant and pick the smallest file
- upload the file to my remote server in a directory where files older than 3 days are cleaned (using find -ctime -type f -delete)
- put the link in the clipboard and show a notification
This simple script has been improved a lot over time like getting a feedback of the result or picking the smallest file from various combinations.
#!/bin/sh
test -f /tmp/capture.png && rm /tmp/capture.png
scrot -s /tmp/capture.png
pngquant -f /tmp/capture.png
convert /tmp/capture-fs8.png /tmp/capture.jpg
FILE=$(ls -1Sr /tmp/capture* | head -n 1)
EXTENSION=${FILE##*.}
MD5=$(md5 -b "$FILE" | awk '{ print $4 }' | tr -d '/+=' )
ls -l $MD5
scp $FILE perso.pw:/var/www/htdocs/solene/i/${MD5}.${EXTENSION}
URL="https://perso.pw/i/${MD5}.${EXTENSION}"
echo "$URL" | xclip -selection clipboard
notify-send -u low $URL
Uploading a file temporarily
Second most used script of mine is a uploading file utility. It will rename a file using the content md5 hash but keeping the extension and will upload it in a directory on my server where it will be deleted after a few days from a crontab. Once the transfer is finished, I get a notification and the url in my clipboard.
#!/bin/sh
FILE="$1"
if [ -z "$1" ]
then
echo "usage: [file]"
exit 1
fi
MD5=$(md5 -b "$1" | awk '{ print $NF }' | tr -d '/+=' )
NAME=${MD5}.${FILE##*.}
scp "$FILE" perso.pw:/var/www/htdocs/solene/f/${NAME}
URL="https://perso.pw/f/${NAME}"
echo -n "$URL" | xclip -selection clipboard
notify-send -u low "$URL"
Sharing some text or code snippets
While I can easily transfer files, sometimes I need to share a snippet of code or a whole file but I want to ease the reader work and display the content in an html page instead of sharing an extension file that will be downloaded. I don't put those files in a cleaned directory and I require a name to give some clues about the content to potential readers. The remote directory contains a highlight.js library used to use syntactic coloration, hence I pass the text language to use the coloration.
#!/bin/sh
if [ "$#" -eq 0 ]
then
echo "usage: language [name] [path]"
exit 1
fi
cat > /tmp/paste_upload <
EOF
# ugly but it works
cat /tmp/paste_upload | tr -d '\n' > /tmp/paste_upload_tmp
mv /tmp/paste_upload_tmp /tmp/paste_upload
if [ -f "$3" ]
then
cat "$3" | sed 's/\</g' | sed 's/>/\>/g' >> /tmp/paste_upload
else
xclip -o | sed 's/\</g' | sed 's/>/\>/g' >> /tmp/paste_upload
fi
cat >> /tmp/paste_upload <