Read command not working as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read command not working as expected
# 1  
Old 02-26-2012
Read command not working as expected

I was trying to write a simple script which will read a text file and count the number of vowels in the file. My code is given below -
Code:

#!/bin/bash
file=$1
v=0

if [ $# -ne 1 ]
  then
    echo "$0 filename"
    exit 1
fi

if [ ! -f $file ]
  then
    echo "$file not a file"
    exit 2
fi

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

echo "Vowels : $v"

When I run the script, getting the following error -
read: 22: Illegal option -n
However the command read -n 1 c is working fine if I run it from terminal.
Any suggestion is appreciated.
Thanks in advance.

Last edited by jim mcnamara; 02-26-2012 at 11:38 PM.. Reason: please use code tags, not bold tags
# 2  
Old 02-26-2012
I cut and pasted your code and it works fine for me (reading with -n 1). Is it possible that the version of bash that is being invoked isn't the same one executed as your login shell?

You could try something like this that doesn't depend on reading character by character:


Code:
while read buf
do
    vbuf="${buf//[^aeiouAEIOU]/}"
    v=$(( v + ${#vbuf} ))
done <$file
echo "$v vowels"



This works in both bash and Kshell.
This User Gave Thanks to agama For This Post:
# 3  
Old 02-27-2012
A similar method - use "tr" to delete everything except the vowels and count the result.
Code:
file="$1"
vowels=$(fold -w 1 "${file}" | tr -cd 'aeiouAEIOU' | wc -c)
echo "Vowels : ${vowels}"


Last edited by methyl; 02-27-2012 at 12:58 PM.. Reason: correction suggested by alister
# 4  
Old 02-27-2012
Can try this too...
Code:
# egrep -io "a|e|i|o|u"  $1 | wc -w

eg.
Code:
[root@nagios Shirish@Shukla]# cat my
aa b c d e f g h i j k l m n o p q r s t u v w x y z
[root@nagios Shirish@Shukla]# cat test.sh
echo "Vovels: "`egrep -io "a|e|i|o|u" $1 | wc -w`
[root@nagios Shirish@Shukla]# sh test.sh my
Vovels: 6

--Shirish Shukla

Last edited by Shirishlnx; 02-27-2012 at 11:33 AM..
This User Gave Thanks to Shirishlnx For This Post:
# 5  
Old 02-27-2012
Quote:
Originally Posted by methyl
A similar method - use "tr" to delete everything except the vowels and count the result.
Code:
file="$1"
vowels=$(fold -w 1 "${file}" | tr -cd 'aeiouAeiou' | wc -c)
echo "Vowels : ${vowels}"

I don't see the need for fold since the complemented tr set will delete non-vowels anyway (including newlines). Also, it looks like your finger slipped off the capslock. Smilie

Code:
tr -cd aeiouAEIOU < "$file" | wc -c

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 6  
Old 02-27-2012
Thanks everyone for your suggestion. But my focus was to run the script, not the algorithm to count the vowels. Sorry for misguiding you.
My problem is that whenever I am trying to run any script from terminal, getting unusual errors like no such commands or invalid option with the command or syntax error etc. But the same commands are working from the terminal separately.
Do I need any settings to run the script? In the script, I am using bash shell, and the same for terminal. If anyone has any suggestion, please let me know.
Thanks in advance.
# 7  
Old 02-27-2012
Just on the off chance, it's not the same problem as before is it?
https://www.unix.com/shell-programmin...-terminal.html

Please post the output from this command which is designed to make control characters visible:
Code:
sed -n l scripname

A normal end-of-line character shows as a dollar sign.
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Why this script is not working as 'expected' when doing ssh with while read ... really confused?

Hi, I have a script below that is running ssh <host> <command> on some servers. Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames check_log.bash #!/bin/bash # myPID=$$ parse_log () { sub="parse_log" host=${1} ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

3. Shell Programming and Scripting

awk command not working as expected

Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Discussion started by: later_troy
31 Replies

4. Shell Programming and Scripting

Cp command not working as expected in HPUX

Hi, I'm having trouble with a simple copy command in a script on HPUX. I am trying to copy a file and append date & time. The echo command prints out what I am expecting.. echo "Backing up $file to $file.$DATE.$FIXNUM" | tee -a $LOGFILE + echo 'Backing up... (4 Replies)
Discussion started by: Glennyp
4 Replies

5. Shell Programming and Scripting

ash busybox read command not working inside function....

I have a script that has to execute a read command in a function, this is in an ash busybox. The code is... trapcatch () { echo "Ctl-c Detected, what do you want to do?" echo "Please choose the number of one of the following options" echo "1. Jump past this Set" echo "2. Exit... (8 Replies)
Discussion started by: tesser
8 Replies

6. UNIX for Dummies Questions & Answers

-atime not working as expected

I need to sort through a volume that contains video files by access time and delete files that have not been accessed over x days. I have to use the access time as video files are originals that do not get modified, just read Testing commands on a local test folder... $ date Wed Sep 28... (10 Replies)
Discussion started by: canon273
10 Replies

7. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

8. OS X (Apple)

Cat command not working as expected

I've been trying to figure this out since last night, and I'm just stumped. The last time I did any shell scripting was 8 years ago on a Unix box, and it was never my strong suit. I'm on a Mac running Leopard now. Here's my dilemma - hopefully someone can point me in the right direction. I'm... (10 Replies)
Discussion started by: Daniel M. Clark
10 Replies

9. UNIX for Dummies Questions & Answers

Find command not working as expected

I have a script with a find command using xargs to copy the files found to another directory. The find command is finding the appropriate file, but it's not copying. I've checked permissions, and those are all O.K., so I'm not sure what I'm missing. Any help is greatly appreciated. This is... (2 Replies)
Discussion started by: mpflug
2 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question