Work the Shell - Resizing Images, Sort Of
Here's the basic script at this point:
filename="edit.png" multiplier="0.75" width="$(file $filename | cut -f5 -d\ )" height="$(file $filename | cut -f7 -d\ | sed 's/,//')" width="$(echo "$width * $multiplier" | bc | cut -d. -f1)" height="$(echo "$height * $multiplier" | bc | cut -d. -f1)" echo "$filename scaled: width=$width height=$height"
Testing it with the filename specified produces the following:
$ sh scale-image.sh edit.png scaled: width=541 height=539
That's not really exactly what I want, however. First, I want to be able to specify the filename and multiplier on the command line. Second, the output needs a slight tweak to be more useful—the values need to be surrounded by quotation marks.
Here's what I'd like to see:
$ sh scale-image.sh 0.75 edit.png edit.png: width="541" height="539" $
That's not too hard to accomplish given the basic script we already have. See if you can do it yourself.
Tip: I actually use a “for name; do; done” loop to step through the file scaling, so I can specify a group of images and calculate them all en masse. Try it, coupled with the shift command, to remove the multiplier value once it's saved into a named variable.
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com. Follow him on Twitter if you'd like: twitter.com/DaveTaylor.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
- Readers' Choice Awards 2013
- Linux Kernel News - November 2013
- Mars Needs Women
- Sublime Text: One Editor to Rule Them All?
- RSS Feeds
- Raspberry Pi: the Perfect Home Server
- Advanced Hard Drive Caching Techniques
- December 2013 Issue of Linux Journal: Readers' Choice
- Web Administration Scripts
- New Products
- on the ground
5 hours 14 min ago
- I was able to read the whole
6 hours 43 min ago
- since i have read the title i
10 hours 3 min ago
- Belanja Online Cari Voucher Diskon
10 hours 8 min ago
- The kernel doesn't really
22 hours 19 min ago
22 hours 50 min ago
22 hours 50 min ago
1 day 55 min ago
- This should be very helpful
1 day 2 hours ago
- As much as I share your point
1 day 4 hours ago