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

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!
主站蜘蛛池模板: 浏阳市| 彰武县| 高碑店市| 桃园县| 乐安县| 古浪县| 沽源县| 六盘水市| 巴东县| 上虞市| 会宁县| 舞钢市| 东山县| 津南区| 措勤县| 察雅县| 隆安县| 深州市| 阜平县| 贡山| 阿巴嘎旗| 固始县| 新河县| 兴山县| 普定县| 万年县| 大姚县| 乐至县| 阳城县| 潍坊市| 五大连池市| 安岳县| 麻栗坡县| 钟山县| 潍坊市| 宜章县| 观塘区| 刚察县| 浪卡子县| 宜君县| 安国市|