- Bash Cookbook
- Ron Brash Ganesh Naik
- 372字
- 2021-07-23 19:17:35
How to do it...
Using the mhelper binary, bc, and other expressions, we can begin the recipe:
- Begin by opening a terminal and an editor to create a new script called mathexp.sh with the following contents:
#!/bin/bash
# Retrieve file system information and remove header
TARBALL="archive.tar.gz"
CURRENT_PART_ALL=$(df --output=size,avail,used /home -B 1M | tail -n1)
# Iterate through and fill array
IFS=' ' read -r -a array <<< $CURRENT_PART_ALL
# Retrieve the size of the contents of the tarball
COMPRESSED_SZ=$(tar tzvf "${TARBALL}" | sed 's/ \+/ /g' | cut -f3 -d' ' | sed '2,$s/^/+ /' | paste -sd' ' | bc)
echo "First inspection - is there enough space?"
if [ ${array[1]} -lt ${COMPRESSED_SZ} ]; then
echo "There is not enough space to decompress the binary"
exit 1
else
echo "Seems we have enough space on first inspection - continuing"
VAR=$((${array[0]} - ${array[2]}))
echo "Space left: ${VAR}"
fi
echo "Safety check - do we have at least double the space?"
CHECK=$((${array[1]}*2))
echo "Double - good question? $CHECK"
# Notice the use of the bc command?
RES=$(echo "$(mhelper "${array[1]}" "/" "2")>0" | bc -l)
if [[ "${RES}" == "1" ]]; then
echo "Sppppppaaaaccee (imagine zombies)"
fi
# We know that this will break! (Bash is driven by integers)
# if [ $(mhelper "${array[2]}" "/" "2") -gt 0 ]; then
#~ echo "Sppppppaaaaccee (imagine zombies) - syntax error"
# fi
# What if we tried this with Bash and a concept again referring to floats
# e.g., 0.5
# It would break
# if [ $((${array[2]} * 0.5 )) -gt 0 ]; then
# echo "Sppppppaaaaccee (imagine zombies) - syntax error"
# fi
# Then untar
tar xzvf ${TARBALL}
RES=$?
if [ ${RES} -ne 0 ]; then
echo "Error decompressing the tarball!"
exit 1
fi
echo "Decompressing tarball complete!"
exit 0
- Now, running the script should produce an output similar to the following:
First inspection - is there enough space?
Seems we have enough space on first inspection - continuing
Space left: 264559
Safety check - do we have at least double the space?
Double - good question ? 378458
Sppppppaaaaccee (imagine zombies)
empty.bin
Decompressing tarball complete!
Great! So we can use Bash size calculations, the bc command, and our binary. If you want to calculate the radius of a circle (which will certainly get you a float value) or a percentage, for example, you will need to be aware of this limitation in Bash.
To bring this recipe to a close, it is important to note that the expr command still exists, but it is deprecated. Using $(( your equation )) is recommended as the preferred method in new scripts.
Using the same premise using the mhelper binary or $((..)) scripting, you can also calculate percentages for situations that need variable output (for example, not whole numbers). For example, calculating screen sizes based on percentages while a whole number will be desired, you can then round up or down post calculation.
推薦閱讀
- The Complete Rust Programming Reference Guide
- JavaScript百煉成仙
- 數(shù)據(jù)庫(kù)原理及應(yīng)用(Access版)第3版
- 編寫(xiě)高質(zhì)量代碼:改善Python程序的91個(gè)建議
- Android 7編程入門(mén)經(jīng)典:使用Android Studio 2(第4版)
- Visual Basic程序設(shè)計(jì)教程
- Python數(shù)據(jù)可視化之Matplotlib與Pyecharts實(shí)戰(zhàn)
- Java EE 7 Performance Tuning and Optimization
- Node.js全程實(shí)例
- 從Java到Web程序設(shè)計(jì)教程
- Learning Laravel's Eloquent
- Protocol-Oriented Programming with Swift
- 軟件供應(yīng)鏈安全:源代碼缺陷實(shí)例剖析
- QlikView Unlocked
- C++程序設(shè)計(jì)教程