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

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.

主站蜘蛛池模板: 晋州市| 安多县| 东明县| 金塔县| 安顺市| 广丰县| 娱乐| 余姚市| 长宁县| 集安市| 利川市| 当阳市| 长海县| 安多县| 和静县| 鲁山县| 通化市| 日喀则市| 阳春市| 手游| 吉林省| 阜康市| 灵宝市| 邵阳市| 青川县| 溧水县| 太保市| 临漳县| 安泽县| 子洲县| 汕头市| 淮北市| 娄烦县| 阿巴嘎旗| 章丘市| 云和县| 武乡县| 郁南县| 农安县| 定襄县| 靖江市|