I have an assigment that asks me to print on screen the number of words that are longer than a given number ,let's say k , which is read from the keyboard. and then to order the result. Until now I decided to try in this way :
#!bin/bash
k=0
if [ $# -eq 0 ]
then
echo "No argument supplied."
exit 1
fi
echo -n "Give the minimal lenght of the words : "
read k
for files in "$@"
do
if [ -f "$files" ]; then
echo "$(cat $files | egrep -o '[^ ]{k,}' $files | wc -w) : $files."
else
echo "Error: File $files has not been found."
fi
done | sort -n
My issues is that whenever I try this program with k in the section "egrep -o '[^ ]{k,}'" it always gives a wrong answer. But if I replaced it with an integer, in works exactly as I wanted.
Which is the right way to make this code work for k read from keyboard? which is the syntax , can't really understand how I should write there , tried other ways to like "$k" , $k , ((k)) , k
.
Any help is welcome , a hint if someone could give me please? I am stuck