- Bash Cookbook
- Ron Brash Ganesh Naik
- 374字
- 2021-07-23 19:17:35
Getting ready
This recipe is going to take into account a few interesting things:
- Bash can't do everything well
- Other utilities exist (for example, bc)
- We can create our own in another language like C
- Creating a tarball
We will use the following command to make sure that we have all of the utilities installed for this experiment:
$ sudo apt-get install -y bc tar
Now, we need to create a tarball called archive.tar.gz:
$ dd if=/dev/zero of=empty.bin bs=1k count=10000
$ tar -zcvf archive.tar.gz empty.bin
$ rm empty.bin
We realize that the purpose of creating/compiling a simple program that is not written in Bash may be beyond the scope of this book, but it can be a helpful skill. To do this, we will need to install GCC, which is short for the GNU Compiler Collection. This sounds terribly complex and we assure you that we did all the hard work:
$ sudo apt-get install -y gcc
The preceding command installs the compiler, and now we need the C source code (in order to compile a simple C program). Open up a console and retrieve the code using:
$ wget https://raw.githubusercontent.com/PacktPublishing/Bash-Cookbook/master/chapter%2002/mhelper.c
To compile the code, we will use gcc and -lm (this refers to libmath) as follows:
$ gcc -Wall -02 -o mhelper main.c -lmath
If the compiler completes successfully (which it should), you will be left with a utility binary called mhelper (or math helper). We can also add this to our list of local commands by copying it to /bin using sudo and cp:
$ sudo cp mhelper /bin; sudo chmod a+x /bin/mhelper;
Now, mhelper can be used for basic operations such as division, multiplication, addition, and subtraction:
$ mhelper "var1" "-" "var2"
- JavaScript百煉成仙
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- INSTANT Mercurial SCM Essentials How-to
- 人人都懂設計模式:從生活中領悟設計模式(Python實現)
- 零基礎學Python數據分析(升級版)
- 從0到1:Python數據分析
- Python Web數據分析可視化:基于Django框架的開發實戰
- Node.js:來一打 C++ 擴展
- Python從入門到精通
- Unity 5.X從入門到精通
- IPython Interactive Computing and Visualization Cookbook
- Hadoop Blueprints
- Puppet Cookbook(Third Edition)
- 機器人ROS開發實踐
- Game Development with SlimDX