Hello everyone,
i am fairly new to shell scripting so please bear with me.
The following script is supposed to read words from a file and depending on the word print different things.
However it always prints the default value.
It also refuses to work completely if the first line in 'numbers.txt' is not a blank line.

I would be grateful for any hints whatsoever.

script

#!/bin/bash

file="numbers.txt"

while read line 
do
operator=`awk '{print $1}' $line`
case $operator in
"divide") echo "/";;
"multiply") echo "x";;
"add") echo "+";;
"subtract") echo "-";;
*) echo "default";;
esac
done < "$file"

numbers.txt (the first line is a blank!)

divide
multiply
add
subtract

You can solve the problem in much simpler steps by just combining the for loop with cat command results.

#!/bin/bash
filename="input.txt"
for word in `cat $filename`
do
        case $word in
                "divide") echo "/" ;;
                "multiply") echo "x" ;;
                "add") echo "+" ;;
                "subtract") echo "-" ;;
                *) echo "default" ;;
        esac
done

oops, my bad, I didn't see that line operator=$(awk '{print $1}' $line)it's bad, it should be operator=$(awk '{print $1}' <<< "$line")and useless!

simply use case $line in..., as numbers.txt has only one column.

This question has already been answered. Start a new discussion instead.