Better Shell Script Performance in GNU Bash - Avoiding Subshells, grep, sed, and More - Jody Bruchon
Jody Bruchon
You can also watch this on YouTube: https://www.youtube.com/watch?v=U4AgozI_vT4
If you're using subshells, grep, sed, and tr inside loops in your shell scripts, you might be able to use some built-in features of GNU Bash to dramatically speed those scripts up. Parameter expansion, globbing, and extended regular expression (regex) matching can be used to replace a lot of string manipulation constructs, reducing or eliminating external commands that must be fork-exec'd and waited upon, including subshells and common simple uses of grep and sed to locate and clean up text strings. Also, sometimes it's better to write a small C helper program if you have a particularly "hot" chunk of script code, and an instance where I've done this instead of Bash optimizations is presented in the video.
This video illustrates four useful constructs:
String search-and-replace: ${VAR/search_term/replace_term} Double-bracket globbing: [[ $VAR == substring_to_match ]] Regex string matching: [[ $VAR =~ regex_to_match ]] Line finding: while read X; do [[ $X == string ]] && break; done ‹ file_to_scan
Chapters: 00:00 Opening 00:24 Intro & Git log 03:31 Replace echo-sed with Bash search-and-replace 13:54 An alternative: write a C helper program 19:25 Replace grep -q with Bash regex matching 27:52 Globbing is faster than regex matching 28:23 Replace grep -m 1 with 'while read' loop
Support me on SubscribeStar: https://www.subscribestar.com/JodyBruchon Personal/programming site: https://www.jodybruchon.com/ Video production site: http://www.gazingcat.com/ Computer repair site: http://nctritech.com/
144720720 Bytes