FAQs: bash
How to convert an IPV4 netmask to CIDR
Convert a decimal number to hexadecimal from Bash
As for the faq with the conversion to binary, also in this we make use of the fantastic bash braces extensions to convert to hexadecimal.
So, always considering the bash faq on the binary we start initially by converting a decimal number between 0 and 255 which corresponds to a hexadecimal digit between 0 and FF:
(more…)Convert a decimal number to binary from Bash
Suppose we are working on a bash script and that for some need, not too difficult from reality, it is essential to have to convert a number or a decimal variable into a binary number.
There are of course several solutions but the one I propose does not make use of external programs but it is bash in all respects.
First we need to know the maximum value of the binary number to be obtained in Bash or in how many bits it can be included.
(more…)Using associative arrays in bash
To declare an associative array, you must do so
declare -A mioarray
and as an index you can use a string, this means that
(more…)Using simple arrays in bash
With Bash to explicitly declare an array you have to use
declare -A mioarray
To have the value of element 5 just write
echo ${mioarray[5]}
(more…) Redirect the standard error to the standard output
To redirect the standard error of a shell command, just use the following syntax:
ls file-that-does-not-exist 2>&1 | grep file
Or even better not to have on the terminal the standard error just throw it in /dev/null
(more…)Rename a group of files based on a regex
Suppose you want to rename the name of a group of files that have the following template:
file1_tmp.sh
file2_tmp.sh
...
file99_tmp.sh
and suppose you want to remove the '_tmp' middle part in all the files. We can use the command "rename" or "prename":
(more…)