I have a program that is currently working, but I need to modify it to ignore some stdin that is not fitting for its correct function.
Right now, to run the program: printf "1\n3\n5\n" | sh prog
The program currently ignores non-integer input (like floats), but I also need it to ignore something like '4 10' on the same line and '5 text' etc.
#! /bin/sh
sum=0;
cnt=0
while read line
do
case "$line" in
*[.]* ) #------I think here is where the regex needs to be edited
printf "\n0"
continue
;;
[0-9]* )
sum=`expr "$sum" + "$line"`
cnt=`expr "$cnt" + 1`
printf "\n%s" `expr $sum / $cnt`
;;
esac
done
I'm pretty sure it's just a matter of changing the regex on the line I pointed out so that it goes to the print 0 and continue case with the two non-desired input types I described above but I am having trouble with it.
Thank you!
[0-9]* )
as first condition, then make the second condition*)
to catch all other thing.*[.]*
means anything or nothing followed by a single character followed by anything or nothing. I'm pretty sure that' not what you intended.[[ $foo ~= pattern ]]
(see the man page). Withshopt -s extglob
, you can use things likefoo.@(zip|7z)
in glob expressions. (also ?, *, +, and ! operators).