We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,763 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

read command not working as expected

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.

2
Contributors
2
Replies
26 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
java_programmer
Junior Poster
125 posts since May 2006
Reputation Points: 10
Solved Threads: 18
Skill Endorsements: 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?

nezachem
Posting Shark
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 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.

java_programmer
Junior Poster
125 posts since May 2006
Reputation Points: 10
Solved Threads: 18
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.2928 seconds using 2.66MB