[Linux error] ex4.sh: 44 lines: [: 100: Unary operator expected

[Error message] Unary operator is expected

Errors that occurred in exercises using if and while statements I'm not sure, so I'll leave the cause and solution.

-There is a possibility that there is wasted space (sometimes full-width space) in or around the pointed out line. -If there is a blank, an error message may be displayed because normal processing cannot be performed. This error message has multiple causes, so if you can't resolve it, see other articles.

This time, the command that identified the cause is listed at the bottom of the article, so please check it.


The code I wrote

File



    1  #!/bin/bash
     2
     3  read -p 'Please enter the file name' fh
     4  if [ -f $fh ];
     5  then
     6          read -p  'sum, avg, min, max, exit ' command
     7          if [ $command = 'sum' ];
     8          then
     9                  sum=0
    10                  while read p;
    11                  do
    12                    sum=$(( sum + p ))
    13                  done < $fh
    14                  echo SUM: $sum
    15                  exit 0
    16          elif [ $command = 'avg' ];
    17          then
    18                  sum=0
    19                  count=0
    20                  while read p;
    21                  do
    22                    sum=$(( sum + p ))
    23                    count=$(( count + 1 ))
    24                  done < $fh
    25                  echo AVG: $(( sum / count ))
    26                  exit 0
    27          elif [ $command = 'min' ];
    28          then
    29                  min=101
    30                  while read p;
    31                  do
    32                          if [ $min -gt $p ];
    33                          then
    34                                  min=$p
    35                          fi
    36                  done < $fh
    37                  echo MIN: $min
    38                  exit 0
    39          elif [ $command = 'max' ];
    40          then
    41                  max=0
    42                  while read p;
    43                  do
    44                          if [ $max -lt $p ];
    45                          then
    46                                  max=$p
    47                          fi
    48                  done < $fh
    49                  echo MAX: $max
    50                  exit 0
    51          elif [ $command = 'exit' ];
    52          then
    53              exit 0
    54          else
    55                  echo 'There is no such command'
    56                  exit 1
    57          fi
    58  else
    59      echo 'The file name you entered does not exist'
    60      exit 1
    61  fi

ex4.sh: 44 lines: [: 100: Unary operator expected

Since there was "wasted space" in a part of the called file, the space was also calculated during processing. I got an error message

I ran the following command to identify the cause. This command is "you can check what kind of command is being executed".

bash command


bash -x file name

Reference article

・ 1. Suppress "unary operator expected" https://www.bee-planetz.com/blog/%E3%80%8C%E5%8D%98%E9%A0%85%E6%BC%94%E7%AE%97%E5%AD%90%E3%81%8C%E4%BA%88%E6%9C%9F%E3%81%95%E3%82%8C%E3%81%BE%E3%81%99%E3%80%8D%E3%82%92%E6%8A%91%E5%88%B6%E3%81%99%E3%82%8B/

・ 2. Error when specifying the number part of the array with a variable in bash https://okwave.jp/qa/q8362685.html

Recommended Posts

[Linux error] ex4.sh: 44 lines: [: 100: Unary operator expected
linux command error collection 1