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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Profiles and RC Files
- Understanding Ceph and Its Place in the Market
- Git 2.9 Released
- Astronomy for KDE
- Snappy Moves to New Platforms
- OpenSwitch Finds a New Home
- The Giant Zero, Part 0.x
- Maru OS Brings Debian to Your Phone
- Susan Lauber's Linux Command Line Complete Video Course (Prentice Hall)
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide