Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell script programming-output diamonds, triangles or digital pyramids (with flashing colors) according to the number of inputs

Shulou Source: shulou.com Published: 2022-06-02 04:44:57 10月03日 Update

Shell script programming-output diamonds, triangles or digital pyramids (with flashing colors) according to the number of inputs

The shell script and explanation are as follows

#! / bin/bash# creates an endless loop. If you enter an error or enter the "quit" string to exit the script while truedo# and wait for the user to enter read-p, enter a positive integer not greater than 20 (enter "quit" to exit): "n # first determine whether the string is quitif [" $n "= =" quit "] then echo" you choose to exit "exit 0fi# determines whether the input is numeric or not If not, the error termination script if [[$n = ~ ^ [0-9] + $]] then: else echo "sorry you input no a number!" Exit 1fi# determines whether the input number is greater than 20, then jump out of this cycle and enter the next cycle if [$n-gt 20] then echo "the number you entered is too large, I may not be able to execute" continue# indicates that the number entered is not more than 20 Determine whether it is odd or even elif [[$n% 2-eq 1]] # determine whether it is odd then echo "you entered an odd number" echo "will display n lines of diamond" let half= (echo) / 2 let otherhalf=half-1 # echo $half for i in `seq 1$ half` do for ((j=$half) J > ibomacher -) do echo-n "done for m in `seq 1$ i` do echo-ne"\ 033 [5Trac34m*\ 033 [0m "done echo done for i in `half1 $otherhalf`do for m in `seq $i`half` do echo-n"done for ((jobless others halfwitj > = I) ) do echo-ne "\ 033 [5 34m *\ 033 [0m" done echo doneelse echo "you entered an even number" # indicates that you entered an even number not less than 20 # to determine whether the even number is less than 10 if [$n-lt 10] then for i in `seq 1$ n` do for ((jobless than n` do for j > I) ) do echo-ne "" done for m in `seq 1$ i` do echo-ne "\ 033 [1x 36m done echo done # indicates that the even number not less than 20 is not less than 10 else for i in `m 1$ n` do for ((jacks seq 1$ n` do for) ) do echo-n "" done for m in `do echo 1$ i` do echo-n "*" done echo done fifidone

You can copy and understand a wave of things directly by yourself after execution.

Tags: Input even script number odd program loop diamond character string triangle script programming gold word pyramid color triangle programming design output integer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info vpn Docker Redmi