Publi

mayúsculas y minúsculas en BASH (toupper(), tolower()) [copia y pega]

mayuscula En ocasiones, es muy útil crear scripts en bash, aunque en ocasiones debemos controlar las mayúsculas y minúsculas, por ejemplo renombrando archivos, escribiendo informes o para ajustarse a diferentes convenciones a la hora de hacer un programa. Aquí dejo ejemplos de funciones tipo strtoupper(), strtolower(), por decir un equivalente en PHP.

Hay opciones, la elección de alguna de estas opciones depende del entorno en que nos encontremos, ya sea BASH, algún otro clon, y dispongamos de awk o tr.

La primera de ellas es para poner un texto en mayúsculas:

1
2
3
4
function uppercase()
{
    echo "$1" | awk '{print toupper($0)}'
}

Otra opción puede ser:

1
2
3
4
function uppercase()
{
    echo "$1" | tr 'a-z' 'A-Z'
}

Si usamos la versión 4.0 o superior de BASH, que ya tiene un tiempo, podemos hacer lo siguiente:

1
2
3
4
function uppercase()
{
    echo "${^^*}"
}

La segunda de ellas para poner un texto en minúsculas:

1
2
3
4
function lowercase()
{
    echo "$1" | awk '{print tolowe($0)}'
}

Otra opción puede ser:

1
2
3
4
function lowercase()
{
    echo "$1" | tr 'A-Z' 'a-z'
}

Si usamos bash 4.0 o superior podemos hacerlo mucho más fácil!

1
2
3
4
function lowercase()
{
     echo "${1,,*}"
}

También podría interesarte....

There are 6 comments left Ir a comentario

  1. Pingback: Bitacoras.com /

  2. Psipes /
    Usando Google Chrome Google Chrome 121.0.0.0 en Windows Windows NT

    Este es un contenido muy interesante y útil para los que quieren aprender a crear scripts en bash y manejar las mayúsculas y minúsculas de forma sencilla y eficiente. Has dado algunos ejemplos de funciones que se pueden usar para convertir un texto a mayúsculas o minúsculas, dependiendo del entorno y de las herramientas disponibles. Me parece una buena forma de mostrar las diferentes opciones y sus ventajas e inconvenientes.
    geometry dash

  3. Alice Bobby /
    Usando Google Chrome Google Chrome 121.0.0.0 en Windows Windows NT

    For people who wish to learn how to write bash scripts and manage upper and lower case in an effective and easy manner, this is highly interesting and helpful stuff. build now gg

  4. Sapna Mathur /
    Usando Google Chrome Google Chrome 122.0.0.0 en Windows Windows NT

    Our Noida Escorts are known for being well-behaved, educated, and skilled when it comes to bed. When needed, they act as friends who listen to whatever might be bothering you while offering romance and sex services to take your mind off any problems or take you straight up into heaven through sexual encounters.

  5. rishu /
    Usando Google Chrome Google Chrome 123.0.0.0 en Windows Windows NT

    Always enjoy the Service provided by the Udaipur Agency. They will serve you the world class female model in Udaipur and the surrounding area. You can try your Service without worry. They support that employees will do their best to fulfill your needs, even if necessary, replace the Escorts in Udaipur girls. According to the commitment of quality and Service to their clients.

  6. Usando Google Chrome Google Chrome 123.0.0.0 en Windows Windows NT

    Hello, I read this nice article. I think You put a best effort to write this perfect article. I appreciate your work. thank you so much.

Leave a Reply