Automatic update data file and display?

前端 未结 2 2039
温柔的废话
温柔的废话 2021-01-27 08:33

I am trying to develop a system where this application allows user to book ticket seat. I am trying to implement an automatic system(a function) where the app can choose the bes

2条回答
  •  误落风尘
    2021-01-27 09:05

    Here's something to get you started. This script reads in each seat from your file and displays if it's taken or empty, keeping track of the row and column number all the while.

    #!/bin/bash
    
    let ROW=1
    let COL=1
    
    # Read one character at a time into the variable $SEAT.
    while read -n 1 SEAT; do
        # Check if $SEAT is an X, 0, or other.
        case "$SEAT" in
            # Taken.
            X)  echo "Row $ROW, col $COL is taken"
                let COL++
                ;;
    
            # Empty.
            0) 
                echo "Row $ROW, col $COL is EMPTY"
                let COL++
                ;;
    
            # Must be a new line ('\n').
            *)  let ROW++
                let COL=1
                ;;
        esac
    done < seats.txt
    

    Notice that we feed in seats.txt at the end of the script, not at the beginning. It's weird, but that's UNIX for ya. Curiously, the entire while loop behaves like one big command:

    while read -n 1 SEAT; do {stuff}; done < seats.txt
    

    The < at the end feeds in seats.txt to the loop as a whole, and specifically to the read command.

提交回复
热议问题