mardi 24 mars 2015

Bourne shell: ignoring certain kinds of stdin


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!



Aucun commentaire:

Enregistrer un commentaire