1.11M Members

read command not working as expected

 
0
 

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.

 
0
 

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?

 
0
 

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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article