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

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.

主站蜘蛛池模板: 阜新| 新邵县| 陆良县| 永靖县| 苗栗市| 渝中区| 荣成市| 海林市| 锦屏县| 清徐县| 会昌县| 西宁市| 松溪县| 兴义市| 精河县| 南城县| 海口市| 绥德县| 策勒县| 东港市| 仙游县| 汉沽区| 武宁县| 巫溪县| 清新县| 太和县| 武夷山市| 辽阳县| 清原| 白玉县| 株洲县| 东乡县| 东乡县| 安泽县| 永城市| 尚志市| 正镶白旗| 临安市| 平定县| 乌鲁木齐市| 甘肃省|