官术网_书友最值得收藏!

Introduction

Hopefully, the previous Bash crash course chapter provided more than a hint of the utility and power of Bash. On the other hand, this chapter introduces several bolt-on technologies to make Bash even more extensive when searching for items and text, or automating file explorer/file system operations. 

By itself, Bash is merely a powerful scripting language, but much of Bash's flexibility comes from being able to "glue" other technologies (tools or languages) together to make the output more useful. In other words, Bash is a base platform similar to how some auto/car lovers choose a particular platform before making their modifications. Will a modified car do everything, even with enhancements? Certainly not, but it can make it more powerful or useful in specific cases, and at least provides four wheels for movement.

Not only do common scripts contain a series of commands for automation, they often include logic to modify strings such as the following:

  • Removing trailing characters
  • Replacing sections of words (substrings)
  • Searching for strings in files
  • Finding files
  • Testing file types (directory, file, empty, and so on)
  • Performing small calculations
  • Limiting the scope of searches or data (filtering)
  • Modifying the contents of variables (strings inside of string variables)

This logic that modifies, limits, and even replaces input/output data can be very powerful when you need to execute broad searches for a specific string or when you have copious amounts of data. Terminals chock; full of output or massive data files can be very daunting to explore! 

However, there is one very important concept that still needs to be discussed, and that is recursive functionality. Recursive functionality can apply to script functions, logic, and even a command operation. For example, you can use grep to recursively crawl an entire directory until no more files remain, or you can recursively execute a function inside of itself until a condition is met (for example, printing a single character at a time within a string):

# e.g. File system
# / (start here)
# /home (oh we found home)
# /home/user (neat there is a directory inside it called user)
# /home/user/.. (even better, user has files - lets look in them too)
# /etc/ # We are done with /home and its "children" so lets look in /etc
# ... # Until we are done
Be careful with recursion (especially with functions), as it can sometimes be really slow depending on the complexity of the structure (for example, file system or size of files). Also if there is a logic error, you can keep executing functions recursively forever!

This chapter is all about limiting data, utilizing it, modifying it, internationalizing it, replacing it, and even searching for it in the first place.

主站蜘蛛池模板: 轮台县| 利川市| 灵武市| 苏州市| 商都县| 郁南县| 铁力市| 北川| 綦江县| 云龙县| 南阳市| 呼伦贝尔市| 南靖县| 广南县| 竹山县| 舞钢市| 慈溪市| 新宁县| 顺平县| 东源县| 新晃| 邵阳县| 元阳县| 通化县| 长汀县| 红河县| 宁晋县| 平利县| 佛教| 教育| 太湖县| 波密县| 江阴市| 随州市| 荣昌县| 寻乌县| 汝城县| 桐柏县| 正蓝旗| 班戈县| 封丘县|