How to convert an IPV4 netmask to CIDR

To switch from an IPV4 netmask to the equivalent CIDR it might occur to do the simplest thing, that is to use a nested “case” block or “if” to obtain the equivalent CIDR so much so that the available netmasks or CIDR are only 32 and that is NetmaskAddress/CIDRAdresses255.255.255.255a.b.c.d/3220255.255.255.254a.b.c.d/3120255.255.255.252a.b.c.d/3021255.255.255.248a.b.c.d/2922255.255.255.240a.b.c.d/2823255.255.255.224a.b.c.d/2724255.255.255.192a.b.c.d/2625255.255.255.128a.b.c.d/2526255.255.255.0a.b.c.0/2427255.255.254.0a.b.c.0/2328255.255.252.0a.b.c.0/2229255.255.248.0a.b.c.0/21210255.255.240.0a.b.c.0/20212255.255.224.0a.b.c.0/19213255.255.192.0a.b.c.0/18214255.255.128.0a.b.c.0/17215255.255.0.0a.b.0.0/16216255.254.0.0a.b.0.0/15217255.252.0.0a.b.0.0/14218255.248.0.0a.b.0.0/13219255.240.0.0a.b.0.0/12220255.224.0.0a.b.0.0/11221255.192.0.0a.b.0.0/10222255.128.0.0a.b.0.0/9223255.0.0.0a.0.0.0/8224254.0.0.0a.0.0.0/7225252.0.0.0a.0.0.0/6226248.0.0.0a.0.0.0/5227240.0.0.0a.0.0.0/4228224.0.0.0a.0.0.0/3229192.0.0.0a.0.0.0/2230128.0.0.0a.0.0.0/12310.0.0.00.0.0.0/0232Netmask to CIDR If we want instead Continue Reading

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 Continue Reading

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 Continue Reading

Using associative arrays in bash

Vedi la faq in italiano To declare an associative array, you must do so declare -A mioarray and as an index you can use a string, this means that mioarray[test]=’aa’ echo ${mioarray[test]} # returns ‘aa’  

Using simple arrays in bash

Vedi la faq in italiano 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] While to change the value you have to write mioarray[5]=’new value’ ${mioarray[*]} o ${mioarray[@]} is expanded by all array values while $[#mionome[@]} is expanded with Continue Reading

Redirect the standard error to the standard output

Vedi la faq in italiano 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 ls file-that-does-not-exist 2>/dev/null The standard open or standard streams that are automatically opened Continue Reading

Rename a group of files based on a regex

Vedi la faq in italiano 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”: rename ‘s/_tmp//’ *.sh