Categories
programming

Humble start of Bash scripting (part 2)

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
Categories
programming

Humble start with Bash scripting

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 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 geo-coordinates. After some checks for parameter and exiftool installed it reads the geo-coordinates 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