Title : 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.
```script shell requiring scrot, pngquant, ImageMagick and notify-send
#!/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.
```script shell
#!/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' >> /tmp/paste_upload
else
xclip -o | sed 's//\>/g' >> /tmp/paste_upload
fi
cat >> /tmp/paste_upload <
EOF
if [ -n "$2" ]
then
NAME="$2"
else
NAME=temp
fi
FILE=$(date +%s)_${1}_${NAME}.html
scp /tmp/paste_upload perso.pw:/var/www/htdocs/solene/prog/${FILE}
echo -n "https://perso.pw/prog/${FILE}" | xclip -selection clipboard
notify-send -u low "https://perso.pw/prog/${FILE}"
```
## Resize a picture
I never remember how to resize a picture so I made a one line script to not have to remember about it, I could have used a shell function for this kind of job.
```shell code
#!/bin/sh
if [ -z "$2" ]
then
else
fi
convert -resize "$PERCENT" "$1" "tn_${1}"
```
# Latency meter using DNS
Because UDP requests are not reliable they make a good choice for testing network access reliability and performance. I used this as part of my stumpwm window manager bar to get the history of my internet access quality while in a high speed train.
The output uses three characters to tell if it's under a threshold (it works fine), between two threshold (not good quality) or higher than the second one (meaning high latency) or even a network failure.
The default timeout is 1s, if it works, under 60ms you get a "_", between 60ms and 150ms you get a "-" and beyond 150ms you get a "¯", if the network is failure you see a "N".
For example, if your quality is getting worse until it breaks and then works, it may look like this: _-¯¯NNNNN-____-_______ My LISP code was taking care of accumulating the values and only retaining the n values I wanted as history.
Why would you want to do that? Because I was bored in a train. But also, when network is fine, it's time to sync mails or refresh that failed web request to get an important documentation page.
```shell script
#!/bin/sh
dig perso.pw @9.9.9.9 +timeout=1 | tee /tmp/latencecheck
if [ $? -eq 0 ]
then
time=$(awk '/Query time/{
if($4 < 60) { print "_";}
if($4 >= 60 && $4 <= 150) { print "-"; }
if($4 > 150) { print "¯"; }
}' /tmp/latencecheck)
echo $time | tee /tmp/latenceresult
else
echo "N" | tee /tmp/latenceresult
exit 1
fi
```
# Conclusion
Those scripts are part of my habits, I'm a bit lost when I don't have them because I always feel they are available at hand. While they don't bring much benefits, it's quality of life and it's fun to hack on small easy pieces of programs to achieve a simple purpose. I'm glad to share those.