I’m relatively new to shell scripting and I’ve been stuck on this error for a couple days now. I’m trying to read in the contents of a file containing a list of strings and numbers, format it, and output the number of numbers below 50.
All the commands work when typed into the shell, however; in the script when I try and pass the filename in as an argument I keep getting a “No such file or directory” error.
Here is the function in question:
belowFifty(){
count=0
numbers=`cut -d : -f 3 < "$2"` #here is where the error occurs
for num in $numbers
do
if ((num<50));
then
count=$((count+1))
fi
done
echo $count
}
edit: sorry I forgot to mention the script does a couple things. $1 is the option, $2 is the file. I’m calling it like so:
./script.sh m filename
Try:
${2? 2 arguments are required to function belowFifty} numbers=$( cut -d : -f 3 < $2 )I suspect the problem is that you are calling the function
and not specifying the 2nd argument. Within the function,
$2 is the argument passed to the function, and not the argument
passed to the main script.