I was hoping someone could give me a hand. I'm just starting to learn shell scripting in Unix using the Bourne shell and I've run into a problem.
I'm trying to make a script that takes a file with a series of digits and reads through it with a while loop and checks if the number is larger than 30. If it is, it will increment a counter. At the end it echoes how many times there was a number larger than 30.
The file looks similar to this but larger.
0107
0027
0110
and this is my code
#!/bin/sh
count=0
while read p;do
if ["$p" -ge 30 ]
then
count=`expr $count + 1`
fi
done < $1
echo " has logged in for more than 30 minutes $count times "
In the end I expected this to say you logged in 2 times but instead I get this error for each number in the file. Here is an example.
./scriptname: 4: ./scriptname: [0107: not found
Anyway I was thinking that perhaps it wasn't treating the integers as integers but instead as strings. Maybe I didn't format my "if" properly but I'm not sure what's wrong; that's why I've come here.
You need to add a single space to your if
like
if [ "$p" -ge 30 ]
The reasons for this should become clear if you
$ type [
and
$ which [