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

  • 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.

As a note from the author, I realize that some of the computer science definitions are not computer science correct, meaning that I tweaked some of their meanings to make sure the key points got across in most general situations.

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).

  1. 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.
  2. After executing our script, the contents of the tarball (empty.bin) should be present in the current working directory.
Inside of the script, we put two different evaluations in the comments, which would return floating point values or errors in syntax. We included them for your awareness and to drive the main lesson home.
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!
主站蜘蛛池模板: 大同市| 武汉市| 南平市| 洛川县| 郴州市| 深州市| 犍为县| 柳河县| 丰都县| 车险| 衡山县| 灵石县| 乐平市| 凯里市| 镇沅| 日照市| 资中县| 都匀市| 曲麻莱县| 公安县| 永定县| 鄂州市| 习水县| 长垣县| 凤山县| 通江县| 韶关市| 龙江县| 湘乡市| 大余县| 万载县| 汤阴县| 成武县| 丰镇市| 贵德县| 察隅县| 丰顺县| 文安县| 光山县| 达拉特旗| 大邑县|