I am trying to write a shell script which will read a file and counts the number of vowels in the file. The code which read the file is as follows -

while read -n 1 c
do
  l=$(echo $c | tr [:upper:] [:lower:])
  [[ "$l" == "a" || "$l" == "e" || "$l" == "i" || "$l" == "o" || "$l" == "u" ]] && (( v++ ))
done < $file

When I run the script, getting the error -
read: 22: Illegal option -n where 22 is the line number.
I am confused why -n option is not working in the script whereas read -n 1 c command is working fine from terminal.
Any suggestion is appreciated.
Thanks in advance.

Recommended Answers

All 2 Replies

The only reason I can think about is that the shells are different. What is in the #! line of the script, and what system you are running at?

The script is run in bash shell.
The line with #! is -

#!/bin/bash

Also when I run the command echo $SHELL it outputs /bin/bash i.e. both command and script running in same shell. But the command is working, not the script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.