このbashスクリプトで生成されたボックスの途中に*を追加するにはどうすればよいですか?

このbashスクリプトで生成されたボックスの途中に*を追加するにはどうすればよいですか?

*このbashスクリプトで生成されたボックスの中央にどのように追加しますか?

#!/bin/bash
#
# raami joonistamine
echo -n "sisesta ridade arv: "
read rida
echo -n "sisesta tärnide arv: "
read tarn
# genereeri rea numbrid
for ((i = 1; i <= $rida;i++))
do
    echo -n "$i "
    # kui on esimene või viimane rida
    if [ $i -eq 1 -o $i -eq $rida ]; then
    # tärnidest tulev rida
    for((j = 1; j <=$tarn; j++))
    do
        echo -n "* "
    done
# teised read
    else
        echo -n "* "
        # tühikud
        for((j = 2; j < $tarn;j++))
        do
            echo -n "  "
        done
    echo -n "* "
    fi
    echo
done

ベストアンサー1

最も内側のループを変更します。

# tühikud
for((j = 2; j < $tarn;j++))
do
    echo -n "  "
done

到着

# tühikud
for((j = 2; j < $tarn;j++))
do
    if [ "$i" -eq "$(( (rida+1) / 2 ))" ] && [ "$j" -eq "$(( (tarn+1) / 2 ))" ]; then
        echo -n '* '
    else
        echo -n "  "
    fi
done

つまり、最も中央にある文字を出力したいことを検出したら、*スペースの代わりにaを挿入します。

行内で少し正確な位置を指定するには、次の手順を実行します。

# tühikud
for((j = 2; j < $tarn;j++))
do
    if [ "$i" -eq "$(( (rida+1) / 2 ))" ] && [ "$j" -eq "$(( (tarn+1) / 2 ))" ]; then
        if [ "$(( tarn%2 ))" -eq 0 ]; then
            echo -n ' *'
        else
            echo -n '* '
        fi
    else
        echo -n "  "
    fi
done

個々の文字を出力する代わりに、行全体を一度に出力します。これはより効率的で、3種類の行(上/下行、中間行、その他の行)を気にするだけです。

#!/bin/bash

read -r -p 'Height: ' rows
read -r -p 'Width : ' cols

topbottom=$( yes '*' | head -n "$cols" | tr '\n' ' ' )
printf -v midrow '*%*s*%*s*' "$(( cols - 2 ))" "" "$(( cols - 2 ))" ""
printf -v otherrows '*%*s*' "$(( 2*(cols - 2) + 1 ))" ""

for (( row = 0; row < rows; ++row )); do

    if (( row == 0 )) || (( row == rows - 1 )); then
        thisrow=$topbottom
    elif (( row == rows / 2 )); then
        thisrow=$midrow
    else
        thisrow=$otherrows
    fi

    printf '%2d %s\n' "$(( ++n ))" "$thisrow"
done

おすすめ記事