📅  最后修改于: 2023-12-03 15:00:44.060000             🧑  作者: Mango
As a programmer, sometimes you may encounter situations where you need to generate random CNPJs (Cadastro Nacional de Pessoa Jurídica), which is an identification number for Brazilian companies. In this project, we will create a Bash function that generates random CNPJs for test or dummy data purposes.
The Bash function fake_cnpj
generates a random CNPJ following the Brazilian format: XX.XXX.XXX/XXXX-XX
. The structure of the CNPJ is as follows:
| XX | XXX | XXX | XXXX | XX |
|------|-------|-------|------|------|
| Root | Branch| Branch| Code | DV |
function fake_cnpj(){
cnpj=$(echo $((RANDOM % 90000000 + 10000000))$((RANDOM % 90000 + 10000)))
root=$(echo $cnpj | cut -c 1-8)
branch=$(echo $cnpj | cut -c 9-11)
code=$(echo $cnpj | cut -c 12-15)
d1=$((($root / 1000000) * 5 + (($root % 1000000) / 100000) * 4 + (($root % 100000) / 10000) * 3 + (($root % 10000) / 1000) * 2 + (($root % 1000) / 100) * 9 + (($root % 100) / 10) * 8 + ($root % 10) * 7))
d2=$((($branch / 10) * 9 + ($branch % 10) * 8 + $d1))
dv1=$((11 - ($d2 % 11)))
if [ $dv1 -ge 10 ]; then
dv1=0
fi
d3=$((($code / 1000) * 2 + (($code % 1000) / 100) * 9 + (($code % 100) / 10) * 8 + ($code % 10) * 7))
dv2=$((11 - (($d2 * 10 + $dv1 + $d3) % 11)))
if [ $dv2 -ge 10 ]; then
dv2=0
fi
echo "${root:0:2}.${root:2:3}.${root:5:3}/${branch}-${dv1}${dv2}"
}
$ fake_cnpj
42.097.563/8564-90
In this project, we have created a Bash function fake_cnpj
that generates random CNPJs following the Brazilian format. This can be useful for generating test or dummy data for your applications.