Lo básico
Así que aquí imprimiremos la pirámide de estrellas en dos partes como se muestra a continuación. Recorreremos el número proporcionado por el usuario e imprimiremos la primera mitad de las estrellas usando un bucle for y la otra mitad usando otro bucle for. Los espacios y los caracteres de nueva línea se agregan en una sección diferente.

El guión
1. Edite el archivo /tmp/star_pyramid.sh y agregue el siguiente script:
#!/bin/bash
makePyramid()
{
# Here $1 is the parameter you passed with the function i,e 5
n=$1;
# outer loop is for printing number of rows in the pyramid
for((i=1;i<=n;i++))
do
# This loop print spaces required
for((k=i;k<=n;k++))
do
echo -ne " ";
done
# This loop print part 1 of the the pyramid
for((j=1;j<=i;j++))
do
echo -ne "*";
done
# This loop print part 2 of the pryamid.
for((z=1;z<i;z++))
do
echo -ne "*";
done
# This echo is used for printing a new line
echo;
done
}
# calling function
# Pass the number of levels you need in the parameter while running the script.
makePyramid $1 2. Proporcione permisos ejecutables en el script.
# chmod +x /tmp/star_pyramid.sh
3. Mientras ejecuta el script, proporcione la cantidad de niveles que desea en la salida. Por ejemplo:
$ /tmp/star_pyramid.sh 10
*
***
*****
*******
*********
***********
*************
***************
*****************
******************* Otra manera
Aquí hay otra forma de imprimir la pirámide de estrellas usando un script de shell.
#!/bin/bash
clear
echo -n "Enter the level of pyramid: "; read n
star=''
space=''
for ((i=0; i<n; i++ ))
do
space="$space "
done
echo "$space|"
for (( i=1; i<n; i++ ))
do
star="$star*"
space="${space%?}"
echo "$space$star|$star";
done Haga que el script sea ejecutable y ejecútelo.
$ /tmp/star_pyramid.sh
Enter the level of pyramid: 10
|
*|*
**|**
***|***
****|****
*****|*****
******|******
*******|*******
********|********
*********|********* Pirámide de Números usando script de shell
De manera similar a los 2 ejemplos anteriores, también puede imprimir una pirámide de números usando el siguiente script.
#!/bin/bash read -p "How many levels? : " n for((i = 0; i < n; i++)) do k=0 while((k < $((i+1)))) do echo -e "$((i+1))\c" k=$((k+1)) done echo " " done
Haga que el script sea ejecutable y ejecútelo.
$ /tmp/star_pyramid.sh How many levels? : 5 1 22 333 4444 55555