Humble start of Bash scripting (part 2)

In Part II of my Bash scripting adventures I’ll write a script to resize an image.

As a follow up on my Bash scripting efforts that I started this year I wrote a script that resizes a photo to a specified width in pixels. The script should work both on Linux and OSX (tested on OSX, will test on Linux later). For this script to work ImageMagick needs to be installed. The script tests if an argument is specified (the original photo of course), if ImageMagick has been installed and if the file output.jpg exists. If output.jpg exists the user gets the option to overwrite the existing file or exit the script. Finally the user needs to specify the width in pixels of the output.jpg.

If you want to use this script, copy the code below and paste it into an editor like vim, vi or Geany. Save it e.g under the name resize and make this file executable by typing.

chmod 755 resize

Now if the photo is in the same folder as the script just type.

./resize yourphoto.jpg

Cheers.

The script

#!/bin/bash
# written by Eric Buijs 12 january 2019

if [ $# -eq 0 ]
	then
		echo You need to specify a file.
		echo e.g: resize photo.jpg
		exit 1
fi

if [ ! $(which convert) ]
	then
		echo This script requires Imagemagick!
		echo You can download it at http://www.imagemagick.org/
		exit 1
fi

if [ -e output.jpg ]
	then
	echo output.jpg already exists.
	read -p "Do you want to overwrite it? (Y/N) " answer
	echo $answer
	if [ $answer = "N" ]
		then
		exit 1
	fi
fi

read -p "What is the desired width in px: " width
convert $1 -resize $width output.jpg

Humble start with Bash scripting

2019 is a good year to start scripting with Bash

To kick off the year I started to learn Bash scripting, something I wanted to do a very long time. I humbly began with tutorials on the web like this one and I’m rewriting scripts from Smokey01, a fanatic Puppy Linux user. As an exercise I simply rewrote this script to work on OSX. If you want to use it you need to install exiftool. I installed exiftool with Homebrew and typed brew install exiftool but there’s also a dmg file available. If you don’t use Homebrew you do have to change the check if exiftool is installed.

Now for the script. It reads a jpeg file that must contain geocoordinates. After some checks for parameter and exiftool installed it reads the geocoordinates and stores them in the variable coord. This variable is then added to a Google search query and the result displayed in the browser.

Save the script e.g. with the name place and run with place /path/to/photo.jpg to display the location where photo.jpg was taken.

The script

1 #!/bin/bash
2 # Originally written by smokey01 28 May 2017
3 # Rewritten for OSX by Eric Buijs 9 Jan 2019
4 if [ $# -eq 0 ]
5 then
6 echo You need to specify a file.
7 echo EG: place photo.jpg
8 exit 1
9 fi
10
11 if [ ! -d /usr/local/Cellar/exiftool ]
12 then
13 echo “This script requires exiftool!”
14 exit 1
15 fi
16
17 coords=`exiftool -n -p ‘$GPSLatitude,$GPSLongitude’ $@`
18 read -e -p “Do you want to see the location in your Browser? ” choice
19 [[ “$choice” == [Yy]* ]] && open https://www.google.com/search?q=$coords || exit 1

Give your old PC a new life

My neighbors wanted to throw away a perfectly good PC with Windows 10. But instead I installed Ubuntu MATE giving the PC a new life and didn’t let it become ewaste.

When I came home the other day my neighbors were loading electronics on their bicycles (I live in the Netherlands you know) to dispose of them. Among all the stuff was a desktop computer that looked in pretty good shape. It was a Packard Bell iMedia S1800. I informed why they would dispose of the PC and they told me that according to their daughter it had become unusable. They are very nice people so I asked them if I could have the PC. They agreed to it and I took the desktop with me.

Back home I took another look at the computer and it was even nicer than I expected. It didn’t have a scratch and when I opened it it looked clean. That night I booted the PC and quickly found out it had Windows 10 running. The PC was slow as molasses and it was very noisy. I felt I had already found the cause of the daughter complaining, Windows 10. I had no intention to run Windows so I took out my Puppy Linux disk (Xenial Pup 7.5) and rebooted the PC.

The difference couldn’t have been greater. Puppy Linux booted fast and ran even faster. I got the idea to make this a general purpose workstation that, if goes well, can replace my iMac. How much I love Puppy Linux I don’t think it’s suitable for my purpose. Besides all the usual tasks I use my iMac for light 3D CAD work (for 3D printing), web design and video editing. I also like to have access to a broad software repository because I like to test new software and to replace the workflow I have on the Mac. In this department Puppy Linux can be lacking due to the Puppy Package Manager which differs from the mainstream package managers.

I therefore decided to give Ubuntu MATE 18.04 LTS (Bionic Beaver) a spin. My son already runs Ubuntu so I’m familiar with it and the MATE desktop is relatively lightweight (Gnome 2). Installation from a USB drive went flawless (overwriting W10 in the process). The desktop looks very clean and the software suite is great. I can add software through the new Software Boutique and if it isn’t there I have access to the Ubuntu repository (through apt). For a full review of MATE 18.04, read this.

So here I am. A nice Packard Bell is sitting on my desk instead of having become e-waste. If you read this don’t throw away your old PC because Windows 10 made the experience a nightmare. Install Ubuntu MATE or any other Linux distro you like and enjoy. In the mean time you’ll be doing the environment a favour. Cheers.