如何检查shell中正确的STDIN用户输入量?

I'm trying to solve a problem where you write a script and then get:

  1. multiple test cases with 5 integer user inputs
  2. one test case with one single 0

I want to check the number of user inputs and then determine which code is executed.

Something like this:

if(fscanf(STDIN, '%d%d%d%d%d', $a, $b, $c, $d, $e) == 5) {
        echo "Five inputs";
    } elseif(fscanf(STDIN, '%d', $a) == 1) {
        echo "Only one input";
    }

So if you get for example the input 1 2 3 4 5 it should echo "Five inputs" but if you only get 0 it should echo "Only one input".

There are a couple of problems with your code. The first is that you scan using %d%d%d%d%d, this is looking for a continuous number, you would at least have to have spaces in there to delimit the values. The second is that if this fails, it will ask for input again for the single input check.

This code reads a line from the input and then splits it (using explode()) into parts. Te middle bit just checks for numerical values and you can adjust this if needed. Then the last part just checks how many parts there are (using count($inputs)) and outputs the appropriate message.

$input = fgets(STDIN);
$inputs = explode(" ", $input);
// Check numeric
foreach ( $inputs as $index => $check )   {
    if ( !is_numeric($check) )  {
        echo "Input $index is not numeric".PHP_EOL; 
    }
}
if(count($inputs) == 5) {
    echo "Five inputs";
} elseif(count($inputs) == 1) {
    echo "Only one input";
}