- Bash Cookbook
- Ron Brash Ganesh Naik
- 790字
- 2021-07-23 19:17:35
How it works...
First things first, as this recipe alluded to—we noticed that the Bash shell doesn't like decimal numbers with fractions or even non-whole numbers. Wait, math!? Unfortunately, we can't hide all the details, but in programming, there are a couple of concepts that you should be aware of:
- Signed and unsigned numbers
- Floats, doubles, and integers
The first concept is fairly simple—current computers are binary, which means they compute using zeros (0) and ones (1). This means that they work in powers of 2^. Without getting into a lesson about basic computer science, if you see a value (datatype) that is an int (integer) and that it is a 32 bit number, this means that the maximum value if it begins at 0 is 4,294,967,295 in decimal (2^32). This makes one critical assumption and that is that all numbers (0 included) are positive. This positive or negative property is called sign! If a datatype mentions signed or unsigned—now you know what it means!
However, there is a consequence of whether something is signed and that is that the maximum positive or negative value is decreased because one bit is used to represent sign. A signed 32 bit int (which can also be referred to as int32) shall now have a range of (-)2,147,483,647 to (+)2,147,483,647.
On another note, Bash only uses integers and you may have already seen that when you divide a value like 1/5, the answer is 0. True, it is not divisible, but the answer is 0.20 as a fraction. We also cannot multiply numbers that have a decimal point as well! Therefore, we have to use other programs such as bc or mhelper.
If you are keen on computers, you also know that there are floats, doubles, and other datatypes to represent numbers. Mhelper and bc can help you deal with these types of numbers when the concept of integers fails (for example, resulting numbers are not whole numbers when dividing).
- Back to the recipe, and in step 1:
- We created a script that will check the /home directory to determine how much size is available using the df command. Using tail, another command that can be used to reduce output, we skip the first line of output and pipe all output into the $CURRENT_PART_ALL variable (or all current partition information).
- Then, the contents of the $CURRENT_PART_ALL variable are read into an array using the read command. Notice the use of the re-direction errors: <<<. This is called a here-string, which in simple terms expands the variable and feeds it into stdin.
- Now, the /home partitions storage information is inside of an array, and we have a tarball (or a file that compresses and contains the contents within), where we need to know the size of the contents within the tarball. To do this, we use a long-winded command with multiple piped commands, which retrieves the size of the contained elements and pushes them through the bc command.
- Upon determining the size of the elements contained within our archive, we validate the calculated size against the remaining available space. This value is inside of the array element[1]. If the available space is less than equal to the extracted files, then exit. Otherwise, print the remaining size after performing.
- For fun, we combined forking a subshell to retrieve the division results of the mhelper, which are piped through bc. This is so we can determine if there is enough space as a mere boolean value of true (1) or false (0).
- Since we assume we have enough space, we untar (decompress and extract the contents) the $TARBALL. If the tar command returns a value not equal to 0, then exit with an error. Otherwise, exit with success.
- After executing our script, the contents of the tarball (empty.bin) should be present in the current working directory.
Did we miss anything? Absolutely! We never checked the size of the tarball itself and made sure that its size was among the used space when performing a check to determine the remaining free space. One should always be careful when performing and enforcing size restrictions!
- 演進式架構(原書第2版)
- Web程序設計及應用
- SOA實踐
- WSO2 Developer’s Guide
- 網頁設計與制作教程(HTML+CSS+JavaScript)(第2版)
- SAP BusinessObjects Dashboards 4.1 Cookbook
- Java Web開發就該這樣學
- Beginning C++ Game Programming
- Django 5企業級Web應用開發實戰(視頻教學版)
- Appcelerator Titanium:Patterns and Best Practices
- 超簡單:用Python讓Excel飛起來(實戰150例)
- C語言程序設計
- 游戲設計的底層邏輯
- Visual C#(學習筆記)
- Mastering Magento Theme Design