Bash shell script checker

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have to write a script, which gets a text file which contains 9 line and 9 number (1-9) in the lines. For example:

123456789

234567891

345678912

456789123

567891234

678912345

789123456

891234567

912345678

I have to check lines and the rows based on the sudoku rules. Can't be same number in a line or in a row. So the example is a correct solution.

How to write it in bash shell script? I cannot use arrays, so what can I do?

回答1:

A solution without awk, combining different other tools.
EDIT: This solution works for an inputfile called input and spaces between the digits. See comments about changing this behaviour.

echo "Checking 9 lines" if [ $(wc -l <input ) -ne 9 ]; then         echo "Wrong number of lines"         exit 1 fi echo "Check for correct layout" if [ $(grep -cv '^[1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9]$' input ) -ne 0 ]; then    echo "Not all lines are correct, maybe spaces at the end of a line?"    grep -v '^[1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9] [1-9]$' input    exit 1 fi i=0 while read -r line; do    ((i++))    if [ $(echo "${line}" | tr " " "\n" | sort -u | wc -l ) -ne 9 ]; then       echo "Wrong nr of unique numbers in row $i"    fi done <input for j in {1..9}; do    if [ $(cut -d" " -f${j} input | sort -u | wc -l ) -ne 9 ]; then       echo "Wrong nr of unique numbers in column $j"    fi done 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!