A few interesting things with the bash script

Tram Ho

After a bit of naughty learning about bash scripts today, I have summarized some functions that can be encountered when using bash scripts. I would like to skip some of the basic definitions and focus more on practice

Declare variables in bash scripts

Declare local variable

We are all familiar with how to declare a variable in bash as follows

Result:

This variable will be used throughout the script. Even inside the function, it is still usable or declared inside the function, and outside it is still usable.

However, in some cases if you want to limit the scope of a variable to functions only, we can use the local variable as follows:

Result:

Get the value of the variable from the string

Result

Alternatively, assign a value to another variable as follows

Result

Some functions are often used with String

Replace string

Result

Cut string

Result

Use array in bash script

Browse all elements of the array

Result

Merge 2 arrays

Result

Add element to array

Add element to the beginning of the array

Result

Add element at the end of the array

Result

Add element to the specified position of the array

To do this need to follow 3 steps as follows

  1. Gets all elements before index position “x”
  2. Add an element to the array.Add an element to the array
  3. Retrieve all elements from index position “x” and later,

For example, the format below: Add element 4 to the index = 2 position of the array

Result

Remove element from array

We will need to perform 2 steps

  1. Gets all elements before index position “x”
  2. Retrieves all elements from index position “x” + “n” or later

For example, the format below: Remove element from position index = 2 of the array

Result

Alternatively, you can use another method when using the unset function as follows

Result

Another way when you know the exact value of the element in the array you can use the same alternative as replacing the string in the format.

Here, just using 1 / symbol is enough to replace all map values

Result

Working with files

Suppose we have a file named text.txt with the following content:

Read all lines in a file

Result

Edit the file

Add to the beginning of the file

Result

This way we will do it by creating a file with a .tmp extension and then executing the mv command to turn the tmp file into the original file name.

Add any line of the file

We can use it using the sed command with the format <line>i<PATTERN> .

For example below we will add the first line of the file with the content “Line number 0”. Note here that <line> must be less than or equal to the number of lines in the file

Result

Add to the end of the file

Result

Note here if we only use 1 > sign then it means that will create a new file for example as follows

Result

Replace a text encountered in the file

We will continue to use it with the sed command with the following format

For example, we will replace “Line” with “This is line”.

Result

Read yaml file from bash script

After a while of searching, luckily I found a link to write github function quite well enough to read yaml file with the following content:

The function parse_yaml needs two input values, namely the file name and the prefix value. The create_variables function is to declare variables after parseing the keys in the file. As in the example below we have the database.yml file and the prefix is config_

We will execute the following command

Result

You can use the sample file to try out the cases.

Above are some of the functions I have just learned. Thank you for watching.

Share the news now

Source : Viblo