bash脚本:main中未调用printLine()函数

时间:2019-03-02 02:41:39

标签: bash shell loops

我的总体目标是在终端屏幕上输出8x8的棋盘图案。我已经编写了一个递归函数,可以打印棋盘格的一个8x1行,从黑色或白色瓷砖开始,这取决于我是否从带有选项标志的终端调用脚本。该函数称为printLine(),我相信它可以完美地工作。但是,当我在main()的循环中调用它时,它仅执行一次,然后程序继续运行,就像在等待用户输入一样。

就像我说的那样,我对printLine()非常有信心,并且我也对main()中的循环有效,因为我已经用简单的回声“ hi”替换了printLine()函数调用语句,并且在运行脚本时它会按循环指定的次数多次打印“ hi”,因此,我不确定为什么循环在其主体包含循环而不是简单的echo语句时不迭代调用printLine() 。我还没有将一些未使用的常量用于测试目的,因此请忽略这些常量。

# Script 1 - Checkerboard 
# 
# Name: xxx  
# PID: xxx  
# Account: xxx  

# ============================== DO NOT CHANGE ============================== #

# Constants used
TILE_BLACK="\033[0;40m  \033[0;0m"    # DO NOT CHANGE
TILE_WHITE="\033[0;47m  \033[0;0m"    # DO NOT CHANGE
SQUARES_PER_LINE=8                    # DO NOT CHANGE
HALF_DIVISOR=2                        # DO NOT CHANGE
NUM_ITERATIONS=4                      # DO NOT CHANGE
ERR="\tERROR: Invalid flag given.\n"  # DO NOT CHANGE
USAGE="USAGE: ./checkerboard.sh [-r]" # DO NOT CHANGE

rotate_flag="f"

# Parse command line arguments
while getopts ":r" arg $@; do
  case $arg in

    # rotation wanted
    r) rotate_flag="t";;

    # Invalid flag provided
   \?) echo; echo -e "$ERR"; echo "$USAGE"; exit 1;;

  esac
done

# Remove parsed option flags
shift $((OPTIND - 1))

# ========================= YOUR CODE STARTS BELOW ========================= #

# FIXME: Write a function that prints one row of the checkerboard. It should
# look at the argument passed in to determine if it will start by printing a
# white or black square. This is an optional method, but will help you
# implement the main method.
count=0;
function printLine() { # One way to define a function
    until [ "$count" == 8 ]; do
    if  [ "$1" ==  "$TILE_WHITE" ]; then
    echo -en "$TILE_WHITE "
    count=$(($count + 1))
    printLine "$TILE_BLACK"
  elif [ "$1" == "$TILE_BLACK" ]; then       
    echo -en "$TILE_BLACK"
    count=$(($count + 1))
    printLine "$TILE_WHITE"
  fi
  done
}

function main() { # Another way to define a function

  counter=0

  if [ $rotate_flag == "t" ]; then
    # This line creates a loop that will run NUM_ITERATIONS times
    #while (!cond)
    until [ "$counter" == 4 ]; do

      # FIXME: Use the print functions to draw two REVERSED rows of the board
      count2=0;
      until [ "$count2" == 2 ]; do
         printLine "$TILE_BLACK"
         printLine "$TILE_WHITE"
         count2=$(($count2 + 1))
      done
      counter=$(($counter + 1))
   done

  else
    until [ $counter -eq $NUM_ITERATIONS ]; do
      # FIXME: Use the print functions to draw two rows of the board
      count3=0;
      until [ "$count2" == 2 ]; do
         printLine "$TILE_BLACK"
         printLine "$TILE_WHITE"
         count3=$(($count3 + 1))
      done
      counter=$(($counter + 1))
    done
  fi
}

# ============================== DO NOT CHANGE ============================== #
main # Calls the main function to start the script up
exit 0      

2 个答案:

答案 0 :(得分:1)

printLine使变量count递增,但是永远不会重置。

因此,一旦$count == 8until循环就什么也不做。

答案 1 :(得分:0)

count3=0;
until [ "$count2" == 2 ]; do
    printLine "$TILE_BLACK"
    printLine "$TILE_WHITE"
    count3=$(($count3 + 1))
done 

这是一个死循环。您检查count2的循环结束条件,但只更新count3。修复它。