Shell脚本钻石

时间:2017-11-17 00:33:55

标签: linux bash shell unix

问题是:使用Shell脚本编程,创建一个脚本,要求用户输入一个数字,然后打印以下形状。行数是列的两倍,用户输入决定了菱形的大小。

                *
               * *
              * * *
             * * * *
            * * * * *
           * * * * * *
          * * * * * * *
         * * * * * * * *
          * * * * * * *
           * * * * * *
            * * * * *
             * * * *
              * * *
               * * 
                *

我好几天都在尝试这个。任何帮助都会很棒

3 个答案:

答案 0 :(得分:1)

#!/bin/bash                    

w=${1-5}                       
line() { printf "%$(($1+w))s\n" "$(yes "* " | sed ${1}q | tr -d \\n)"; }
for i in $(seq $w) $(seq $((w-1)) -1 1); do line $i; done

答案 1 :(得分:0)

我写过这样的smth。 'size'变量实际上是整个钻石的1/4。但这是化妆品,你应该能够解决它:

#!/bin/bash

# reading from stdin
read -p 'Size: ' size
# checking if size is a number
echo $size | grep -q -E '^[0-9]+$' || exit 5

vpos=0
while [[ $vpos -le $(( $size * 2 )) ]] ;
do
        hpos=0
        while [[ $hpos -le $(( $size * 2 )) ]] ;
        do

                if [[ $vpos -le $size ]] ;
                then
                        # upper vertical half
                        if ( [ $hpos -lt $(( $size - $vpos )) ] && [ $hpos -lt $size ] ) || ( [ $hpos -gt $(( $size + $vpos )) ] && [ $hpos -gt $size ] )  ;
                        then
                                echo -n ' '
                        else
                                echo -n '*'
                        fi

                else
                        # bottom vertical half
                        if ( [ $hpos -lt $(( $vpos - $size ))  ] && [ $hpos -lt $size ] ) || ( [ $hpos -gt $(( 3 * $size - $vpos )) ] && [ $hpos -gt $size  ] )  ;
                        then
                                echo -n ' '
                        else
                                echo -n '*'
                        fi

                fi

                hpos=$((hpos + 1))

        done ;
        echo ''
        vpos=$((vpos + 1))
done

答案 2 :(得分:0)

我已经为您编写了以下代码,希望对您有所帮助:

#!/usr/bin/env bash

#get the first argument passed to your script and assign it to STAR_NUMBER_MAX,
#if it is empty -> default value will be 0
readonly STAR_NUMBER_MAX="${1:-0}"

#check if your STAR_NUMBER_MAX is a number
re='^[0-9]+$'
if ! [[ $STAR_NUMBER_MAX =~ $re ]] ; then
   echo "error: your column_size is not a number" >&2; exit 1
fi
#variable that will be used to compute the number of stars to display per line
star_number=0
#variable that will be used in order to know if we have reached the max amount of stars
test_star_max=0
for i in `seq 1 $((2*STAR_NUMBER_MAX +1))`;
do
    #print the spaces
    for j in `seq $star_number $STAR_NUMBER_MAX`
    do
        echo -n " "
    done
    #print the stars
    for j in `seq 1 $star_number`;
    do
        if (( star_number == STAR_NUMBER_MAX )); then
            test_star_max=1
        fi
        echo -n "*"
    done
    #print the remaining stars
    for j in `seq 2 $star_number`;
    do
        echo -n "*"
    done
    if (( test_star_max == 0 )); then
        star_number=$((star_number + 1));
    else
        star_number=$((star_number - 1));
    fi
    echo ""
done

您描述了脚本的结果。

enter image description here