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

Recommended Answers

All 5 Replies

hi,

can you show the result of this command

cat -A numbers.txt
$
divide$
multiply$
add$
subtract$
Member Avatar for b1izzard

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.

Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.