Easy steps for optimizing shell scripts
Computation
You have several different options for calculating the sum of numbers that a text file contains. In the example in Listing 14, every line containing the fiftieth
string is interesting. The script evaluates a file that contains one million lines. Every fiftieth
line contains the string.
Listing 14
Looking for a String
#!/bin/bash typeset -i sum=0 while read line; do set $line # Totaling field 6 sum=$((sum+$6)) # Parse output from grep done < <(grep " fiftieth " largefile) echo "Sum total: $sum"
Here, too, you can use Awk as a tool for quick summation (Listing 15). But Awk does not work directly in machine language. For this reason, it makes sense to pass the search for the character string to the grep
command and then add the lines found using Awk (Listing 16).
Listing 15
Faster with Awk
# time awk '/ fiftieth / {sum += $6} END {print "Sum total:", sum}' largefile
Listing 16
Fastest: Awk with grep
# time awk '{sum += $6} END {print "Sum total:", sum}' < <(grep " fiftieth " largefile)
In this example, too, optimization achieved significant speed gains. The variant from Listing 15 reduces the runtime to about one third; the variant from Listing 16 runs twenty times faster than the first alternative (see Table 3).
Table 3
Awk Timer
Category | |||
---|---|---|---|
real |
0m4.471s |
0m1.408s |
0m0.231s |
user |
0m2.374s |
0m1.348s |
0m0.050s |
sys |
0m1.956s |
0m0.013s |
0m0.010s |
Conclusions
The examples in this article show that you can drastically increase the speed of your scripts by skillfully using multifunctional tools such as Awk, Python, or Perl and by avoiding complex constructs with Tr, Sed, or Grep: You will thus consistently avoid many context changes.
However, not every anonymous pipe is detrimental to throughput, as the last example shows. Instead, it is more important to use the strengths of the various tools and keep the number of subprocesses as low as possible.
It is also important to remember that you can improve the readability and thus the maintainability of the scripts by doing without complex chains of commands.
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.