效果如下图: image.png

    1. #!/bin/bash
    2. #设置两个变量,i 和 j,一个代表行,一个代表列,国际象棋为 8*8 棋盘
    3. #i=1 是代表准备打印第一行棋盘,第 1 行棋盘有灰色和蓝色间隔输出,总共为 8 列
    4. #i=1,j=1 代表第 1 行的第 1 列;i=2,j=3 代表第 2 行的第 3 列
    5. #棋盘的规律是 i+j 如果是偶数,就打印蓝色色块,如果是奇数就打印灰色色块
    6. #使用 echo ‐ne 打印色块,并且打印完成色块后不自动换行,在同一行继续输出其他色块
    7. for i in {1..8}
    8. do
    9. for j in {1..8}
    10. do
    11. sum=$[i+j]
    12. if [ $[sum%2] eq 0 ];then
    13. echo ne "\033[46m \033[0m"
    14. else
    15. echo ne "\033[47m \033[0m"
    16. fi
    17. done
    18. echo
    19. done
    1. #!/bin/bash
    2. #set chess cell's width
    3. read -p "Please set the chess cell's width( two space width as unit ):" width
    4. if [[ $width =~ "^[0-9]+$" ]];then
    5. echo "wrong width setting, check your input and try again."
    6. exit
    7. fi
    8. let width=$width*2
    9. #choose player's board cell color
    10. player="player1"
    11. PS3="Which color do you want to set for $player :"
    12. select choice in red green yellow blue purple cyan white;do
    13. case $REPLY in
    14. [1-7])
    15. if [[ $player == player2 ]];then
    16. declare -i color2=$REPLY
    17. break
    18. else
    19. declare -i color1=$REPLY
    20. fi
    21. player="player2"
    22. PS3="Which color do you want to set for $player :"
    23. ;;
    24. *)
    25. ;;
    26. esac
    27. done
    28. if (( color1==color2 ));then
    29. echo "two player must choose different color, check your choice and try again."
    30. exit
    31. fi
    32. #print the chess board
    33. for (( i=0; i<4; i++ )); do
    34. for (( j=0; j<$width/2; j++ ));do
    35. for (( k=0; k<4; k++ ));do
    36. echo -e "\e[4${color1}m$(printf %${width}s)\e[0m\c"
    37. echo -e "\e[4${color2}m$(printf %${width}s)\e[0m\c"
    38. done
    39. echo
    40. done
    41. for (( j=0; j<$width/2; j++ ));do
    42. for (( k=0; k<4; k++ ));do
    43. echo -e "\e[4${color2}m$(printf %${width}s)\e[0m\c"
    44. echo -e "\e[4${color1}m$(printf %${width}s)\e[0m\c"
    45. done
    46. echo
    47. done
    48. done