|
| 1 | + |
| 2 | +Get Started |
| 3 | +=========== |
| 4 | + |
| 5 | +It is trivial to get started using gobash, because it only adds to the |
| 6 | +knowledge you already have about shell programming. Also, it does not |
| 7 | +require any special setup, because everything is pure bash. |
| 8 | + |
| 9 | +There are two modes in which gobash can be used: (a) as a library, |
| 10 | +or (b) as a tool. Most of the examples below use gobash as a library |
| 11 | +(i.e., source gobash into a script and use available functions). There |
| 12 | +is a separate section to describe using gobash as a tool. |
| 13 | + |
| 14 | +We will use several examples in this section to get you started. |
| 15 | + |
| 16 | +Prepare Environment |
| 17 | +------------------- |
| 18 | + |
| 19 | +Create a space for trying gobash: |
| 20 | + |
| 21 | +.. code-block:: bash |
| 22 | +
|
| 23 | + mkdir space |
| 24 | + cd space |
| 25 | +
|
| 26 | +Clone the gobash repository: |
| 27 | + |
| 28 | +.. code-block:: bash |
| 29 | +
|
| 30 | + git clone [email protected]:EngineeringSoftware/gobash |
| 31 | +
|
| 32 | +Alternatively, you can avoid cloning the repo and directly source the |
| 33 | +library in your bash script: |
| 34 | + |
| 35 | +.. code-block:: bash |
| 36 | +
|
| 37 | + source /dev/stdin <<< "$(curl https://raw.githubusercontent.com/EngineeringSoftware/gobash/master/hsabog 2>/dev/null)" |
| 38 | +
|
| 39 | +Now, we are ready to run some examples. |
| 40 | + |
| 41 | +Collection Example |
| 42 | +------------------ |
| 43 | + |
| 44 | +Write your first script (let's call it `s`) that uses gobash. The |
| 45 | +example below "imports" the entire gobash library and uses the `List` |
| 46 | +collection for storing values. |
| 47 | + |
| 48 | +.. code-block:: bash |
| 49 | +
|
| 50 | + #!/bin/bash |
| 51 | + . gobash/gobash |
| 52 | + |
| 53 | + # Instantiate a list and add two elements. |
| 54 | + lst=$(List) |
| 55 | + $lst add 55 |
| 56 | + $lst add 100 |
| 57 | + |
| 58 | + # Get the length of the list. |
| 59 | + $lst len |
| 60 | + # Output: 2 |
| 61 | + |
| 62 | + # Print the list (default print is in the json format). |
| 63 | + $lst to_string |
| 64 | + # Output: |
| 65 | + # [ |
| 66 | + # "55", |
| 67 | + # "100" |
| 68 | + # ] |
| 69 | +
|
| 70 | +Struct Example |
| 71 | +-------------- |
| 72 | + |
| 73 | +In the following example (`point.sh`), we introduce a `struct` for a |
| 74 | +2D point, set/get values, and write a function to add two points. |
| 75 | + |
| 76 | +.. code-block:: bash |
| 77 | +
|
| 78 | + #!/bin/bash |
| 79 | + . gobash/gobash |
| 80 | + |
| 81 | + function Point() { |
| 82 | + make_ $FUNCNAME \ |
| 83 | + "x" "$1" \ |
| 84 | + "y" "$2" |
| 85 | + } |
| 86 | + |
| 87 | + function point_add() { |
| 88 | + local p1="${1}" |
| 89 | + local p2="${2}" |
| 90 | + |
| 91 | + local x=$(( $($p1 x) + $($p2 x) )) |
| 92 | + local y=$(( $($p1 y) + $($p2 y) )) |
| 93 | + local p3=$(Point "${x}" "${y}") |
| 94 | + echo "$p3" |
| 95 | + } |
| 96 | + |
| 97 | + p1=$(Point 3 4) |
| 98 | + p2=$(Point 8 9) |
| 99 | + p3=$(point_add "$p1" "$p2") |
| 100 | + $p3 to_string |
| 101 | + # Output: |
| 102 | + # { |
| 103 | + # "x": "11", |
| 104 | + # "y": "13" |
| 105 | + # } |
| 106 | +
|
| 107 | +Test Example |
| 108 | +------------ |
| 109 | + |
| 110 | +This example illustrate a way to write tests using a testing package. |
| 111 | +The tests can be executed with the gobash tool. |
| 112 | + |
| 113 | +We will extend the previous example to add tests (`point_test.sh`) for |
| 114 | +the function `point_add`. |
| 115 | + |
| 116 | +.. code-block:: bash |
| 117 | +
|
| 118 | + #!/bin/bash |
| 119 | + |
| 120 | + . point.sh |
| 121 | + |
| 122 | + function test_point() { |
| 123 | + p1=$(Point 3 4) |
| 124 | + p2=$(Point 8 9) |
| 125 | + p3=$(point_add "$p1" "$p2") |
| 126 | + assert_ze $? |
| 127 | + assert_eq 11 $($p3 x) |
| 128 | + assert_eq 13 $($p3 y) |
| 129 | + } |
| 130 | +
|
| 131 | +Tests can be run with the following command: |
| 132 | + |
| 133 | +.. code-block:: bash |
| 134 | +
|
| 135 | + ./gobash test --paths point_test.sh --verbose |
| 136 | +
|
| 137 | +The output of this execution will be along these lines: |
| 138 | + |
| 139 | +.. code-block:: bash |
| 140 | +
|
| 141 | + test_point start |
| 142 | + test_point PASSED |
| 143 | + ./point_test.sh 1[sec] |
| 144 | + Tests run: 1, failed: 0, skipped: 0. |
| 145 | + Total time: 1[sec] |
| 146 | +
|
| 147 | +Further Reading |
| 148 | +--------------- |
| 149 | + |
| 150 | +There are a number of other examples that illustrate gobash in the |
| 151 | +`examples directory |
| 152 | +<https://github.com/EngineeringSoftware/gobash/tree/main/examples/README.md>`_. |
| 153 | +If you like learning by examples, that is the best place to go next. |
| 154 | +If you prefer to read higher level doc, then check |
| 155 | +:doc:`language`. |
| 156 | + |
| 157 | +.. toctree:: |
| 158 | + :maxdepth: 2 |
0 commit comments